Content of the lesson:
You often meet cycles in the real life - repeating actions or processes, for example:
In general: until the condition is valid you have to repeat the action. You can define any finite number of steps inside a cycle and you can write as much complicated condition as you want. In case a bad condition is used, the while cycle will run forever.
There is the WHILE cycle in Pascal (as in the most of other programming languages). The syntax should look like the following sample:
WHILE (condition) DO commands
Program WriteFiveNumbers;
var cislo:integer;
begin
cislo := 1;
WHILE (cislo<=5) DO
begin
writeln(cislo);
cislo := cislo + 1;
end;
readln;
end.
This program writes 5 numbers from zero to five (you will see numbers 1,2,3,4,5, each on a new line) and it works using these steps:
Using a cycle you can repeat any action so you can repeat the whole program. Consider a situation when user is asked in the end whether he wants to run the program once again. He presses 'a' to run the program again or anything else to terminate it. To be able to get a letter from user you have to use the char data type. This data type can store exactly one character from the keyboard. Take a look at the following sample which computes the sum of inserted numbers:
Program RepeatedSumOfTwoNumbers;
var odpoved:char;
a,b:integer;
begin
odpoved:='a';
while (odpoved='a') do
begin
writeln('Enter number a:');
readln(a);
writeln('Enter number b:');
readln(b);
writeln('The sum is ',(a+b));
writeln('Do you want to enter new numbers (a/n)?');
readln(odpoved);
end;
readln;
end.
This program repeats reading two numbers from console until the user replies 'a' (+enter) to the last question. See the first line (before begin command). The value 'a' is assigned to the odpoved variable. Why?
The reason is that the WHILE cycle checks given condition and executes the inner commands in case that odpoved stores value 'a'. Without the first line, the variable odpoved would not contain the value 'a' and WHILE cycle would not be started.
You might want to know the final number of realized sums so define a new variable called pocet to store the number of passes. The principle is that you assign it with a zero value in the beginning and increase it by one in every pass of the while cycle.
Program RepeatedSumOfTwoNumbers;
var odpoved:char;
a,b,pocet:integer;
begin
odpoved:='a';
pocet:=0;
while (odpoved='a') do
begin
writeln('Enter number a:');
readln(a);
writeln('Enter number b:');
readln(b);
writeln('The sum is ',(a+b));
writeln('Do you want to enter new numbers (a/n)?');
readln(odpoved);
pocet:=pocet+1;
end;
writeln('The number of realized sums: ',pocet);
readln;
end.
Until now we were able to write programs which can handle only a limited number of inserted numbers. Now we are going to take a look at a program to compute the sum of all inserted numbers (similarly to a calculator).
Program SumOfAllInsertedNumbers;
var odpoved:char;
soucet:integer;
begin
odpoved:='a';
soucet:=0;
while (odpoved='a') do
begin
writeln('Enter number:');
readln(cislo);
soucet:=soucet+cislo;
writeln('Do you want to insert another number (a/n)?');
readln(odpoved);
end;
writeln('The sum of all inserted numbers is ',soucet);
readln;
end.
You can improve the program to be able to write the number of inserted numbers and to write the order of current number ("Enter number 1, ... Enter number 2" etc.). To get this result you have to define a new variable and call it pocet.
Program SumOfAllInsertedNumbers;
var odpoved:char;
cislo,pocet,soucet:integer;
begin
odpoved:='a';
pocet:=0;
soucet:=0;
while (odpoved='a') do
begin
writeln('Enter number ',(pocet+1),':');
readln(cislo);
soucet:=soucet+cislo;
writeln('Do you want to insert another number (a/n)?');
readln(odpoved);
pocet:=pocet+1;
end;
writeln('The sum of all inserted numbers is ',soucet);
writeln('You have inserted ',pocet,' numbers');
readln;
end.
Write a program to compute the arithmetic mean of all inserted numbers.
Write a program to find minimum and maximum values from all inserted numbers.