Thread: infinite while loop inside a switch statement.

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    9

    infinite while loop inside a switch statement.

    Does any one has any idea if the while statement in the following code is ever executed.

    Code:
    switch XYZ
    {
       while(1)
       {
         case X:
              -------
              -------
              return p;
         
          case Y:
              -------
              -------
              return q;
        }
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If that is representative of your real code, then anything that isn't X or Y when you enter the switch, will just loop around within the while-loop, since there is no way out of that loop other than in the case X and case Y options.

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

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    9
    ok, I understood.
    Now my problem is I am getting a warning when compiling this code.
    "loop is not reachable from preceding code".
    Is there a way to fix this warning? Any ideas??

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are you ACTUALLY trying to achieve? Normally, you do not put a switch, then a while, and then case-labels inside that, so it would help to understand what you want to achieve...

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

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This compiles, runs and produces expected output. I don't see what would be the purpose of that, though.

    Code:
    #include <stdio.h>
    
    int foo(int n)
    {
        switch (n) {
            while (1) {
                case 1:
                    return 2;
                case 2:
                    return 4;
                case 3:; //a case that doesn't return
            }
        }
        return 7;
    }
    
    int main()
    {
        printf("&#37;d %d %d %d", foo(1), foo(2), foo(3), foo(4));
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it is VALID to do that. There is an example of a piece of code that puts case-labels in an if-statement in the Samuel P. Harbison "C - A reference manual" [at least, there was one in my older copy].

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

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yes but u have to declare the return values if u dunt want one of them to break out of the loop
    but i prefer forever loops for infinite loops

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  2. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  3. switch statement inside a do while
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:33 PM
  4. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  5. can (switch) be apart of a loop?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 06:29 PM