course content

Course Content

Introduction to C++

For LoopFor Loop

The for loop is more complex than the other loops and consists of three parts:

  1. Counter;
  2. Exit condition;
  3. Loop expression.

Structure of for loop:

For example:

cpp

main.cpp

  • int counter = 0iteration counter;
  • counter++ is the same as counter = counter + 1;
    For each iteration, 1 will be added to the counter variable to mark the passage of the loop;
  • counter <= 5 – loop termination condition. The loop will continue if the counter variable is less than or equal to 5.

Loops are convenient to use with arrays:

cpp

main.cpp

In this case, we used one loop to initialize all 10 array elements and a second loop to display the array elements.

question-icon

How many iterations will this loop make?

Select the correct answer

Section 4.

Chapter 6