0
0
C++programming~20 mins

Do–while loop in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Do–while Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple do–while loop
What is the output of this C++ code using a do–while loop?
C++
int i = 1;
do {
    std::cout << i << " ";
    i++;
} while (i <= 3);
A1 2
B1 2 3
C2 3
DNo output
Attempts:
2 left
💡 Hint
Remember, do–while executes the loop body before checking the condition.
Predict Output
intermediate
2:00remaining
Do–while loop with condition false initially
What will this C++ program print?
C++
int x = 5;
do {
    std::cout << x << " ";
    x++;
} while (x < 5);
A5
BNo output
C5 6 7 ... (infinite loop)
D6
Attempts:
2 left
💡 Hint
The loop runs once before checking the condition.
🔧 Debug
advanced
2:00remaining
Identify the error in this do–while loop
What error will this code cause?
C++
int count = 0;
do {
    std::cout << count << " ";
    count++;
} while (count < 3);
ASyntaxError: missing semicolon
BRuntime error: infinite loop
CNo error, prints 0 1 2
DCompilation error: undeclared variable
Attempts:
2 left
💡 Hint
Check punctuation carefully in C++.
Predict Output
advanced
2:00remaining
Counting down with do–while loop
What does this code print?
C++
int n = 3;
do {
    std::cout << n << " ";
    n--;
} while (n > 0);
A3 2 1 0
B1 2 3
C3 2 1
DNo output
Attempts:
2 left
💡 Hint
The loop runs while n is greater than zero.
🧠 Conceptual
expert
2:00remaining
Behavior difference between while and do–while loops
Which statement best describes the difference between a while loop and a do–while loop in C++?
AA while loop always executes its body at least once, while a do–while loop may not execute at all.
BNeither loop executes if the condition is false initially.
CBoth loops always execute their bodies at least once.
DA do–while loop always executes its body at least once, while a while loop may not execute at all.
Attempts:
2 left
💡 Hint
Think about when the condition is checked in each loop.