Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ For Loop | Introduction to Program Flow
C++ Introduction (copy) 1769617216468njii0h

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?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 4.  5

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 4.  5
some-alt