Infinite Loop with Label lazarus / 61-Beginners ---------------------------------------------------------------------- OlivenHandcreme · 2022-01-27 16:35 Hey Lazarus-Free Pascal forum, im a totally beginner in Pascal/Lazarus IDE. I got a task to say which output this programm will have in the first picture. My first guess was, that it had to be an infinite loop. But the outcome is not infite, in fact it just works like the Label is not there? After trying out, it somehow works to be infinite if i put a writeln there, as you can see in the second picture. Funny thing is, the writeln doesnt seem to be in the console, rather just the infinite loop now?? Why does the infinite loop works, if i put a writeln there? Thanks in advance! ---------------------------------------------------------------------- Thaddy · 2022-01-27 16:57 Read up on modulo  What you need is a comparison to <= or equal or below the limt. Instead of = the limit. That may never be true and hence you enter an infinite loop. Exactness can bite..... ---------------------------------------------------------------------- wp · 2022-01-27 17:34 Your indentation suggests that several begin-end pairs are missing. First picture: The "for" loop executes "if ((i mod 3) = 1) or (i = 9) then writeln(...)". The next instruction is NOT part of the loop although you indented it (Pascal is not Python!). (You must use "begin"/"end" pairs to enclose multiple instructions inside the loop - see below). Therefore, your code executes only the WriteLn instructions. Then the loop is finished. It is not clear which value i has after the loop, maybe 15 or 16, certainly not 2. Therefore the condition "if i = 2" is false and the program does not jump back to L1, there is no infinite loop. If you want to have the "goto" inside the loop you must add a begin after the "do" of the "for" loop, and an "end" after the "goto". Like this: 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 procedure Versuche;label   L1;var  i: Integer;begin  L1:  for i := 1 to 15 do  begin    if ((i mod 3) = 1) or (i = 9) then       writeln('i was: ', i);    if i = 2 then      goto L1;  end;end;  In this case the program jumps out of the loop back to the label L1 each time when the counter i reaches the value 2. A very bad programming technique, by the way. Shame on your teacher for even showing you something like this! Second picture: Here again the program is behaving not as suggested by the indentation due to the missing begin/end pairs. Like before, it iterates through 15 i values in the "for" loop and prints out the "i was.." lines. After the loop we have the instruction "if i = 2 then goto L1" - but since i is not 2 after the loop the program does not jump the L1. But the next line has an unconditional goto which brings you back to the start and into the endless loop. I want to show you what your original code looks like with standard indentation: 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 procedure Versuche;Label   L1;var  i: Integer;beginL1:  for i := 1 to 15 do    if ((i mod 3) = 1) or (i = 9) then      writeln('i was: ', i);  if i = 2 then    writeLn('again');  goto L1;end; When I interpret your indentation correctly you probably mean the code to be like this: 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 procedure Versuche;Label  L1:var  i: Integer;beginL1:  for i := 1 to 15 do  begin    if ((i mod 3) = 1) or (i = 9) then      writeLn('i was: ', i);    if i = 2 then begin      Writeln('again');      goto L1;    end;  end;end; ---------------------------------------------------------------------- Zvoni · 2022-01-28 07:22 The first picture without Label and Goto (untested): 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 Project1;Var  i:Integer;Begin  i:=1;  Repeat    If ((i mod 3=1) or (i=9)) Then       Writeln('i was: ',i);    If i=2 Then i:=1 Else Inc(i);  Until i>15;end. ---------------------------------------------------------------------- OlivenHandcreme · 2022-01-28 19:22 First of all, thank you all for you very detailed help! This helped me really alot, and more that my professor could possibly do haha. And for wp, i wish i could give you an award you really took sometime to explain it to me. Intresting to see that someone is criticizing my professor just by seeing his code, makes me really ask myself if  i learn something right. One point to defend him, i guess his goal was to confuse us, and see if we understood whats really happening there. But then again, i dont know if this is a good way to do. Wish you all a nice weekend! ---------------------------------------------------------------------- Josh · 2022-01-28 21:06 Hi I think your tutor was trying to demonstrate to you the flaws of badly indented coding. As you can assume a program flow by how its laid out, badly indentented formatted code is a nightmare to follow and debug, I have spent many hours just re-formatting such code even before I attempt to debug it. If the code you have was reformatted the flow does become more obvious. Also adding in begin end blocks in the if then and for loops again helps the flow ie, 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 L1:  for i:= 1 to 15 do  begin    if ((i mod 3) = 1) or (i =9 ) then    begin    writeln('i was: ',i);    end;  end; // end of loop  if i = 2 then  begin    goto L1;  end; L1:  for i:= 1 to 15 do  begin    if ((i mod 3) = 1) or (i =9 ) then    begin      writeln('i was: ',i);    end;  end; // end of loop  if i = 2 then  begin    writeln('again');  end;  goto L1;     Note the value of a for loop variable cannot be guaranteed to be what you think it is after the loop has finished. ---------------------------------------------------------------------- MarkMLl · 2022-01-28 21:43 Quote from: OlivenHandcreme on January 28, 2022, 08:22:28 pmIntresting to see that someone is criticizing my professor just by seeing his code, makes me really ask myself if  i learn something right. One point to defend him, i guess his goal was to confuse us, and see if we understood whats really happening there. But then again, i dont know if this is a good way to do. If you think /you've/ got problems, or for that matter if you think /he's/ got problems, I suggest that the section on "Whitespace" in https://hackaday.com/2022/01/26/strange-computer-languages-a-hackers- field-guide/ might interest (or at least amuse). To put a slightly different spin on things, it's worth emphasising that in Pascal if...then controls a single adjacent statement where that statement can be a begin...end block. The subtlety there is that begin...end can itself be a container of multiple statements. MarkMLl