Forum > Beginners

Need clarification for this simple double loop progam.

(1/3) > >>

MouseTech:
I am currently learning how to use loops and have recently made a program from my Pascal Learning Book that shows you how a double loop works. At first I was confused on how the program was executing the loop so I decided to make a diagram showing what I think is going on when the program is ran.

I would like to know if my presumptions are correct on how the program is handling its loops. Please correct me if my diagram is wrong and please tell me how the program is executing the loops in the right sequence. Thank you for your time.

lucamar:
Yes, that's how it works :)

MarkMLl:
Yes, but you'd make life much easier for yourself if you indented your code:


--- 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 dblloop; var  loop: integer;  inner: integer; begin  for loop := 1 to 10 do    begin      for inner := 1 to 10 do        write('*');      writeln;    end;  readln;end. 
Note that there isn't a "correct" indentation style. The important thing is to be consistent, i.e. to stick to the same layout in a program and to try to conform to the same convention as anybody else you're working with.

MarkMLl

dseligo:

--- Quote from: MarkMLl on April 30, 2021, 10:30:13 am ---Yes, but you'd make life much easier for yourself if you indented your code:


--- 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 dblloop; var  loop: integer;  inner: integer; begin  for loop := 1 to 10 do    begin      for inner := 1 to 10 do        write('*');      writeln;    end;  readln;end. 
Note that there isn't a "correct" indentation style. The important thing is to be consistent, i.e. to stick to the same layout in a program and to try to conform to the same convention as anybody else you're working with.

MarkMLl

--- End quote ---

WriteLn in line 12 is not part of the inner loop, so it should be more left.

mika:

--- Quote from: MouseTech on April 30, 2021, 04:06:27 am ---I would like to know if my presumptions are correct

--- End quote ---
Yes, but no!
1.
Green rectangle stands for outer loop
Red rectangle stands for body of outer loop
No color addresses inner loop
2.
Your explanation is ambiguous. You have to make it clear that writeln; is part of outer loop.

Navigation

[0] Message Index

[#] Next page

Go to full version