Forum > General
if then else syntax
waltfair:
What is wrong with this snippet of code?
The compiler complains "; expected but ELSE found."
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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:
--- 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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- if(i>= Length(s))then t:=s; elsebegint:= Copy(s,1,i);s:= Copy(s,i+1,Length(s)-i);end;Firstword:= t;end;
--- End quote ---
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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:
[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
* if
* Boolean expression
* then
* statement
* plus (optional) else and
* statement.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:
this compile, even if it looks strange
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- repeat if condition1 then until condition2;
no statement after then (or better hidden no-statement without ";").
440bx:
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 pm ---this compile, even if it looks strange
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- repeat if condition1 then until condition2;
no statement after then (or better hidden no-statement without ";").
--- End quote ---
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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.
Navigation
[0] Message Index
[#] Next page