Content of the lesson:
The FOR cycle can be (as well as the WHILE cycle) used to repeat a set of commands. This cycle is used very often so we have to deal with it. The FOR cycle is being used in situations when we know the number how many times should a command be done.
The syntax of the FOR cycle is:
FOR variable := lower_limit TO upper_limit DO commands
The FOR cycle is being executed using the following steps:
Program NapisPetCisel;
var cislo:integer;
begin
FOR cislo:=1 TO 5 DO
begin
writeln(cislo);
end;
readln;
end.
This programs writes five numbers from one to five (you will see 1, 2, 3, 4, 5 each on a new line) and it follows these steps:
Consider a simple program to compute the sum of five numbers. The program assigns the variable soucet to zero and then the FOR cycle is repeated five times. In every pass it requires an input number from user and adds it to the value of the variable sum. After the cycle ends, the variable sum stores the final sum of five inserted numbers.
Program SoucetPetiCisel;
var cislo, i, soucet:integer;
begin
soucet:=0;
FOR i:=1 TO 5 DO
begin
writeln('Enter number:');
readln(cislo);
soucet:=soucet + cislo;
end;
writeln('The sum of inserted numbers is: ',soucet);
readln;
end.
We can improve the program to inform an user about the number of the current pass ("Enter number 1, ... Enter number 2" etc.) We can use the i variable (from the for cycle) to solve this problem. You can use this variable anytime during the execution of the cycle.
writeln('Enter number ',i,':');
Program SoucetPetiCisel;
var cislo, i, soucet:integer;
begin
FOR i:=1 TO 5 DO
begin
writeln(Enter number ',i,':'');
readln(cislo);
end;
writeln('The sum of inserted numbers is: ',soucet);
readln;
end.
Think about the properties of this program. It really computes the sum of five numbers, but this is the only good thing it does. A huge disadvantage is that this program can handle exactly five numbers (not more or less) so compared to the variation using the WHILE cycle, it is much worse. This property of the FOR cycle was fully illustrated here - you have to know the number of passes to be able to use the FOR cycle. Is it possible to improve the program to be at least able to set the number of values to insert?
You can define a new variable and set let the user set its value after the start. Then you can use it inside the condition of the FOR cycle.
Program SoucetZadanehoPoctuCisel;
var cislo, i, pocetcisel, soucet:integer;
begin
writeln('Enter the number of all values:');
readln(pocetcisel);
FOR i:=1 TO pocetcisel DO
begin
writeln('Enter number ',i,':');
readln(cislo);
end;
writeln('The sum of inserted numbers is: ',soucet);
readln;
end.
Using cycles you are able to write a program which can handle a previously known number of values. However, we might want to write a program which would be able to handle any number of values (we would not know the number before launching our program) - you know that this is possible using the while cycle.
It is also possible to use the for cycle but you would have to set a high value in the condition to allow the user to set really "any" number of values. Then you would have to break the cycle (using the break command inside it) which could be a solution but it is useless.
When can the for cycle be used? In case you really know the exact number of passes which should be executed. You will use the for cycle when handling and sorting arrays. This cycle is explained because it is used very often but it does not have such possibilities as the while cycle. It is also true that you can write any algorithm using the while cycle and if...then conditions only.
Every for cycle can be re-written using a while cycle.
FOR variable := lower_limit TO upper_limit DO commands
WHILE (lower_limit<=upper_limit) DO
begin
commands;
lower_limit := lower_limit + 1;
end;
In the end you should know that it is possible to reverse any for cycle in Pascal.
FOR variable := upper_limit DOWNTO lower_limit DO
commands
Individual Task: Create a program to compute the factorial from any inserted natural number. Use the FOR cycle.
You can find the definition of a factorial in the Internet so try to explain its meaning at first. Then you can write the program.
Program Faktorial;
var cislo, faktorial, nasob:integer;
begin
writeln('Enter a number:');
readln(cislo);
faktorial := 1;
for nasob := 1 to cislo do
faktorial:=faktorial * nasob;
writeln('Factorial is ',faktorial);
end;
Try to examine if your program works properly and in case you find any problem, try to fix it.
Create a program which outputs to screen every multiple of 7 from the interval of 1..20. The result should be a written sequence of numbers (7, 14, 21, 29, ... ,140). Use the FOR cycle.
Create a program to find the maximum from any number of inserted values. Use the FOR cycle.
Create a program which outputs the small multiplication table - multiples of numbers 1..10 formatted in a well-arranged table. Allow the user to set the maximum number n for the table. Use the FOR cycle.