Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to exit the loop when i equals 5.
C
for(int i = 0; i < 10; i++) { if(i == 5) { [1]; } printf("%d\n", i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue instead of break, which skips the rest of the loop iteration but does not exit the loop.
Using return or exit which exit the function or program instead of just the loop.
✗ Incorrect
The break statement immediately exits the nearest enclosing loop.
2fill in blank
mediumComplete the code to stop the while loop when count reaches 3.
C
int count = 0; while(1) { if(count == 3) { [1]; } printf("Count: %d\n", count); count++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue which skips the current iteration but does not exit the loop.
Using exit or return which exit the program or function.
✗ Incorrect
break exits the loop when count equals 3.
3fill in blank
hardFix the error in the code to exit the for loop when num is 7.
C
for(int num = 0; num < 10; num++) { if(num == 7) { [1]; } printf("%d\n", num); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue which does not exit the loop.
Using exit() or return which exit the program or function.
✗ Incorrect
break exits the loop correctly; exit() and return affect the program or function, not just the loop.
4fill in blank
hardFill both blanks to create a loop that prints numbers until it reaches 4, then stops.
C
for(int i = 0; i < 10; i++) { if(i [1] 4) { [2]; } printf("%d\n", i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of == in the condition.
Using continue instead of break to exit the loop.
✗ Incorrect
The condition i == 4 triggers break to exit the loop.
5fill in blank
hardFill all three blanks to create a loop that stops printing when the character 'q' is found in the array.
C
char letters[] = {'a', 'b', 'c', 'q', 'd'};
for(int [1] = 0; [2] < 5; [3]++) {
if(letters[i] == 'q') {
break;
}
printf("%c\n", letters[i]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop declaration.
Using a variable not declared or inconsistent.
✗ Incorrect
The loop variable i is used consistently in initialization, condition, and increment.