Course Introduction to Programming

Command: while

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

In this lesson, we will learn about one of the most important structures in programming: the While loop.

You will understand how it works, in which situations it should be used, see practical examples, and comprehend how it allows a piece of code to be executed repeatedly, automatically, as long as a condition is true. By the end, you will be able to create programs that repeat actions in a controlled manner, making your code more dynamic, intelligent, and efficient.

Problem Example

Write a program that prints the first 20 positive numbers. Print one number per line. We can solve this problem in two ways: manually, by printing the numbers, 1, 2, 3, 4, ..., 20, or by using a repetition structure to automate the printing.

The While Structure

The while structure is a repetition structure (also called a loop), that is, a structure we use to repeat a certain piece of code.

The keyword while indicates that this structure will repeat a piece of code as long as a certain condition is true. Below is the syntax of the structure.

while(condition){
	// Code
}

When a while is encountered, the code will test the condition. If it is false, the while loop is not executed, but if it is true, the code between the curly braces will be executed, and then the condition is checked again. Thus, as long as the condition is true, the code will follow this loop of executing the code and testing the condition, and the moment it becomes false, the while loop stops executing.

Similarity between while and if

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

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

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

Now we will replace the if with a while and check the code's behavior.

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

By replacing the if with a while, the code will have a peculiar behavior, printing multiple 1s without stopping. This happens because of how while works: as long as the condition is true, what is between the curly braces will be executed, and since the value of in the code does not change, 1 is always less than 5, so the while loop never stops, printing 1 infinitely.

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

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

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

In the code above, the value of will start at 1 and will be gradually incremented by one unit, until the point where, upon checking, the value of will be 5, which is not less than 5, and the while structure stops repeating the code. When we execute our algorithm, we see that it prints the numbers from 1 to 4, because when the value of is 5, it no longer executes the code inside the curly braces.

To solve the initial problem, printing the first 20 positive numbers, we just need to alter the code a bit, changing the while condition.

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

We see then that while is very useful for repeating a piece of code a certain number of times, using a variable to serve as a counter, gradually increasing its value.

Conclusion

Throughout this lesson, you have learned what the while repetition structure is and how to use it, understood its syntax, its operation, and the necessary precautions to avoid infinite loops. You also saw practical examples and learned to apply the while loop in situations where you need to repeat commands in a controlled manner.

Now you know how to use the while repetition structure to create dynamic, efficient, and intelligent programs ๐Ÿ˜ƒ