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 C language

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 C
while (condition)
{
	sequence-of-commands
}

Simple Example

WHILE sample in C
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int cislo = 1;
	while (cislo <= 5)
	{
		printf("%d", cislo);
		cislo = cislo + 1;
	}
	fflush(stdin);
	getchar();
	return 0;
}

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
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	char odpoved;
	int a, b;
	odpoved = 'a';
	while (odpoved == 'a')
	{
		printf("Zadejte cislo a: ");
		scanf("%d", &a);
		printf("Zadejte cislo b: ");
		scanf("%d", &b);
		printf("Soucet zadanych cisel je %d!\n",(a+b));
		printf("Prejete si secist dalsi dve cisla? (a/n) ");
		scanf("%c",&odpoved);
	}
	fflush(stdin);
	getchar();
	return 0;
}

This program repeats reading two numbers from console until the user replies 'a' (+enter) to the last question. See the first line (below the header of main function). 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
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	char odpoved;
	int a, b, pocet;
	odpoved = 'a';
	pocet = 0;
	while (odpoved == 'a')
	{
		printf("Zadejte cislo a: ");
		scanf("%d", &a);
		printf("Zadejte cislo b: ");
		scanf("%d", &b);
		printf("Soucet zadanych cisel je %d!\n",(a+b));
		printf("Prejete si secist dalsi dve cisla? (a/n) ");
		scanf("%c",&odpoved);
		pocet++;
	}
	printf("Pocet realizovanych prikladu: %d\n",pocet);
	fflush(stdin);
	getchar();
	return 0;
}

You can notice one more interesting syntax, the line pocet++. This line is a shortcut for pocet = pocet + 1 (you can shorten the syntax pocet = pocet - 1 to pocet-- similarly).

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
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	char odpoved = 'a';
	int soucet = 0;
	while (odpoved == 'a')
	{
		printf("Zadejte cislo: ");
		scanf("%d", &cislo);
		soucet = soucet + cislo;
		printf("Soucet zadanych cisel je %d!\n",(a+b));
		printf("Prejete si nacist dalsi cislo? (a/n) ");
		scanf("%c",&odpoved);
	}
	printf("Soucet vsech zadanych cisel je %d.\n", soucet);
	fflush(stdin);
	getchar();
	return 0;
}

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
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	char odpoved = 'a';
	int soucet = 0, pocet = 0;
	while (odpoved == 'a')
	{
		printf("Zadejte %d. cislo: ", pocet+1);
		scanf("%d", &cislo);
		soucet = soucet + cislo;
		pocet++;
		printf("Soucet zadanych cisel je %d!\n",(a+b));
		printf("Prejete si nacist dalsi cislo? (a/n) ");
		scanf("%c",&odpoved);
	}
	printf("Soucet vsech zadanych cisel je %d.\n", soucet);
	printf("Pocet zadanych cisel je %d.\n",pocet);
	fflush(stdin);
	getchar();
	return 0;
}

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áš