Online Computer Courses Classes and Training Program

how to use Break statement in c++


Break Statement in C++


Break Statement is a loop control statement which is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stops there and control returns from the loop immediately to the first statement after the loop.
Syntax:
break;
We will see here the usage of break statement with three different types of loops:
  1. Simple loops
  2. Nested loops
  3. Infinite loop
  4. Example of break statement
#include<iostream>
using namespace std;
main()
{
               int num,sum=0;
               while(true)
               {
                              cout<<"Enter a number:";
                              cin>>num;
                              if(num!=0)
                              {
                                             sum+=num;
                              }
                              else
                              {
                                             break;
                              }
              
               }
               cout<<"Sum = "<<sum;
               return 0;
}

Post a Comment

0 Comments