Czech version
logolink

< Back to the list of lessons

While Cycle

AlgortimyContent of the lesson:

  • What Is a Cycle
  • WHILE Cycle Syntax in Pascal
  • Simple Example
  • Repeating Program as Required by User
  • Processing any Number of Input Numbers

What is a Cycle

You often meet cycles in the real life - repeating actions or processes, for example:

  • Walking up any stairs is a cycle. It can be described as a cycle when you have to step on every following stair before reaching the end. Using other words: "Until you are on the top, step on the following stair." It is a cycle of repeating actions (moving one stair up) which is done for a particular time.
  • I want to buy 5 rolls. I have to put them into a basket gradually which is a cycle. The action is putting one roll inside a basket and this action is repeated 5 times. In the end I have 5 rolls inside my basket and the cycle ends.

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.

WHILE Cycle Syntax in Pascal

There is the WHILE cycle in Pascal (as in the most of other programming languages). The syntax should look like the following sample:

Basic WHILE cycle syntax in Pascal
WHILE (condition) DO commands

Simple Example

WHILE sample in Pascal
 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:

  1. Assign the value 1 into the cislo variable.
  2. UNTIL the number is lower than or equal to 5 DO the following two instructions
  3.   Write the value of the cislo variable.
  4.   Increment the value of the cislo variable by one.
  • The number 1 is written in the first pass of the cycle (the value of cislo) and the value of cislo is increased by one.
  • After those two commands inside the cycle the condition cislo<=5 is checked. It is true because the variable stores value 2 so the cycle is launched again.
  • In the second pass the cislo variable is written again (it contains the value 2) and its value is increased by one again.
  • The previous step is repeated until the value of cislo is lower than or equal to 5. Then the cycle ends and program continues executing the readln command (it waits until enter is pressed) and then it is terminated.

Repeating Program as Required by User

Repeated Sum of Two Numbers

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:

Repeated Sum of Two 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.

Repeated Sum of Two Numbers
 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.

Sum of All Inserted Numbers

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).

Sum of All Inserted Numbers
 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.

Sum of All Inserted Numbers
 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.

Individual Task I

Write a program to compute the arithmetic mean of all inserted numbers.

Individual Task II

Write a program to find minimum and maximum values from all inserted numbers.

Questions and Tasks

  1. What is the proper syntax of the while cycle?
  2. Name 3 examples of using the while cycle.
  3. Try to find a way to break the while cycle or to skip the current pass and continue with the next one.
webdesign, xhtml, css, php - Mgr. Michal Mikláš