Course Introduction to Programming

Command: for

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

In this lesson, we will learn about one of the fundamental concepts of programming: the For.

We will understand what it is, what its main characteristics are, see practical examples of its use in day-to-day programming, and finally, understand how this structure allows us to repeat instructions automatically, compactly, and in an organized way, making our programs more efficient and easier to understand.

Problem

Write a program that reads an integer N and prints the message "Neps" N times. To solve this problem, we have two options. We can use the While structure, learned in the previous lesson, just as we can also use the For structure.

The For Structure

The for structure is similar to the while loop, as it is a repetition structure that repeats a piece of code as long as a certain condition is true. However, we can include a command to be executed before the first check and a command that will be executed at the end of each repetition.

Syntax

for(initialization; condition; increment){
	//Code
}

Below is an image to better explain how the for loop works.

Figure 1

When the code reaches the for loop, the initialization is executed. Then, while the condition is true, the code and then the increment are executed. The moment the condition becomes false, the for loop ends.

Coding

If we were to solve the problem using the while structure, the final code would be as follows.

#include<stdio.h>

int main(){
	int n;
	int i;
	scanf("%d", &n);
	
	i = 1; // initialization
	while(i<=n){ //condition
		printf("Neps\n");
		i = i+1; // increment
	}
}

If is 1, i will start at 1, it will test the condition, which is true because 1 is less than or equal to 1, execute the printf, and increment by 1, making it 2. In the second test, the condition will be false, because 2 is not less than or equal to 1, so the execution will stop. Thus, as a result of the program, the line "Neps" will be printed only once. If is 2, two lines will be printed, and so on.

In the while structure, we can only place the condition to check when the structure will stop. However, in the for loop, we can use two other parameters: the initialization, executed at the beginning of the structure, and the increment, which is executed at each repetition.

Therefore, when coding with the for loop, we can initialize the variable we use to count the repetitions inside the structure, perform the check, and increment the variable in a very compact way. The solution with the for structure is as follows:

#include<stdio.h>

int main(){
	int n;
	int i;
	scanf("%d", &n);
	
	for(i = 1; i<=n; i = i+1){
		printf("Neps\n");
	}
}

Upon encountering the for loop, the initialization will be executed, and the value of the variable will be set to 1. The condition will then be tested. If it is false, the for loop ends, but if it is not false, the commands will be executed. After the commands, the variable is incremented, and the condition is tested again.

The result of the algorithm is the same as the previous one, but done in a more concise way.

It is also common to find some shorthand ways to increment variables, such as i++ or i--, which, respectively, increment the variable by 1 or decrement the variable by 1.

When to use for and while

The for and while repetition structures are used to repeat sections of code, but each is better suited to different situations.
We use for when we know in advance how many times the code should be executed, as in cases where there is a defined number of repetitions โ€” for example, printing the numbers from 1 to 20 or executing an action exactly 10 times.
while, on the other hand, is more suitable when we do not know the exact number of repetitions, as its execution depends on a logical condition that can vary according to the program's behavior or user input โ€” such as reading data until the user types a specific value.

In summary, for is ideal for controlled and predictable repetitions, while while is used in dynamic situations, where the end of the loop depends on an external condition.

Conclusion

Throughout this lesson, you learned what the for repetition structure is and how it works, got to know its main components โ€” initialization, condition, and increment โ€” and saw how it can make code cleaner and more compact. You also learned the difference between for and while, and saw practical examples of use for each.

Now you know how to code and use the For structure to solve problems that require repetitions in an organized and efficient way ๐Ÿ˜ƒ