period vs semicolon lazarus / 21-General ---------------------------------------------------------------------- waltfair · 2023-04-30 17:50 Can someone explain when to use a period rather than a semicolon in code? I thought that the period indicated the end of code in a unit, but I keep getting an error and always need to change a semicolon for a period or vice versa. I know that's a real newbie question, but I'd appreciate the clarification. ---------------------------------------------------------------------- TRon · 2023-04-30 17:53 The dot is only required when a module (program/unit) is ending. For use of semicolons see https://wiki.freepascal.org/; and https://wi ki.freepascal.org/Basic_Pascal_Tutorial/Chapter_1/Punctuation_and_Inde ntation In case that isn't very helpful for you then please show some example code that you are struggling with. Note that you can omit a semicolon for an end statement but that for newbies that can feel a bit inconsistent. It is no shame when always adding a semicolon in such cases as it does not matter much for the compiler. ---------------------------------------------------------------------- wp · 2023-04-30 17:57 Your are right. But this error seems to indicate that some "end;" statement is missing somewhere inside your unit. A great helping tool to match the "begin"/"end" pairs is the "outline" feature of the IDE. It gives each "begin"/"end" of the same logical block the same color. The default settings, for example, are that the last "end;" of a procedure must be red (like the first "begin" of that same procedure). So, you simply scroll though your unit and find procedures where the last "end;" is not red. To activate this feature right-click on the source editor, go to "Editor" > "Display" > "Markup and Matches" and check the "Outline (global)" box. ---------------------------------------------------------------------- Curt Carpenter · 2023-04-30 18:30 Did not know that!  Thanks! ---------------------------------------------------------------------- MarkMLl · 2023-04-30 20:02 I'd add that END. (with no intervening space) is a special token that goes /way/ back: it means "you really have reached the end of the program, and don't try reading the next card since it really isn't any of your business" :-) MarkMLl ---------------------------------------------------------------------- waltfair · 2023-05-01 00:07 Yes, that was my understanding, since way back in Turbo Pascal as well as Delphi. Thanks, Walt ---------------------------------------------------------------------- Curt Carpenter · 2023-05-01 04:14 A number of Pascal's notational quirks are I think rooted in the Backus Normal Form (Bakus-Naur) metalanguage that was used in the formal definition of ALGOL with Wirth's involvement and which he later shaped into the language we use here.  Most notably the " := " which I think is a shortened version of BNF's " ::= " ---------------------------------------------------------------------- MarkMLl · 2023-05-01 07:19 Quote from: Curt Carpenter on May 01, 2023, 06:14:30 amA number of Pascal's notational quirks are I think rooted in the Backus Normal Form (Bakus-Naur) metalanguage that was used in the formal definition of ALGOL with Wirth's involvement and which he later shaped into the language we use here.  Most notably the " := " which I think is a shortened version of BNF's " ::= " END. goes back to a specific manufacturer's implementation of ALGOL-60. Dijkstra is reported approving, but it did result in a quirk: other usages of END caused the remainder of the line (card) on which they were to be treated as a comment (repurposed in Modula-2 etc. to contain the function name), while END. had to be precise with nothing appended- at least in my experience. := was more interesting. Apparently when IAL (ALGOL-58) was being designed the German contingent wanted a special assignment operator which had been coined by Zuse (and for which no Unicode glyph exists). The American contingent held out for := on the basis that those characters were universally supported by the six-bit characters sets of the day. ALGOL-60 leant towards the APL character set with an arrow for assignment and special inequality operators, but allowed := etc. as alternatives. MarkMLl ---------------------------------------------------------------------- Warfley · 2023-05-01 14:19 The probably not so useful but technical answer is, semicolons are seperators between statements and definitions, as well as a seperator between different blocks. So they must always come between two things. A period must always come at the end of the file, because nothing else comes afterwards this is not a seperator and therefore not a semicolon. So the semicolon seperates two things, the period ends a thing with nothing coming afterwards Example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Test;  // Semecolon because now uses or definition blocks start procedure Foo; // Semicolon because new definition block startsvar  a: Integer;  // Semicolon between definitions  b: Integer;  // Semicolon because code block startsbegin  a := 42;     // Semicolon because seperation between two statements  b := a + 2   // no semicolon because last statementend;           // Semicolon because code block ends/program code block starts begin  Foo          // No semicolon because only one statementend.           // Period because no new block comes afterwards, only end of file As you can see, no semicolons are needed for the last statement in a series of statements. The reason for this is simply because as I wrote above, semicolons just seperate statements and are therefore only required if another statement follows. But as an exception to this rule Pascal allows to put semicolons there anyway to ease development. The only point where this is not the case is in an if-then-else statement: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if then else As this is one continous statement, there cannot be a semicolon in between. Meaning if you do the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if a < 0 then  b := 42; // Semicolon as end of statement would "break" the if statement and thererefore else is wrong hereelse  b := 32Therefore there should not be a semicolon before an else, because this is one statement that contains another statement, not a list of multiple statements. Also it should be noted that a begin-end block is nothing other than a statement that consists of multiple statements so if you put a begin- end block after an if, this whole block is just a way to turn multiple statements into one statement: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if a < 0 thenbegin  b := 42;   // Semicolon because list of statements  c := 3.14  // no semicolon because last statement in infend            // no semicolon because this statement is just part of if-then-else and not part of a list of statementselse  b := 32;  // Semicolon because if-then-else is just one statement in list of statements followed by writelnWriteLn(b); // ... ---------------------------------------------------------------------- MarkMLl · 2023-05-01 15:02 Quote from: Warfley on May 01, 2023, 04:19:01 pmBut as an exception to this rule Pascal allows to put semicolons there anyway to ease development. The only point where this is not the case is in an if- then-else statement: I've also seen it explicitly prohibited before    until    but can't remember the compiler... I think some versions of Turbo. Perhaps a few other places, I'm afraid that I'm a bit of a fossil and try to only put it in where actually needed. MarkMLl