Hi!
Is there any way to use 'break', 'continue', and 'return' statements in an if-statement of the form "expr ? t : f"? My compiler complains when I try this.
Hi!
Is there any way to use 'break', 'continue', and 'return' statements in an if-statement of the form "expr ? t : f"? My compiler complains when I try this.
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"?
Sent from my iPadŽ
Ah I see. I thought it was translated like a normal if-statement at compile time. But that explains it. Thanks
I don't think it's a good idea to imply that an operator is a function, it's not.Originally Posted by SlyMaelstrom
only in functional languages can it be considered a function.
In something like C it's a hard coded operator.
break and continue are not allowed in if statements. They are used in loops.Originally Posted by cyph1e
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
You really should clarify that, because it's wrong.Quzah.Code:if( foo == bar ) break; else if( foo == baz ) continue;
Hope is the first step on the road to disappointment.
Really?
Error E2030 x.c 8: Misplaced break in function mainCode:#include <stdio.h> int main() { int foo=1, bar=1, baz=1; if( foo == bar ) break; else if( foo == baz ) continue; return 0; }
Error E2033 x.c 11: Misplaced continue in function main
*** 2 errors in Compile ***
Do you know something you're not telling us? Or that these sites don't know?
http://www.imada.sdu.dk/~svalle/cour...2005/mirror/c/
The break Statement
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.
==================================
http://cplus.about.com/od/cprogrammi...lglossaryb.htm
break
Related Terms
* do while
* continue
* for
* for
* while
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.
==================================
http://www.lysator.liu.se/c/bwk-tutor.html#switch
==================================
http://www.cs.cf.ac.uk/Dave/C/node6....00000000000000
break and continue
C provides two commands to control how we loop:
* break -- exit form loop or switch.
* continue -- skip 1 iteration of 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.
Last edited by WaltP; 03-30-2006 at 02:45 AM.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
Oh, I see. So you're saying the only way to use a break is this:That's funny Walt. I'm sure you're allowed to do this:Code:for( x = 0; x < 10; x++ ) { continue; }Don't be stupid Walt. Why the ........ do you think I told you to clarify it?Code:for( x = 0; x < 10; x++ ) { if( x == 5 ) continue; }
Quzah.
Hope is the first step on the road to disappointment.
Edit: Too slow, but I've typed it all out now
Obviously quzah is talking about using break in an if inside a loop or switch statement, something like:
or:Code:for (x = 0; x < 100; x++) if (a[x] = 'c') break;
Code:switch (x) { case 1: if (FOO) break; a = 5; break; case 2: /* ... */ }
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...Originally Posted by quzah
Maybe posters should clarify what they mean, including senior members/experts?
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
You cannot break an if-statement. You can workaround it by nestling another if-statement.
becomesCode:if (x) { x += y; if (x > z) break; x++; y--; }
Code:if (x) { x += y; if (x <= z) { x++; y--; } }
I didn't need to mention them. All you need to do is learn how to read:Originally Posted by WaltPSee 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:Originally Posted by WaltPOh, 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.Code:for( x = 0; x < 10; x++ ) { /* if x is 3, break from this loop */ }
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.Oh, look. I broke from an if. No errors or warnings. Looks like you were wrong again. Dumbass.Code:#include<stdio.h> int main( void ) { int foo = 0, bar = 1, baz = 2; do if ( foo == bar ) break; else if ( foo == baz ) continue; while ( 0 ); return 0; }
[/edit]
Quzah.
Last edited by quzah; 03-30-2006 at 04:51 AM.
Hope is the first step on the road to disappointment.
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?
Last edited by WaltP; 03-30-2006 at 10:57 AM.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
Singling? Just trying to answer the question the way i interpret it.