The C++ break
keyword ends the loop structure. It causes a loop to end immediately, instead of waiting for its condition to be false. In case of inner loop, it breaks only inner loop.
jump-statement; break;
Let's see a simple example of C++ break statement which is used inside the loop.
#include <iostream> using namespace std; int main() { for (int i = 1; i <= 10; i++) { if (i == 5) { break; } cout<<i<<"\n"; } }
If you use break
statement inside the inner loop it breaks only the inner loop.
Let's see the example code
#include <iostream> using namespace std; int main() { for(int i=1;i<=3;i++){ for(int j=1;j<=3;j++){ if(i==2&&j==2){ break; } cout<<i<<" "<<j<<"\n"; } } }