Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to skip printing the number 3 in the loop.
C++
#include <iostream> int main() { for (int i = 1; i <= 5; i++) { if (i == 3) { [1]; } std::cout << i << " "; } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' instead of 'continue' which stops the loop completely.
Using 'return' which exits the whole function.
Using 'exit' which ends the program.
✗ Incorrect
The 'continue' statement skips the current iteration and moves to the next one, so number 3 is not printed.
2fill in blank
mediumComplete the code to skip all even numbers in the loop.
C++
#include <iostream> int main() { for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { [1]; } std::cout << i << " "; } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' which stops the loop entirely.
Using 'return' which exits the function.
Using 'exit' which ends the program.
✗ Incorrect
The 'continue' statement skips printing even numbers by moving to the next iteration.
3fill in blank
hardFix the error in the code to correctly skip printing negative numbers.
C++
#include <iostream> int main() { int numbers[] = {2, -1, 3, -4, 5}; for (int i = 0; i < 5; i++) { if (numbers[i] < 0) { [1]; } std::cout << numbers[i] << " "; } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' which stops the loop completely.
Using 'return' which exits the function.
Using 'exit' which ends the program.
✗ Incorrect
Using 'continue' skips printing negative numbers and continues the loop.
4fill in blank
hardFill both blanks to skip printing multiples of 3 and continue the loop.
C++
#include <iostream> int main() { for (int i = 1; i <= 10; i++) { if (i [1] 3 [2] 0) { continue; } std::cout << i << " "; } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%' for modulus.
Using '!=' instead of '==' in the condition.
Using 'break' instead of 'continue'.
✗ Incorrect
The condition 'i % 3 == 0' checks for multiples of 3, and 'continue' skips printing them.
5fill in blank
hardFill all three blanks to skip printing numbers less than 5 and continue the loop.
C++
#include <iostream> int main() { int arr[] = {2, 5, 7, 3, 8}; for (int [1] = 0; [2]; [3]++) { if (arr[i] < 5) { continue; } std::cout << arr[i] << " "; } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop declaration and condition.
Using 'j' instead of 'i' causing errors.
Incorrect loop condition.
✗ Incorrect
The loop variable is 'i', the condition is 'i < 5', and the increment is 'i++'.