break statement in c++


  • The break statement causes an immediate exit from a loop. Because the code that follows it in the loop is not executed if the break is reached, you can also sometimes avoid nesting by including a break.
  • It is possible to force an immediate exit from a loop, bypassing the loop’s conditional test, by using the break statement.
  • The break keyword causes the entire switch statement to exit.
  • We have used a while statement to compute Fibonacci numbers less than 100. The loop exits when the condition at the beginning becomes false. Loops also can be exited at any point through the use of a break statement.

Syntax

    break;

Flow Diagram

    break statement in c++ language

Example

    #include <iostream>
    using namespace std;
       int main()
        {
         int t;
    
         // Loops from 0 to 9, not to 100!
         for(t=0; t<100; t++)
          {
           if(t==10)
           break; cout << t << ' ';
         }
      return 0;
    }
    

Output

     0 1 2 3 4 5 6 7 8 9