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:
- Simple loops
- Nested loops
- Infinite loop
- 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;
}
0 Comments
कमेंट केवल पोस्ट से रिलेटेड करें