if then else syntax lazarus / 21-General ---------------------------------------------------------------------- waltfair · 2023-06-01 21:47 What is wrong with this snippet of code? The compiler complains "; expected but ELSE found." 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(i>= Length(s))then t:=s;  elsebegint:= Copy(s,1,i);s:= Copy(s,i+1,Length(s)-i);end;Firstword:= t;end; ---------------------------------------------------------------------- eljo · 2023-06-01 21:49 Quote from: waltfair on June 01, 2023, 11:47:50 pm What is wrong with this snippet of code? The compiler complains "; expected but ELSE found." 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(i>= Length(s))then t:=s;  elsebegint:= Copy(s,1,i);s:= Copy(s,i+1,Length(s)-i);end;Firstword:= t;end;  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(i>= Length(s))then t:=s//;<--- this semicolon is wrong.  elsebegint:= Copy(s,1,i);s:= Copy(s,i+1,Length(s)-i);end;Firstword:= t;end; ---------------------------------------------------------------------- Kays · 2023-06-02 20:45 [In addition to eljo’s reply, some explanation:] Remember that in Pascal the semicolon is a statement separator, not a terminator like in many other programming languages.The if statement is composed of ifBoolean expressionthenstatementplus (optional) else andstatement.Note, it says statement (singular), not statements (plural). Thus you have t:=s as one statement. A second statement (properly separated by ;) as part of the then branch is not allowed according to the syntax repeated above. ---------------------------------------------------------------------- Paolo · 2023-06-03 11:34 this compile, even if it looks strange 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   repeat  if condition1 then  until condition2;  no statement after then (or better hidden no-statement without ";"). ---------------------------------------------------------------------- 440bx · 2023-06-03 12:09 As @Kays pointed out, semicolon in Pascal is a statement separator (with one exception in Delphi and FPC), this means that "else" cannot be preceded by a semicolon because "else" is not a statement. Quote from: Paolo on June 03, 2023, 01:34:53 pmthis compile, even if it looks strange 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   repeat  if condition1 then  until condition2;  no statement after then (or better hidden no-statement without ";"). the "repeat until" pair also includes the compound statement functionality (that's why it does not require a "begin/end" pair to apply to multiple statements.)  Because of this, the following also compiles: 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   repeat  if condition1 then ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;   until condition2; where each semicolon separates an empty statement (there is an empty statement after the "then".)  The example could have also been written as:  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   repeat  if condition1 then begin end  until condition2; and adding semicolons after the "end" would have the same effect (which is none) as in the previous example. I find terminators instead of separators easier to understand. ---------------------------------------------------------------------- Paolo · 2023-06-03 13:02 Yes I know. But in case I write such code, I prefer to add either ';' or empty 'begin..end' just for better readability and make clearer the code intention (intentional empty statement, no code mistake). ---------------------------------------------------------------------- 440bx · 2023-06-03 13:06 Quote from: Paolo on June 03, 2023, 03:02:34 pmBut in case I write such code, I prefer to add either ';' or empty 'begin..end' just for better readability and make clearer the code intention (intentional empty statement, no code mistake). I do too.  I normally use a lone semicolon to indicate an empty statement. ---------------------------------------------------------------------- jamie · 2023-06-03 14:37 Quote from: Paolo on June 03, 2023, 03:02:34 pmYes I know. But in case I write such code, I prefer to add either ';' or empty 'begin..end' just for better readability and make clearer the code intention (intentional empty statement, no code mistake). C/C++ programmers!