For statement: repetition structure

Advance your knowledge in repetition structures. Learn how the "for statement", the "continue" statement and the "break" statement work.

11/09/2023

youtube-thumb
algol-youtube-play

Before you start to learn about the for command, it is essential that you already know what repetition structures are. If you still don’t know, read our previous article on the while statement and learn in detail all the mechanics of functioning of a repetition structure.

The knowledge of the while statement will give you the basis to understand the meaning of the topic covered in this article.

But let’s get down to business!


What is the for statement?

The for statement can be understood as an evolution of the while statement. It is especially suitable for creating structures where the exact number of repetitions is already known in advance.

The basic structure of the for command is composed of 3 elements:

  • initialization.
  • conditional expression.
  • iteration.

See in the code below the generic structure (only illustrative) of the for command and its 3 elements:

1.for(initialization; conditional expression; iteration){
2. // repeated commands
3.}

Note that all 3 elements are enclosed in parentheses ‘(‘ ‘)’ of the command and are separated by semicolons ‘;’.

We will understand each of these 3 elements by making a comparison. Let’s compare them to a while repetition structure.

Returning to the code of the previous article, responsible for printing the values from 1 to 10 on the screen.

1.int count=1;
2.while(count <= 10){
3. println(count);
4. count++;
5.}

In line 1 (code above), initialization of the variable “count” is performed, the control variable of the repetition structure. In line 2 (code above) there is the conditional expression, which will impose a limit for repetitions. In line 4 (code above) there is the iteration, which changes the control variable “count” during repetitions.

Now let’s transform this code from the while statement into a for statement:

1.for(int count=1; count <= 10; count++){
2. println(count);
3.}

Did you notice? The for statement is nothing more than a while statement that already incorporates within its structure, in addition to the conditional expression, the initialization and iteration of the control variable.

There’s no secret! Most repetition structures require a control variable and an iteration. The difference between while and for is that the for statement already provides native support for creating these elements within its own structure.

See the animation below for a simulation of the execution of the algorithm above.


“Continue” command

Continue is an auxiliary command that plays a very important role within repetition structures. Your role is to stop the repetition in advance. It forces the structure to skip to the next repetition before its scope ends.

To understand its working mechanics, let’s see a repetition structure with the for command:

1.for(int i=1; i<=10; i++){
2. println(i);
3.}

From what you’ve already learned about the for command, you can see that the code above displays a structure that prints the numbers from 1 to 10 on the screen:

1 2 3 4 5 6 7 8 9 10

Now let’s add the continue command to this code:

1.for(int i=1; i<=10; i++){
2. if(i==7){
3. continue;
4. }
5. println(i);
6.}

Notice in lines 2, 3 and 4 (code above) that we add a conditional statement that executes the continue command if the control variable “i” is equal to the value 7. But what does that mean?

If the continue command (line 3 of the code above) is executed, the repetition is interrupted at that moment and passed to the next repetition, not executing line 5 (code above). As a result, numbers 1 to 10 are printed on the screen except the number 7.

1 2 3 4 5 6 8 9 10

That is, the continue command provides a mechanism to stop the repetition in advance.

See this complete code below in several different programming languages. Note that the Python language has a very different structure for the for statement.

Java
C
C++
C#
Python
1.class Main {
2. public static void main(String[] args) {
3. for(int i=1; i<=10; i++){
4. if(i==7){
5. continue;
6. }
7. System.out.println(i);
8. }
9. }
10.}
1.#include <stdio.h>
2.int main(void) {
3. for(int i=1; i<=10; i++){
4. if(i==7){
5. continue;
6. }
7. printf("%d\n",i);
8. }
9.}
1.#include <iostream>
2.int main() {
3. for(int i=1; i<=10; i++){
4. if(i==7){
5. continue;
6. }
7. std::cout << i << std::endl;
8. }
9.}
1.using System;
2.class MainClass {
3. public static void Main (string[] args) {
4. for(int i=1; i<=10; i++){
5. if(i==7){
6. continue;
7. }
8. Console.WriteLine(i);
9. }
10. }
11.}
1.for i in range(1, 11):
2.if i==7:
3. continue
4.print(i)

“Break” command

As well as the continue command, the break command also stops the repetition in advance. However, it interrupts the entire structure.

Instead of skipping to the next repetition, he simply abandons the entire repetition structure. Let’s go back to the previous code example by placing the break command in place of continue:

1.for(int i=1; i<=10; i++){
2. if(i==7){
3. break;
4. }
5. println(i);
6.}

If the break command (line 3 of the code above) is executed, the entire repetition structure is interrupted at that moment, no longer executing line 5 (code above) as well as no next repetition. As a result, only the numbers 1 to 6 are printed on the screen.

1 2 3 4 5 6

That is, the break command provides a mechanism to interrupt the entire repetition structure in advance.


Take the next step. Read our article on the switch case command, the selection structure of computer programming.

autor

David Santiago

Master in Systems and Computing. Graduated in Information Systems. Professor of Programming Language, Algorithms, Data Structures and Development of Digital Games.

Other articles