Question

I ran into strange situation here is what happens. I am using a do while loop and it exceutes a while function one more time after brake.
Here is a simplyfied version of what happens:

int i = 0;
do{
if(i>=1) break;
}while(i++);

And after this i value is 2. I am just curious if that is how it should be or I just made an error somewhere in my more copmlex version of this code?
This is an easy fix for this I just have to assign a to say an iTemp variable in the begining of the wgile and use that insted of i.

P.S. I was using two do while loops one inside of another maybe that is what caused it?

EDIT: Cmon people give me a break it was typo. :D

Was it helpful?

Solution

P.S. I was using two do while loops one inside of another maybe that is what caused it?

It could well be. A break (note the spelling) will only break out of one loop, so if it was inside the inner loop, it would break out of that loop, but continue execution immediately after the end of the inner loop. In the (fairly common) case of the outer loop ending immediately after the inner, you'd end up executing the condition of the outer loop again.

OTHER TIPS

brake is not a keyword and your modified version does not even compile. The old version is run instead, that is, without your new break.

Solution: Use break.


update:

P.S. I was using two do while loops one inside of another maybe that is what caused it?

Yes. break does only break out of the innermost loop it is written in.

Btw, I realise that you displayed loop will always only run once. Your looping condition is i++, which increments i and gives you the value before the increment. Before your first increment, i is 0, thus your looping condition is 0 upon first run, therefore, runs once.

check its break not brake?

int i = 0;
do
{    
 if(i>=1) 
    break;
}while(i++);

the looping condition and beak ing conditions are unthinkable. "sorry if i seem rude"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top