Jump out of the loop and start executing the statements after the loop using the break - C++ Statement

C++ examples for Statement:break

Description

Jump out of the loop and start executing the statements after the loop using the break

Demo Code

#include <iostream> 
using namespace std ; 

int main(){//from   ww w  . ja va2  s.co  m
   cout << " Enter positive numbers followed "; 
   cout << "by a negative number to quit \n"; 
   int total = 0; 
   while (true ) { 
       int next ; 
       cin >> next ; 
       if (next <0) { 
          break ; 
      } 
      total += next ; 
   } 
   cout << " The total is " << total << "\n"; 
}

Result


Related Tutorials