June 2025
Intermediate to advanced
1093 pages
33h 24m
English
The break keyword allows you to exit the current loop—the innermost one, from the perspective of break. The program continues with the statement following the loop. You can use this in all for and while loops, including the range-based for and the do-while. In this sense, switch is considered a loop.
// https://godbolt.org/z/75h9Y95vf#include <iostream> // coutint main() { for(int x=1; x<20; x+=1) { // outer for-loop for(int y=1; y<20; y+=1) { // inner for-loop int prod = x*y; if(prod>=100) { break; // exit inner y-loop } std::cout << prod << " "; } /* end for y */ // destination of break } /* end for x */ // first actual line after break std::cout << " ";}
Listing 8.20 With break, you terminate a loop prematurely. ...
Read now
Unlock full access