Results 1 to 12 of 12

Thread: how can I break out of an if statement?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    how can I break out of an if statement?

    ?????
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how can I break out of an if statement?

    There is no breaking out of an If statement. You simply put the remainder of the code within that If into another, nested If statement. You would have to test some condition to know whether to break out anyway, and how would you do that if not with an If statement.

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: how can I break out of an if statement?

    BLEH
    thats bad
    cant you use break in c++????
    I just dislike having many many if statements inside each other
    if I could use "break" then it would just be a short if-statement breaking out (rather than wrapping a whole chunk of code in an if block )
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how can I break out of an if statement?

    Break is for loops or Switch only. Generally speaking you would test a condition to determine whether to break out of a loop, which means that the Break statement would be inside an If statement. It wouldn't be much help if break just exited the If.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how can I break out of an if statement?

    Actually, there is a way that I just thought of. You'll like this. It's goto, which is supported by C# as well VB.NET.

    Edit:
    And C++, and presumably C (edit: which I just confirmed by checking the index of my C text book from university, after blowing the dust off).
    Last edited by jmcilhinney; Jul 18th, 2005 at 01:13 AM.

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: how can I break out of an if statement?

    hehe yeah I thought of goto
    but don't you think it'd be a bad idea? Everyone considers it "evil" I've always heard I shouldn't use goto statements eh
    that's the only solution apparently
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: how can I break out of an if statement?

    Only its misuse is evil. Tends to cause speghetti code if you have 25 of them in a 200 line program.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how can I break out of an if statement?

    The goto statement is considered "evil" because people abused it. It was included in programming languages in the first place for a reason, but undisciplined programmers used it wherever they felt like it instead of looking for the best solution. While I get an uneasy feeling myself saying this, if goto is used in only isolated situations then there is no potential for spaghetti code, which is the reason that goto got a bad name. As long as you use other solutions in situations where they are more appropriate, the occasional goto is not a mortal sin.

  9. #9
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: how can I break out of an if statement?

    Break out of an If statment is just as evil or not evil as using a Goto. It is unlogical, and breakes the normal flow of the app. Nothing more or less then that....

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how can I break out of an if statement?

    I haven't tested this but it just occurred to me that the goto statement might be the answer to one of your earlier threads regarding falling through cases in a switch statement. That is more likely to be considered an abuse of goto than this current application though.

  11. #11

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: how can I break out of an if statement?

    Quote Originally Posted by jmcilhinney
    The goto statement is considered "evil" because people abused it. It was included in programming languages in the first place for a reason, but undisciplined programmers used it wherever they felt like it instead of looking for the best solution. While I get an uneasy feeling myself saying this, if goto is used in only isolated situations then there is no potential for spaghetti code, which is the reason that goto got a bad name. As long as you use other solutions in situations where they are more appropriate, the occasional goto is not a mortal sin.
    aha good explanation
    I think I'll use goto for now

    I was trying to break out of it instead of creating a new if block because the if block indents the code and I alreayd have too much indentations (I have too many loops and if statements inside another). I guess the real answer is that I should break my function down into smaller functions to make it simpler

    thanks for all the replies
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  12. #12
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: how can I break out of an if statement?

    Quote Originally Posted by MrPolite
    I guess the real answer is that I should break my function down into smaller functions to make it simpler

    I was actualy just going to suggest that when I saw you did it your self. If you have so much logical branching that you have no room for more indentation, then it is definitly time to break up the function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width