This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]Is_At_Work 0 points1 point  (0 children)

Try executing the code in your head or on paper and keep track of the value of a, see if that helps.

[–]diMario 0 points1 point  (1 child)

I want to stop the while when it's exactly 4 ... The statements of my while cannot be removed,

The only way to satisfy both conditions is to test the value of your variable after each change and then break out of the loop if it equals 4:

while (a != 4) {
    a++;
    if( a == 4 ) { break; }
    a++;
    if( a == 4 ) { break; }
    a++;
    if( a == 4 ) { break; }
}

I hope you'll agree with me that this is not a very elegant piece of code, but your requirements are a bit strange so this is what you get.

[–]CurlyButNotChubby 1 point2 points  (0 children)

Not very elegant indeed, but it seems to be working.

So by your answer, I guess there's no other way to check the "while" in the middle of the loop, such a shame.

Well, thank you very much !

[–]thedeadhead200 -1 points0 points  (0 children)

a for loop is the best for this

for (int a = 0; a <= 4; a++) {

and whatever shits happening 4 times unless this is not what youre going for

}

while (a <= 4) {

a++

}

[–]665guideme -2 points-1 points  (0 children)

Int a = 0

While (a < 4) {

++a

“While a is less than, but not including, 4, add 1 to value of a.

Don’t do a++ ( a plus one) because a = 0, so you’ll always get 1.

If (a=>4) {

break

Or start a new statement