0
0
Cprogramming~10 mins

Do–while loop in C - Interactive Code Practice

Choose your learning style9 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.