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)
{
sequence-of-commands
}
#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:
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:
#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.
#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).
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).
#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.
#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;
}
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.