tags:

views:

218

answers:

5
while (status)

for (int i = 0; i < 3; i++)

Is the following syntactically correct:

for (int i = 0; i < 3; i++ && status)

I am trying to have the for loop break early if status is true.

+15  A: 

Syntactically, you might want to use:

for (int i = 0; i < 3 && status; i++)

which is valid.

Some consider it bad form though, as it leads to more complicated loops and annoyed maintenance programmers. Another alternative you might want to explore would be:

for (int i = 0; i < 3; i++) {
     if (!status) { break; }
}
Adam Luchjenbroers
So what would be considered good form? Using a while loop instead?
Brandon
I've added an example using break, which is (debatably) better form. If you've got a copy of Code Complete handy there's a whole chapter on the readability of control structures, so that's hardly the end of the discussion.
Adam Luchjenbroers
I'm voting this up in anticipation of all the "never-use-break-in-loops" crowd (that I disagree with) soon to berate you. I just can't resist poking people with a stick :-)
paxdiablo
+1, I disagree with anyone saying "never-use-..."
MAK
pax and MAK are up in arms about fighting some "battle" against some "crowd" and completely missed that it needs to be `!status` if moved from the for condition to an if statement. Maybe there's something to the idea this hurts maintainability.
Roger Pate
Correction made. I'd personally consider the use of break to be clearer than overloading the for loop test, but there's other solutions as well out there (eg. use a `while` loop or find another way of organizing the code).
Adam Luchjenbroers
Adam: I just dislike people provoking conflict when more basic concerns, like correctness, are overlooked.
Roger Pate
@Roger, the first thing you should do when looking at any comment is to see if there's a smiley at the end :-)
paxdiablo
Yeah, who cares what we say or do as long as there's a smiley at the end. It's all fun 'n' games until someone loses an eye. .-)
Roger Pate
@Roger: though I mainly agree with you, there is a problem with the question. The text ("I am trying to have the for loop break early if status is true") disagrees with the code, and this may have misled Adam. In fact, taking into acount the the code in the question is wrong (status is in the wrong place of the for loop), I would say that the original example by Adam may be correct, after all.
Gorpik
True, the question is ambiguous; but that's no excuse for an answer to use status in contradictory ways.
Roger Pate
@Roger, loved the "losing an eye" smiley.
paxdiablo
+2  A: 

"I am trying to have the for loop break early if status is true. " The preferred way to do this is with an if statement in the body of the for loop.

for (int i = 0; i < 3; i++) { if(status) break; }

mothis
A: 

Syntactically correct yes. But the meaning is different. The first piece of code, run your for loop while status is equal to 1. The second piece of code, run only the for loop for 10 times and then exits.

Amokrane
+2  A: 

I suspect the following is syntactically correct:

for (int i = 0; i < 3; i++ && status)

but its probably not what you mean. As pointed out by Adam, you probably want:

for (int i = 0; i < 3 && status; ++i)

This has all the meaning you want and all the details of the loop conditions are in the for statement.

The alternative form:

for (int i = 0; i < 3; i++) {
     if (!status) { break; }
}

is useful if you want some code before or after the if, but the if becomes less visible to the maintenance programmer.

Note: With status in the for statement, the loop may never run if status is false.

quamrana
A: 

Instead of setting status to true you should just use "break".