Next: Nested Loops
Up: Overview of C++
Previous: continue Statement
It is possible to force an immediate exit from a loop by using the break
statement. When the break statement is encountered inside a loop, the loop
is immediately terminated and the program control resumes at the next statement
following the loop.
Example
#include <iostream.h>
main()
{
int x;
for(x=1;x<=100;x++) {
if(x==10) break;
cout << x<<' ';
}
return 0;
}
This program print numbers 1 through 10 on the screen before ending. It will not
go to 100 because the break statement will cause it to terminate early.
Yousef Haik
2/23/1998