Bird
Raised Fist0
C++programming~10 mins

Do–while loop in C++ - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a do-while loop that prints "Hello" once.

C++
int main() {
    int count = 0;
    do {
        std::cout << "Hello\n";
        count++;
    } while ([1]);
    return 0;
}
Drag options to blanks, or click blank then click option'
Acount < 1
Bcount > 1
Ccount == 1
Dcount != 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that is false initially, so the loop never repeats.
Using 'count > 1' which is false at the start, causing no repetition.
2fill in blank
medium

Complete the code to keep asking the user for a number until they enter 0.

C++
int main() {
    int num;
    do {
        std::cout << "Enter a number (0 to stop): ";
        std::cin >> num;
    } while ([1]);
    return 0;
}
Drag options to blanks, or click blank then click option'
Anum == 0
Bnum != 0
Cnum > 0
Dnum < 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'num == 0' which would stop the loop immediately.
Using conditions that only check for positive or negative numbers.
3fill in blank
hard

Fix the error in the do-while loop condition to repeat while x is less than 5.

C++
int main() {
    int x = 0;
    do {
        std::cout << x << " ";
        x++;
    } while (x [1] 5);
    return 0;
}
Drag options to blanks, or click blank then click option'
A>
B==
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which would cause the loop to never run.
Using '==' which only runs when x equals 5.
4fill in blank
hard

Fill both blanks to create a do-while loop that prints numbers from 1 to 3.

C++
int main() {
    int i = 1;
    do {
        std::cout << i << " ";
        i[1];
    } while (i [2] 3);
    return 0;
}
Drag options to blanks, or click blank then click option'
A++
B--
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which decreases i and causes an infinite loop.
Using '>' or '>=' which would not print numbers 1 to 3 correctly.
5fill in blank
hard

Fill all three blanks to create a do-while loop that sums numbers from 1 to 5.

C++
int main() {
    int sum = 0;
    int num = 1;
    do {
        sum [1] num;
        num [2];
    } while (num [3] 5);
    std::cout << sum << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A+=
B++
C<=
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' or '--' which decreases values incorrectly.
Using '<' instead of '<=' which misses the last number 5.

Practice

(1/5)
1.

What is the main difference between a do-while loop and a while loop in C++?

easy
A. do-while loop runs the code block at least once before checking the condition.
B. while loop always runs the code block at least once.
C. do-while loop checks the condition before running the code block.
D. There is no difference; both behave the same.

Solution

  1. Step 1: Understand do-while loop behavior

    A do-while loop executes the code block first, then checks the condition to decide if it repeats.
  2. Step 2: Compare with while loop

    A while loop checks the condition first before running the code block, so it may not run at all if the condition is false initially.
  3. Final Answer:

    do-while loop runs the code block at least once before checking the condition. -> Option A
  4. Quick Check:

    do-while runs once before condition check [OK]
Hint: Remember: do-while runs code first, then checks condition [OK]
Common Mistakes:
  • Thinking while loop always runs code once
  • Confusing order of condition check
  • Assuming do-while and while behave the same
2.

Which of the following is the correct syntax for a do-while loop in C++?

?
easy
A. while (condition) do { /* code */ };
B. do ( /* code */ ) while condition;
C. do { /* code */ } while (condition);
D. do { /* code */ } until (condition);

Solution

  1. Step 1: Recall correct do-while syntax

    The correct syntax uses do { ... } while (condition); with braces and a semicolon after the while condition.
  2. Step 2: Identify syntax errors in other options

    do ( /* code */ ) while condition; misses braces and semicolon; C uses incorrect keyword 'until'; A reverses order incorrectly.
  3. Final Answer:

    do { /* code */ } while (condition); -> Option C
  4. Quick Check:

    Syntax ends with semicolon after while(condition) [OK]
Hint: Look for braces and semicolon after while(condition) [OK]
Common Mistakes:
  • Omitting semicolon after while(condition)
  • Using 'until' instead of 'while'
  • Incorrect order of do and while keywords
3.

What will be the output of the following C++ code?

int i = 3;
do {
    std::cout << i << " ";
    i--;
} while (i > 0);
medium
A. 3 2 1
B. 3 2 1 0
C. 2 1 0
D. 1 2 3

Solution

  1. Step 1: Trace the loop iterations

    Initially, i=3. The loop prints 3, then decrements i to 2. Condition i>0 is true, so repeats printing 2, then 1.
  2. Step 2: Check when loop stops

    After printing 1, i becomes 0. Condition i>0 is false, loop stops. So output is "3 2 1 "
  3. Final Answer:

    3 2 1 -> Option A
  4. Quick Check:

    Loop runs while i>0, prints 3,2,1 [OK]
Hint: Count printed values before condition fails [OK]
Common Mistakes:
  • Including 0 in output
  • Starting count from 2 instead of 3
  • Printing in wrong order
4.

Identify the error in this do-while loop code snippet:

int x = 5;
do {
    std::cout << x << " ";
    x++;
} while x < 10;
medium
A. The loop should end with a comma, not a semicolon.
B. The variable x should be decremented, not incremented.
C. The do keyword is misspelled.
D. Missing parentheses around the condition in while statement.

Solution

  1. Step 1: Check syntax of while condition

    The condition after while must be enclosed in parentheses: while (x < 10);.
  2. Step 2: Verify other parts

    Incrementing x is valid; do is spelled correctly; loop ends with semicolon, which is correct.
  3. Final Answer:

    Missing parentheses around the condition in while statement. -> Option D
  4. Quick Check:

    while condition needs parentheses [OK]
Hint: Always put condition in parentheses after while [OK]
Common Mistakes:
  • Forgetting parentheses in while condition
  • Confusing semicolon with comma at loop end
  • Thinking increment is always wrong
5.

You want to write a do-while loop that asks the user to enter a positive number and repeats until the user enters a number greater than zero. Which code snippet correctly implements this?

?
hard
A. int num; while (num <= 0) { std::cout << "Enter positive number: "; std::cin >> num; }
B. int num; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num <= 0);
C. int num = 0; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num > 0);
D. int num; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num < 0);

Solution

  1. Step 1: Understand the condition to repeat

    The loop should continue while the number is less than or equal to zero, so condition is num <= 0.
  2. Step 2: Check each option's condition

    int num; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num <= 0); uses do-while (num <= 0); which repeats until user enters positive number. int num = 0; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num > 0); repeats while number is positive (wrong). int num; while (num <= 0) { std::cout << "Enter positive number: "; std::cin >> num; } uses while loop, not do-while. int num; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num < 0); repeats while number is less than zero, so zero input stops loop incorrectly.
  3. Final Answer:

    int num; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num <= 0); -> Option B
  4. Quick Check:

    Repeat while input ≤ 0 to ensure positive input [OK]
Hint: Repeat while input is not positive (≤ 0) [OK]
Common Mistakes:
  • Using wrong condition sign (> 0 instead of ≤ 0)
  • Using while loop instead of do-while
  • Stopping loop too early on zero input
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard