break only exits the immediate innermost loop or switch block. It does not break out of multiple nested outer loops in a single statement.
main.cpp
#include <iostream>
int main() {
int target = 7;
std::cout << "Searching for " << target << " in loop...
";
for (int i = 1; i <= 20; ++i) {
if (i == target) {
std::cout << "Found target " << target << " at index " << i << ". Breaking loop.
";
break; // Exits loop immediately
}
}
// Switch case with break
char grade = 'A';
switch (grade) {
case 'A':
std::cout << "Grade: Excellent Achievement
";
break;
case 'B':
std::cout << "Grade: Well Done
";
break;
default:
std::cout << "Grade: Passing
";
break;
}
return 0;
}