📖 Scenario: You are creating a simple program that checks a list of numbers to find the first number that is greater than a certain limit. Once the program finds this number, it should stop checking the rest.
🎯 Goal: Build a C++ program that uses a break statement inside a for loop to stop the loop when a number greater than a limit is found.
📋 What You'll Learn
Create an array of integers with exact values
Create an integer variable for the limit
Use a for loop to check each number in the array
Use a break statement to stop the loop when a number greater than the limit is found
Print the first number found that is greater than the limit
💡 Why This Matters
🌍 Real World
Breaking out of loops early is useful when searching for a specific item in a list, like finding the first available seat in a theater or the first error in a log.
💼 Career
Understanding how to control loops with break statements is important for efficient programming and is used in many software development tasks.
Progress0 / 4 steps
1
Create the array of numbers
Create an integer array called numbers with these exact values: 3, 7, 10, 15, 20.
C++
Hint
Use curly braces {} to list the values inside the array.
2
Set the limit value
Create an integer variable called limit and set it to 12.
C++
Hint
Use int limit = 12; to create the variable.
3
Use a for loop with break
Write a for loop using int i = 0 to i < 5 to check each number in numbers. Inside the loop, use an if statement to check if numbers[i] is greater than limit. If yes, use break to stop the loop.
C++
Hint
Use for (int i = 0; i < 5; i++) and inside it check if (numbers[i] > limit) then break;.
4
Print the first number greater than the limit
After the loop, print the message "First number greater than 12 is: " followed by the value numbers[i].
C++
Hint
Use std::cout to print the message and the number.
Practice
(1/5)
1.
What does the break statement do inside a loop in C++?
easy
A. It skips the current iteration and continues.
B. It pauses the loop until user input.
C. It restarts the loop from the beginning.
D. It immediately exits the loop.
Solution
Step 1: Understand the role of break in loops
The break statement is used to stop the loop immediately when executed.
Step 2: Compare with other loop controls
Unlike continue which skips to the next iteration, break exits the loop completely.
Final Answer:
It immediately exits the loop. -> Option D
Quick Check:
Break stops loop = exit loop [OK]
Hint: Break means stop loop now, not skip or pause [OK]
Common Mistakes:
Confusing break with continue
Thinking break pauses instead of exits
Assuming break restarts the loop
2.
Which of the following is the correct syntax to use break inside a for loop in C++?
for (int i = 0; i < 5; i++) {
if (i == 3) {
?
} // other code
}
easy
A. stop;
B. break;
C. exit;
D. continue;
Solution
Step 1: Recall the keyword to exit loops
The keyword to exit a loop immediately in C++ is break;.
Step 2: Check other options for correctness
stop; and exit; are invalid here, and continue; skips iteration, not exit.