### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Infinite Loop with Label  (Read 2129 times)

#### OlivenHandcreme

• Newbie
• Posts: 2
##### Infinite Loop with Label
« on: January 27, 2022, 05:35:58 pm »
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?

• Hero Member
• Posts: 11518
##### Re: Infinite Loop with Label
« Reply #1 on: January 27, 2022, 05:57:31 pm »
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

• Hero Member
• Posts: 9594
##### Re: Infinite Loop with Label
« Reply #2 on: January 27, 2022, 06:34:42 pm »
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][+][-]
1. procedure Versuche;
2. label
3.   L1;
4. var
5.   i: Integer;
6. begin
7.   L1:
8.   for i := 1 to 15 do
9.   begin
10.     if ((i mod 3) = 1) or (i = 9) then
11.       writeln('i was: ', i);
12.     if i = 2 then
13.       goto L1;
14.   end;
15. end;
16.

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][+][-]
1. procedure Versuche;
2. Label
3.   L1;
4. var
5.   i: Integer;
6. begin
7. L1:
8.   for i := 1 to 15 do
9.     if ((i mod 3) = 1) or (i = 9) then
10.       writeln('i was: ', i);
11.   if i = 2 then
12.     writeLn('again');
13.   goto L1;
14. end;

When I interpret your indentation correctly you probably mean the code to be like this:
Code: Pascal  [Select][+][-]
1. procedure Versuche;
2. Label
3.   L1:
4. var
5.   i: Integer;
6. begin
7. L1:
8.   for i := 1 to 15 do
9.   begin
10.     if ((i mod 3) = 1) or (i = 9) then
11.       writeLn('i was: ', i);
12.     if i = 2 then begin
13.       Writeln('again');
14.       goto L1;
15.     end;
16.   end;
17. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### Zvoni

• Hero Member
• Posts: 1185
##### Re: Infinite Loop with Label
« Reply #3 on: January 28, 2022, 08:22:37 am »
The first picture without Label and Goto (untested):
Code: Pascal  [Select][+][-]
1. program Project1;
2. Var
3.   i:Integer;
4. Begin
5.   i:=1;
6.   Repeat
7.     If ((i mod 3=1) or (i=9)) Then
8.        Writeln('i was: ',i);
9.     If i=2 Then i:=1 Else Inc(i);
10.   Until i>15;
11. end.
12.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

#### OlivenHandcreme

• Newbie
• Posts: 2
##### Re: Infinite Loop with Label
« Reply #4 on: January 28, 2022, 08:22:28 pm »
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!

« Last Edit: January 28, 2022, 08:26:32 pm by OlivenHandcreme »

#### Josh

• Hero Member
• Posts: 931
##### Re: Infinite Loop with Label
« Reply #5 on: January 28, 2022, 10:06:55 pm »
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][+][-]
1. L1:
2.   for i:= 1 to 15 do
3.   begin
4.     if ((i mod 3) = 1) or (i =9 ) then
5.     begin
6.       writeln('i was: ',i);
7.     end;
8.   end; // end of loop
9.   if i = 2 then
10.   begin
11.     goto L1;
12.   end;
13.
14. L1:
15.   for i:= 1 to 15 do
16.   begin
17.     if ((i mod 3) = 1) or (i =9 ) then
18.     begin
19.       writeln('i was: ',i);
20.     end;
21.   end; // end of loop
22.   if i = 2 then
23.   begin
24.     writeln('again');
25.   end;
26.   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.
« Last Edit: January 28, 2022, 10:09:33 pm by josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX..
Any posts made from 2015 will be based on Lazarus Trunk.

#### MarkMLl

• Hero Member
• Posts: 4187
##### Re: Infinite Loop with Label
« Reply #6 on: January 28, 2022, 10:43:13 pm »
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.

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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories