Bird
Raised Fist0
Cprogramming~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 print numbers from 1 to 3 using a do–while loop.

C
int i = 1;
do {
    printf("%d\n", i);
    i[1];
} while (i <= 3);
Drag options to blanks, or click blank then click option'
A-
B--
C+=
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' decreases the value, causing an infinite loop.
Using '-' alone is a subtraction operator but needs a number.
Using '+=' without a number is incomplete.
2fill in blank
medium

Complete the code to ensure the loop runs at least once and stops when count is 5.

C
int count = 0;
do {
    printf("Count: %d\n", count);
    count++;
} while ([1] < 5);
Drag options to blanks, or click blank then click option'
Acount
Bcount++
Ccount--
D++count
Attempts:
3 left
💡 Hint
Common Mistakes
Using count++ or ++count in the condition changes the variable unexpectedly.
Using count-- decreases the value, causing wrong loop behavior.
3fill in blank
hard

Fix the error in the do–while loop condition to avoid an infinite loop.

C
int n = 10;
do {
    printf("%d\n", n);
    n--;
} while (n [1] 0);
Drag options to blanks, or click blank then click option'
A==
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' stops the loop after printing only 10 because after n-- , n=9 which is not < 0.
Using '==' stops the loop after printing only 10 because after n-- , n=9 which != 0.
4fill in blank
hard

Fill both blanks to create a do–while loop that prints even numbers from 2 to 8.

C
int num = 2;
do {
    printf("%d\n", num);
    num [1] 2;
} while (num [2] 8);
Drag options to blanks, or click blank then click option'
A+=
B-=
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' decreases the number, causing an infinite loop.
Using '>' or '>=' in the condition causes the loop to never run or run infinitely.
5fill in blank
hard

Fill all three blanks to create a do–while loop that prints numbers from 5 down to 1.

C
int val = [1];
do {
    printf("%d\n", val);
    val [2] 1;
} while (val [3] 1);
Drag options to blanks, or click blank then click option'
A5
B-=
C>=
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 instead of 5 causes the loop to print only once.
Using '+=' increases the value, causing an infinite loop.
Using '<=' in the condition causes the loop to run infinitely.

Practice

(1/5)
1. What is the main difference between a do-while loop and a while loop in C?
easy
A. A do-while loop cannot be used with integers.
B. A do-while loop checks the condition before executing the code block.
C. A do-while loop executes the code block at least once before checking the condition.
D. A do-while loop does not require a semicolon after the while condition.

Solution

  1. Step 1: Understand the execution order of loops

    A while loop checks the condition first, so it may not run the code block if the condition is false initially.
  2. Step 2: Identify the behavior of do-while loop

    A do-while loop runs the code block once before checking the condition, ensuring at least one execution.
  3. Final Answer:

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

    do-while runs once first = A [OK]
Hint: Remember: do-while runs code first, then checks condition [OK]
Common Mistakes:
  • Thinking do-while checks condition first
  • Believing semicolon after while is optional
  • Confusing do-while with while loop behavior
2. Which of the following is the correct syntax for a do-while loop in C?
easy
A. do { /* code */ } while (condition);
B. do ( /* code */ ) while condition;
C. do { /* code */ } while condition
D. do while (condition) { /* code */ };

Solution

  1. Step 1: Recall the correct syntax structure

    The do-while loop requires braces around the code block and a semicolon after the while condition in parentheses.
  2. Step 2: Compare options to syntax

    do { /* code */ } while (condition); matches the correct syntax: do { /* code */ } while (condition); with braces and semicolon.
  3. Final Answer:

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

    Semicolon after while(condition) = B [OK]
Hint: Look for braces and semicolon after while(condition) [OK]
Common Mistakes:
  • Omitting semicolon after while condition
  • Using parentheses instead of braces for code block
  • Placing while before do
3. What will be the output of the following C code?
int i = 3;
do {
    printf("%d ", i);
    i--;
} while (i > 0);
medium
A. 1 2 3
B. 2 1 0
C. 3 2 1 0
D. 3 2 1

Solution

  1. Step 1: Trace the loop iterations

    Initially, i = 3. The loop prints i, then decrements i by 1. It repeats while i > 0.
  2. Step 2: List printed values

    First iteration: prints 3, i becomes 2
    Second iteration: prints 2, i becomes 1
    Third iteration: prints 1, i becomes 0
    Loop stops because i is not > 0.
  3. Final Answer:

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

    Prints 3 down to 1 = A [OK]
Hint: Count printed values before condition fails [OK]
Common Mistakes:
  • Including 0 in output
  • Starting from 2 instead of 3
  • Printing in ascending order
4. Identify the error in this do-while loop code snippet:
int x = 5;
do {
    printf("%d", x);
    x--;
} while x > 0;
medium
A. The loop condition should be inside braces.
B. Missing parentheses around the while condition.
C. Variable x is not initialized.
D. Missing semicolon after the do block.

Solution

  1. Step 1: Check syntax of the while condition

    The while condition must be enclosed in parentheses, like while (x > 0);.
  2. Step 2: Identify the error in the code

    The code has while x > 0; missing parentheses, causing a syntax error.
  3. Final Answer:

    Missing parentheses around the while condition. -> Option B
  4. Quick Check:

    while(condition) needs parentheses = D [OK]
Hint: Always put condition in parentheses after while [OK]
Common Mistakes:
  • Omitting parentheses in while condition
  • Forgetting semicolon after while statement
  • Misplacing braces around condition
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 positive number. Which code snippet correctly implements this?
hard
A. int num; do { printf("Enter a positive number: "); scanf("%d", &num); } while (num <= 0);
B. int num = 0; do { scanf("%d", &num); printf("Enter a positive number: "); } while (num > 0);
C. int num; do { scanf("%d", &num); } while (num < 0);
D. int num; do { printf("Enter a positive number: "); scanf("%d", &num); } while (num < 0);

Solution

  1. Step 1: Understand the requirement

    The loop must ask for input at least once and repeat while the number is not positive (<= 0).
  2. Step 2: Analyze each option

    int num; do { printf("Enter a positive number: "); scanf("%d", &num); } while (num <= 0); prompts before input and repeats while number is <= 0, which matches the requirement.
    int num = 0; do { scanf("%d", &num); printf("Enter a positive number: "); } while (num > 0); prints prompt after input and repeats while number > 0, which is incorrect.
    int num; do { scanf("%d", &num); } while (num < 0); repeats while number < 0, missing zero case.
    int num; do { printf("Enter a positive number: "); scanf("%d", &num); } while (num < 0); repeats while number < 0, missing zero case.
  3. Final Answer:

    int num; do { printf("Enter a positive number: "); scanf("%d", &num); } while (num <= 0); -> Option A
  4. Quick Check:

    Repeat while number not positive = C [OK]
Hint: Repeat while input ≤ 0, prompt before input [OK]
Common Mistakes:
  • Checking wrong condition (e.g., num < 0 instead of <= 0)
  • Prompting after input instead of before
  • Not handling zero as invalid 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