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 ? ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    14

    syntax question

    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.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    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«

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    14
    Ah I see. I thought it was translated like a normal if-statement at compile time. But that explains it. Thanks

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by SlyMaelstrom
    No, I'm pretty sure you can't. You have to remember that ?: is an operator and is therefor a function.
    I don't think it's a good idea to imply that an operator is a function, it's not.

  5. #5
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    only in functional languages can it be considered a function.
    In something like C it's a hard coded operator.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by cyph1e
    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.
    break and continue are not allowed in if statements. They are used in loops.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You really should clarify that, because it's wrong.
    Code:
    if( foo == bar )
        break;
    else
    if( foo == baz )
        continue;
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Really?
    Code:
    #include <stdio.h>
    
    int main()
    {
        int foo=1, bar=1, baz=1;
        
        if( foo == bar )
            break;
        else
        if( foo == baz )
            continue;
    
        return 0;
    }
    Error E2030 x.c 8: Misplaced break in function main
    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 01:45 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oh, I see. So you're saying the only way to use a break is this:
    Code:
    for( x = 0; x < 10; x++ )
    {
        continue;
    }
    That's funny Walt. I'm sure you're allowed to do this:
    Code:
    for( x = 0; x < 10; x++ )
    {
        if( x == 5 )
            continue;
    }
    Don't be stupid Walt. Why the ........ do you think I told you to clarify it?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    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:
    Code:
    for (x = 0; x < 100; x++)
        if (a[x] = 'c')
            break;
    or:
    Code:
    switch (x)
    {
        case 1:
            if (FOO)
                break;
            a = 5;
            break;
        case 2:
            /* ... */
    }

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by quzah
    Oh, I see. So you're saying the only way to use a break is this:
    Code:
    for( x = 0; x < 10; x++ )
    {
        continue;
    }
    That's funny Walt. I'm sure you're allowed to do this:
    Code:
    for( x = 0; x < 10; x++ )
    {
        if( x == 5 )
            continue;
    }
    Don't be stupid Walt. Why the ........ do you think I told you to clarify it?


    Quzah.
    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?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You cannot break an if-statement. You can workaround it by nestling another if-statement.
    Code:
    if (x) {
        x += y;
        if (x > z)
            break;
        x++;
        y--;
    }
    becomes
    Code:
    if (x) {
        x += y;
        if (x <= z) {
            x++;
            y--;
        }
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by WaltP
    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:
    Quote 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.
    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;
    }
    Oh, look. I broke from an if. No errors or warnings. Looks like you were wrong again. Dumbass.
    [/edit]

    Quzah.
    Last edited by quzah; 03-30-2006 at 03:51 AM.
    Hope is the first step on the road to disappointment.

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    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 09:57 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Singling? Just trying to answer the question the way i interpret it.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax question: &(++x)
    By yoshiznit123 in forum C Programming
    Replies: 8
    Last Post: 06-02-2006, 10:40 PM
  2. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21