Thread: switch-case,Infinite while-loop ,break

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    32

    switch-case,Infinite while-loop ,break

    hi
    If a switch-case is inside a while-loop how to get out of while-loop without using if- conditional statement(using break inside switch gets only out of switch).
    Last edited by babu198649; 09-19-2008 at 11:54 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use additional flag in the while condition
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Throwing an exception would certainly get you out of everything until you reach a catch statement, but that's probably not what you want.
    Goto would work, but in 99.999% of the time, it's very bad to use, especially for beginners.
    Why don't you want to use an if statement after the switch to break out of the while loop?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    32
    Why don't you want to use an if statement after the switch to break out of the while loop?
    If there are n-cases in a switch,at each time in a loop ,one of the cases is executed,after all the n-cases are executed,if the default-case takes the control out of the while-loop ,the code would be cleaner than using a if statement after the switch.

    use additional flag in the while condition
    Good idea.
    Last edited by babu198649; 09-20-2008 at 12:11 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Or put the whole while-loop in a seperate function and simply return from the particular place inside the switch.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, I would say that the only common case that goto seems reasonable to use is when you want to break from more than one "block" of code, since there is no such think as break2 etc etc. But I won't say anything, I don't use goto my self either....

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Well, I would say that the only common case that goto seems reasonable to use is when you want to break from more than one "block" of code, since there is no such think as break2 etc etc. But I won't say anything, I don't use goto my self either....
    That is certainly a sort of valid use of goto, but I would thinkt that setting a flag to break the loop would work just as well, and make it at least as obvious what is going on.

    If there is some processing outside of the switch, perhaps a "breakflag" is most appropriate, e.g.
    Code:
    while(true)
    {
        ... some code
        switch(x)
        {
           case ...:
               ...
               if (y) breakflag = true;
               else {
                  ...
                  .... 
               }
               break;
           ... // more cases and default
        }
         if (breakflag)
             break;   // Ends while-loop. 
         ... More code here. 
    }
    In some ways, a goto may be more obvious, since you at least see the label.

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by matsp View Post
    If there is some processing outside of the switch, perhaps a "breakflag" is most appropriate, e.g.
    Code:
    while(true)
    {
        ... some code
        switch(x)
        {
           case ...:
               ...
               if (y) breakflag = true;
               else {
                  ...
                  .... 
               }
               break;
           ... // more cases and default
        }
         if (breakflag)
             break;   // Ends while-loop. 
         ... More code here. 
    }
    In some ways, a goto may be more obvious, since you at least see the label.

    --
    Mats

    --
    Mats
    Or perhaps

    Code:
    while(true && !(breakflag))
    Instead of the 'if'
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Neo1 View Post
    Or perhaps

    Code:
    while(true && !(breakflag))
    Instead of the 'if'
    which can be replaced with while(!breakflag) which is exactly what I would prefer, but my point was that if you have a fair bit of code AFTER the end of the switch statement, the if(breakflag) break will break the while-loop before that code, rather than wrapping the whole lot of code in a if(!breakflag).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to loop in switch??
    By pczafer in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2009, 01:53 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  4. can (switch) be apart of a loop?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 06:29 PM