Definition of While

(While). One of the repetition structures used in programming algorithms. While allows the programmer to specify how many times an action (one or more programming statements) is repeated while a condition remains true. The form of the while in C language is:

while (condition)
{
instruction block
};

As long as the condition remains true, the block of statements within the curly braces will be executed «x» number of times. It is necessary that at some point the condition becomes false, otherwise an infinite cycle of repetitions (infinite loop) would be entered and the program would be considered blocked. Therefore, it is necessary that in the statement block within the while structure, some action is executed that at some point makes the condition false.

An example of the operation of the while repetition structure:

int num;

num = 0;
while (num {
printf(Repetition number %d
,num);
num = num + 1;
};

The above code will print to screen:

repetition number 0
repetition number 1
repetition number 2
repetition number 3
repetition number 4
repetition number 5
repetition number 6
repetition number 7
repetition number 8
repetition number 9
repetition number 10

why? We see that we initialize the integer variable num to zero. Then, it is evaluated for the first time if it is less than or equal to 10, being true, the block inside the while is executed for the first time. Repetition number 0 is printed, since the value inside num is zero. Then the process is repeated until num with value 10 is added to 1, and takes the value 11. The condition of the while is evaluated and it is determined that it is NOT fulfilled, therefore, the block is skipped and the execution of the program continues.

Doubts? needs more information? Write and we will respond to your email: click here