0
0
Cprogramming~10 mins

Why loop control is required - Test Your Understanding

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 5 using a loop.

C
for(int i = 1; i [1] 5; i++) {
    printf("%d\n", i);
}
Drag options to blanks, or click blank then click option'
A>=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will stop the loop before printing 5.
Using '>' or '>=' will cause the loop not to run as expected.
2fill in blank
medium

Complete the code to skip printing the number 3 in the loop.

C
for(int i = 1; i <= 5; i++) {
    if(i [1] 3) {
        continue;
    }
    printf("%d\n", i);
}
Drag options to blanks, or click blank then click option'
A==
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' will skip all numbers except 3.
Using '>' or '<' will skip wrong numbers.
3fill in blank
hard

Fix the error in the loop to avoid an infinite loop.

C
int i = 1;
while(i [1] 5) {
    printf("%d\n", i);
    // Missing increment
    i++;
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Not incrementing the loop variable causes infinite loops.
Using wrong comparison operators can cause the loop to never run or run infinitely.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

C
for(int i = [1]; i [2] 10; i += 2) {
    printf("%d\n", i);
}
Drag options to blanks, or click blank then click option'
A2
B1
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will print odd numbers.
Using '<' will stop before printing 10.
5fill in blank
hard

Fill all three blanks to create a loop that prints numbers from 10 down to 1.

C
for(int i = [1]; i [2] 1; i[3]) {
    printf("%d\n", i);
}
Drag options to blanks, or click blank then click option'
A10
B>=
C--
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' will cause the loop to never run.
Using '++' will cause an infinite loop counting up.