Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn For Loop | Introduction to Program Flow
C++ Introduction

bookFor Loop

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

loop_statement.cpp

loop_statement.cpp

copy
1234
for (counter; condition; expression) { // Block of instruction }
Counter
expand arrow

Initializes the loop variable. It usually sets a starting point, such as int i = 0, which determines where the loop begins.

Condition
expand arrow

Defines when the loop should stop running. The loop continues executing as long as this condition remains true.

Expression
expand arrow

Updates the loop variable after each iteration. This often increments or decrements the counter, ensuring progress toward the exit condition.

main.cpp

main.cpp

copy
123456789
#include <iostream> int main() { for (int counter = 0; counter <= 5; counter++) { std::cout << counter << std::endl; } }

The variable int counter = 0 starts the iteration counter at 0. The expression counter++ adds 1 to the counter each time the loop runs, marking each iteration. The condition counter <= 5 ensures the loop continues as long as the counter is less than or equal to 5.

question mark

How many iterations will this loop make?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 3.85

bookFor Loop

Swipe to show menu

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

loop_statement.cpp

loop_statement.cpp

copy
1234
for (counter; condition; expression) { // Block of instruction }
Counter
expand arrow

Initializes the loop variable. It usually sets a starting point, such as int i = 0, which determines where the loop begins.

Condition
expand arrow

Defines when the loop should stop running. The loop continues executing as long as this condition remains true.

Expression
expand arrow

Updates the loop variable after each iteration. This often increments or decrements the counter, ensuring progress toward the exit condition.

main.cpp

main.cpp

copy
123456789
#include <iostream> int main() { for (int counter = 0; counter <= 5; counter++) { std::cout << counter << std::endl; } }

The variable int counter = 0 starts the iteration counter at 0. The expression counter++ adds 1 to the counter each time the loop runs, marking each iteration. The condition counter <= 5 ensures the loop continues as long as the counter is less than or equal to 5.

question mark

How many iterations will this loop make?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 5
some-alt