Complete the code to exit the loop when the number is 5.
#include <iostream> int main() { for (int i = 1; i <= 10; ++i) { if (i == 5) { [1]; } std::cout << i << " "; } return 0; }
The break statement immediately exits the loop when i equals 5.
Complete the code to stop the while loop when the input number is negative.
#include <iostream> int main() { int num; while (true) { std::cin >> num; if (num < 0) { [1]; } std::cout << "Number: " << num << std::endl; } return 0; }
The break statement exits the infinite while loop when a negative number is entered.
Fix the error in the code to exit the loop when the character 'q' is entered.
#include <iostream> int main() { char ch; while (true) { std::cin >> ch; if (ch == 'q') { [1]; } std::cout << ch << std::endl; } return 0; }
The break statement exits the loop when the user inputs 'q'.
Fill both blanks to create a loop that prints numbers 1 to 10 but stops at 7.
#include <iostream> int main() { for (int i = 1; i <= [1]; ++i) { if (i == [2]) { break; } std::cout << i << " "; } return 0; }
The loop runs from 1 to 10, but breaks when i equals 7, stopping the loop early.
Fill all three blanks to create a loop that prints numbers 1 to 20 but stops at the first number divisible by 8.
#include <iostream> int main() { for (int num = 1; num <= [1]; ++num) { if (num % [2] == 0) { [3]; } std::cout << num << " "; } return 0; }
The loop runs from 1 to 20, and breaks when it finds the first number divisible by 8.