The control structure for

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Control structures

    /

  5. The control structure for

The loop structure for

The control structure for is used to repeat a section of code a specific amount of times. It is a bit more complex than the other loop structures (while and do while). Here is an example of a structure for that repeats, 9 times, the instructions inside the braces {} following it:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream> int main() { for(int c = 0; c < 9; c++) { std::cout << "Hello" << std::endl; } return 0; }

There are 3 sections inside the parentheses of a for structure. In the first part (int c = 0;), we define what is called the counter (That variable is used to keep track of the number of iterations the loop has gone through). In the second part (c < 9;), we put the condition that must be true in order for the loop to continue. In the last part (c++), we put the instruction that will be executed at the end of each loop iteration.

The example above could be translated, using a while structure, into the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> int main() { { int c = 0; while(c < 9) { std::cout << "Hello" << std::endl; c++; } } return 0; }

Later, but very soon, you will understand why, in the example above, there are braces {} around the loop while.

Note that the loop structure for is in general used more often than the structure while. An example, where the decimal digits ([0, 9]) are written to the console:

1
2
3
4
5
6
7
8
9
#include <iostream> int main() { for(int c(0); c < 10; c++) std::cout << c << std::endl; return 0; }

Note that a variable defined inside the parentheses of a for structure (The counter) can not be used outside of it. The reason is that variables have life spans and they exist only within certain scopes. We will learn about that very soon.

For now, here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream> int main() { for(int c(0); c < 6; c++) std::cout << c*c << std::endl; // Error: No variable named c exist here. c = 0; return 0; }

Also note that it is not an obligation to define a counter inside the parentheses of a for loop structure. We can, instead, write a semicolon with nothing before it:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> int main() { int var = 2; for(; var < 8; var++) std::cout << var*var << std::endl; // Here, var exists because it is not defined inside the parentheses of the loop structure 'for'. std::cout << std::endl << var * var << std::endl; return 0; }

It is possible, in the third section of the loop for, to write many instructions to be executed at the end of the iterations, but they must be separated by commas. It is also possible, in the first section, to define many variables, but they must be of the same type.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream> int main() { for(int c=0, var=1; c < 4; c++, var *= 2) { std::cout << c << " " << var << std::endl; } return 0; }

The break instruction

The instruction break may be used to get out of any kind of loop structure. When that instruction is reached, the program gets out of the current loop structure and continues its execution after it. It is coded by simply writing the keyword break followed by a semicolon ;.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> int main() { int n = 0; while(true) { std::cout << n << std::endl; n++; if(n >= 5) break; } /* The integers from 0 to 4 are written to the console. */ return 0; }

An other example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream> int main() { for(int c = 0; true; c++) { std::cout << c << std::endl; if(c >= 5) break; } /* The integers from 0 to 5 are written to the console. */ return 0; }