syntax question

This is a discussion on syntax question within the C Programming forums, part of the General Programming Boards category; Hi!
Is there any way to use 'break', 'continue', and 'return' statements in an if-statement of the form "expr ? ...

No, I'm pretty sure you can't. You have to remember that ?: is an operator and is therefor a function. The values around the operator being the arguements. Have you ever seen a function take an arguement like "return suchandsuch"?

We have already met break in the discussion of the switch statement. It is used to exit from a loop or a switch, control passing to the first statement beyond the loop or a switch.

With loops, break can be used to force an early exit from the loop, or to implement a loop with a test to exit in the middle of the loop body. A break within a loop should always be protected within an if statement which provides the test to control the exit condition.

The continue Statement

This is similar to break but is encountered less frequently. It only works within loops where its effect is to force an immediate jump to the loop control statement.

* In a while loop, jump to the test statement.
* In a do while loop, jump to the test statement.
* In a for loop, jump to the test, and perform the iteration.

Like a break, continue should be protected by an if statement. You are unlikely to use it very often.

Definition: A break statement is used to exit a for, switch, while or do-while statement. Execution continues following the statement. With nested loops or switches, if a break statement is used in an inner loop the outer loop is uneffected. The break terminates only the inner loop.

continue

Related Terms
* break
* do while
* for
* for
* while

Definition: A continue statement terminates the current iteration of its innermost enclosing loop. Execution continues with the evaluation of the loop condition. Compare this to break, which terminates the loop.

Consider the following example where we read in integer values and process them according to the following conditions. If the value we have read is negative, we wish to print an error message and abandon the loop. If the value read is great than 100, we wish to ignore it and continue to the next value in the data. If the value is zero, we wish to terminate the loop.

Don't be stupid Q, there was no mention of loops in this discussion until I posted them. The question was about IF, no indication of LOOP. If you had mentioned them, I would have conceeded...

Maybe posters should clarify what they mean, including senior members/experts?

I didn't need to mention them. All you need to do is learn how to read:

Originally Posted by WaltP

break and continue are not allowed in if statements

See that? You say that you cannot use a continue in an if statement. I told you to clarify it, because that's blatantly incorrect. You can use them in an if statement. If you couldn't, you're be pretty much ........ed, because how else would you break from this loop:

Code:

for( x = 0; x < 10; x++ )
{
/* if x is 3, break from this loop */
}

Oh, I suppose you could do it with a switch. But that'd be pretty stupid, when you can use it in an if and do it.

But wait! Walt just told us we can't use it in an if!? You mean... I'm so confused! *head explodes*

See dumb ........? That's why I told you to clarify it. You most certainly can use it in an if.

[edit]
Furthermore, and this is just to further prove my point, if you have a simple if you really really want to break from using break, you can do it.

What the hell is with the attack? If you don't like what I said, simply correct me respectfully and quit with the flames! I'm big enough to accept correction esp. if I've misread or misunderstood a post. But I don't need to be called names from an arrogant SOB that has to prove he's better than the rest of the world. Then posting obfuscated code to prove your point....

No flame to OnionKnight or are you singling me out for some reason?

And after 900+ posts why this disrespect all of a sudden? What did I do that hasn't been done before by others that had to make it personal?