0
0
C++programming~5 mins

For loop in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a for loop in C++?
A for loop is a control structure that repeats a block of code a set number of times. It has three parts: initialization, condition, and update.
Click to reveal answer
beginner
Write the basic syntax of a for loop in C++.
for (initialization; condition; update) {<br> // code to repeat<br>}
Click to reveal answer
beginner
What happens if the condition in a for loop is false at the start?
The loop body does not run at all, and the program continues after the loop.
Click to reveal answer
beginner
How can you use a for loop to print numbers from 1 to 5?
for (int i = 1; i <= 5; i++) {<br> std::cout << i << std::endl;<br>}
Click to reveal answer
beginner
What is the role of the update expression in a for loop?
The update expression changes the loop variable each time the loop runs, usually to move toward the loop ending condition.
Click to reveal answer
Which part of the for loop runs first?
AInitialization
BCondition check
CUpdate expression
DLoop body
What happens if the condition in a for loop is always true?
AThe loop runs once
BThe loop runs forever unless broken
CThe loop never runs
DThe program crashes
Which symbol is used to separate the parts of a for loop?
A;
B,
C:
D.
How do you increase the loop variable by 1 each time?
Ai = i - 1
Bi--
Ci++
Di = 0
What will this loop print? for(int i=0; i<3; i++) { std::cout << i; }
A321
B123
C000
D012
Explain how a for loop works in C++ and describe its three main parts.
Think about how the loop starts, checks if it should continue, runs code, and then changes the variable.
You got /5 concepts.
    Write a for loop that counts down from 5 to 1 and prints each number.
    Start with i = 5, stop when i is less than 1, and decrease i each time.
    You got /5 concepts.