Course Introduction to Programming

Command: while

Course's Page 14 min Text by
User Image
Vitor Hugo Couto

In this lesson, you will learn your first repetition structure, while, learning topics like:

  1. Example of a problem;
  2. While structure;
  3. Coding.

Example of a Problem

Create a program that prints the first 20 positive numbers. Print a number on each line.

We can solve this exercise in two ways: manually printing the numbers, 1, 2, 3, 4, ..., 20, or use a repetition structure to automate printing.

While Structure

The while structure is a repetition structure, that is, a structure we can use to repeat a certain piece of code.

The while structure will repeat a code snippet while a certain condition is true.

See the syntax of the structure below.

while(condition){
	// Code
}

When the code finds a while, the condition will be tested. If it is false, the while is not executed, but if it is true, the code between the braces will be executed and then the condition is checked again.

While the condition is true, the code will follow this loop, executing the code and testing the condition, and at the moment it is false the while stops being executed.

We will now see some similarities between the if selection structure, which we already know, and the while structure we are learning now.

int main(){
	int i = 1;
	if(i < 5) {
		printf("%d\n", i);
	}
}

The above code works as follows: the variable is initialized with the value 1, and then the if will test if the value of is less than 5. If the condition is true, the code between the braces will be executed, which in this case will only print the value of . Since the value of is 1, when we execute the code, the code will print 1.

We will now change the if to a while and verify the behaviour of the code.

int main(){
	int i = 1;
	while(i < 5) {
		printf("%d\n", i);
	}
}

Exchanging the if for a while, the code will have a peculiar behaviour, printing several 1's without stopping. This happens because the while works in a way that, while the condition is true, what is between the braces will be executed, and since the value of in the code doesn't ever change, 1 is always smaller than 5, so the while never stops, printing the 1 nonstop.

It is important to note then that when writing a while you need a condition that at some point will be false, otherwise the program will repeat what is between the braces infinitely, generating what we call an infinite loop.

To make the code work, we then need to make the condition be false at some point. So if inside the braces, at each repetition, we increase the value of , at some point the condition will be false.

int main(){
	int i = 1;
	while(i<5){
		printf("%d\n", i);
		i = i+1;
	}
}

In the above code, the value of will start at 1, and will be incremented by one unit gradually, to the point where the verification the value of will be equal to 5, which is not less than 5, so the while structure stops repeating the code.

When executing our algorithm, we can see it prints the numbers from 1 to 4, because when the value of is 5 it no longer executes the code inside the braces.

To solve the initial problem, which is to print the first 20 positive numbers, we only need to modify the code a little, changing the condition of the while.

int main(){
	int i = 1;
	while(i <= 20){
		printf("%d\n", i);
		i = i+1;
	}
}

We can notice then that the while structure is very useful to repeat a piece of code a certain number of times, using a variable to serve as a counter, increasing its value gradually.

Now you already know how to use the while repetition structure to repeat code snippets a certain number of times.