Use of break statement

Break-in this case not only exits if statement but while as well. Is it possible that if we use break statement in the nested conditional loop it will exit all the statements.

@rastogi.g1998
break; statement is used to break out of loop for a specific condition.
while (true)
{
int n;
cin >> n ;
if ( n == 0 )
break ;
}

This code snippet shows the use of break;. Instead of putting in a loop condition , I have used break statement to break out of the loop. It does not break the if statement , that doesnt mean anything. Break only has any meaning with loop here.
If you were to use break in nested loops , it would only break out of the inner loop. The outer loop would still work.

So if code is
While(i<=n-1){
If(n%i==0){
Cout<<not prime;
Break;
}
I=i+1;}
Cout<<prime;
So in this program will the value of I would be updates or not or will it directly execute cout command.as break statement exits if will increment in while take place.

@rastogi.g1998
When the break statement is hit , the control of the program is transferred to the next statement outside the loop. So as soon as we hit the break condition in the loop , we will move to the next statement outside the loop which is the cout << “prime” ; statement. The value of i will not be incremented in this iteration as that statement would be skipped.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.