Code:
IF ... THEN
BEGIN
WriteLn('1');
WriteLn('2');
WriteLn('3');
END;
This is fine.
Code:
BEGIN
IF ... THEN
BEGIN
WriteLn('1');
WriteLn('2');
WriteLn('3');
END;
END;
This is not. You already encapsulated the THEN part of the IF, so why do you want to encapsulate the IF itself as well?
IF x THEN dostuff ELSE dostuff
is treated as ONE statement by PAL/Pascal.
So is WHILE x DO dostuff, REPEAT dostuff UNTIL x and FOR x:=1 TO 2 DO dostuff
There's no need to wrap either of these in BEGIN END.
Obviously if you want to put that inside another thing and combine it with one or more other commands, then you need BEGIN and END, but only because that outer thing wants it, not because of IF or any of the LOOPS need to be encapsulated.
This is the original code from my website:
Code:
{**
* SAM Broadcaster Web2.0 Connector (Twitter)
*
* @copyright 2011 by Benedikt Bauer | http://www.sam-song.info
* @version 11.07-11b
* @link http://www.sam-song.info
* @author Benedikt Bauer
*
* @license
* The contents of this file are subject to the
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright for this License (C) 2004 Sam Hocevar <sam@hocevar.net>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
*}
/////////////////////////////////////////////////////////////////////
// Nettiquette //
// You may use this PAL Script to communicate with my server //
// Change it to your like but don't flood the server/network //
// //
// Also make sure you don't post any inappropriate messages //
// Either me or twitter/fb might then cease to run this service //
// //
// This service has no guaranteed uptime nor support //
// However you may ask Questions in the Spacialaudio Forums //
// I will probably come back to you and help //
// //
// The Webservice will stay private //
// At least until its completely done and tidied up //
// //
// //
// Don't make any changes outside of boxes like this //
// unless you really know what you do. //
// I won't give any support if you changed something //
// you better shouldn't have touched //
// //
/////////////////////////////////////////////////////////////////////
{Declaration (Variables)}
var Player : TPlayer;
var Song : TSongInfo;
var statusmessage, returnmessage, picture : String;
var ErrorLog, DebugLog : String;
{Declaration (Functions and Procedures)}
// Construct the GET String for the Web Script, call it and return the output
FUNCTION update(status, picture : String) : String; forward;
// Write Error / Debug Log
PROCEDURE WriteLog(msg : String; error : boolean); forward;
// BEGIN MAIN
PAL.Loop := True;
PAL.WaitForTime('+00:15:00');
// reliably detect ActivePlayer (needed for SAM <= 4.9.0, fixed in 4.9.1)
//PAL.LockExecution;
//IF (DeckA.Duration - DeckA.CurTime) >= (DeckB.Duration - DeckB.CurTime) THEN
// Player := DeckA
//ELSE
// Player := DeckB;
//PAL.UnlockExecution;
// SAM >= 4.9.1 can use this one:
Player := ActivePlayer;
// Is anything playing right now?
IF ( Player <> NIL ) THEN
BEGIN
Song := Player.GetSongInfo;
IF (Song <> NIL) AND (Pos(Song['songtype'],'SJI') > 0) THEN
BEGIN
// Message to display in Twitter
statusmessage := Song['artist'] + ' - ' + Song['title'];
picture := Song['picture'];
IF Pos('jpg', picture) = 0 THEN
BEGIN
picture := 'na.gif'
END;
returnmessage := update(statusmessage, picture);
// Errormessages don't contain the underscore
IF ( Pos('##', returnmessage) = 0 ) THEN
BEGIN
WriteLog(returnmessage, TRUE)
END
// For debugging purposes Successful Posts will be logged as well
ELSE
WriteLog(returnmessage, FALSE);
END;
END;
Song.Free;
Player.Free;
// END MAIN
FUNCTION update(status, picture : String) : String;
var getstr,returnstr : String;
BEGIN
// URLEncode Blanks and Special Chars
status := URLEncode(status);
picture := URLEncode(picture);
getStr := 'http://dev.sam-song.info/twitter/'
+ '?message=' + status + '&userid=123456';
WriteLog(getStr, FALSE);
// Here goes the magic!
returnstr := WebToStr(getStr);
result := returnstr;
END;
PROCEDURE WriteLog(msg : String; error : boolean);
BEGIN
IF NOT SetCurrentDir('C:\sam-song.info') THEN
BEGIN
CreateDir('C:\sam-song.info');
SetCurrentDir('C:\sam-song.info');
END;
PAL.LockExecution;
msg := DateTimeToStr(NOW) + ' ' + msg + chr(13) + chr(10);
Writeln(msg);
IF error THEN
BEGIN
//ERROR LOG create if not exists
IF FileExists('twitter-error.log') THEN
AppendStringToFile('twitter-error.log', msg)
ELSE
SaveStringToFile('twitter-error.log', msg)
END
ELSE
BEGIN
//DEBUG LOG create if not exists
IF FileExists('twitter-debug.log') THEN
AppendStringToFile('twitter-debug.log', msg)
ELSE
SaveStringToFile('twitter-debug.log', msg);
END;
PAL.UnlockExecution;
END;
If you look closely you can see that BEGIN and END are only used INSIDE of some PAL/Pascal internal structure (IF, WHILE, FUNCTION, PROCEDURE), but never outside.
My code is even indented to help for better readability.
Code works a lot better if you have an editor that has syntax highlighting for the programming language. (In case of PAL, the closest relatives are Pascal and Delphi)
VS Code, SublimeText or even Notepad++ if nothing else is available are good choices.
If you're really completely new to programming, I'm pretty impressed by how far you got without any major problems so far.
Keep up the good work and don't take my critique personally. I'm really not good at critizizing people without making it look like I want to blame them in english.
