Forum > Beginners
A lot If-Else-If
Josh:
Hi
You may find it easier if you block your if then ...... else .... with begin end; it makes it easier to follow ie
--- 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 {Condition A} thenbegin if {Condition B} then begin ..... end // no ; because else is following else begin {Else Section of Condition B} if {Condition C} then begin ..... if {Condition D} then begin ....... if ??????? then begin ...... end else begin if ?????????? then begin ...... end else begin ...... end; end; end // no ; else following else {Else Section of Condition D} begin if ????????? then begin .... end else begin if ?????????? then begin ..... end else begin ...... end; end; end; {End Section of Condition D} end; {End Section of Condition C} end; {End Section of Condition B}end; {End of Condition A}
--- 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";}};} ---program Project1;{$mode objfpc}{$H+}uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Classes { you can add units after this }; var x:integer=2; y:integer=4; z:integer=0; begin if (x > y) then begin write ('X is bigger'); z := x+y; if (z > x) then begin writeln('Z is bigger'); end else begin writeln('X still bigger'); end; end // no ; this allows an else statement ie if then else; else // i think you want the whole next section executed in th else block; so try using a begin end block begin // added if (x > y) then begin writeln ('Y is bigger'); end else begin // added if (x=y) then // changed from == to = begin writeln ('x & y are the same'); end else begin writeln('something wrong happend'); end; end; // added end; // end of else section so ;end.
Handoko:
Or use colored outline of source structure feature, which is already available since Lazarus 1.8.0. Read more:
http://wiki.freepascal.org/Lazarus_IDE_Tools#Outline
But I personally will try to rewrite the code if the if clause has more than 3 levels, maybe try other logics or move some parts of them to a local nested procedure.
Zoran:
This line with "==" will surely fail to compile:
--- Quote from: NightWing on May 08, 2018, 03:40:21 pm ---
--- 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";}};} ---else if (x == y) then
--- End quote ---
Kays:
Why are you engaging him*her in such a bad design? Nested if-then-elses starting at a level of, prrprprp, four are getting ridiculous. And error-prone, as the OP's question proves.
Bart:
This code seems to calculate how much Peta/Giga/Mega/Kilo-bytes a certain amount of bytes is?
The names of the variables confuse me: they seem to be the names of planets in the solar system?
Bart
Navigation
[0] Message Index
[#] Next page
[*] Previous page