break and continue can also be used with nested loops. When break is encountered inside a nested loop, it only terminates the innermost loop and control is transferred to the next statement outside that loop. Similarly, when continue is encountered, it only skips the current iteration of the innermost loop.
Example:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i * j == 6) {
break; // Exit the inner loop when i*j equals 6
}
cout << i * j << " ";
}
cout << endl;
}
// Output:
// 1 2 3
// 2 4
// 3