Jump to content

[PASCAL] For loop


CouldnoT
 Share

Recommended Posts

For loop

 

 

You can execute for statements for a specific number of cycles using a counter like this example:

 

var
 i: Integer;
 Count: Integer;
begin
 Write('How many times? ');
 Readln(Count);
 for i:= 1 to Count do
 Writeln('Hello there');
 Write('Press enter key to close');
 Readln;
end.

 

We should use ordinal types like Integer, Byte, and Char in for loop variables. We call this variable a loop variable or loop counter. The value of loop counter can be initialized with any number, and we can also determine the last value of loop counter. For example, if we need to count from 5 to 10, then we can do this:

 

  for i:= 5 to 10 do

 

We can display loop counter in every cycle of the loop, as in the modified example below:

 

var
 i: Integer;
 Count: Integer;
begin
 Write('How many times? ');
 Readln(Count);
 for i:= 1 to Count do
 begin
 Writeln('Cycle number: ', i);
 Writeln('Hello there');
 end;
 Write('Press enter key to close');
 Readln;
end. 

 

Note: that this time we need to repeat two statements, and for that reason we have used the begin . . end keywords to make them one statement.

Edited by CouldnoT
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.