switch() inside while()

This is a discussion on switch() inside while() within the C Programming forums, part of the General Programming Boards category; hello all, say for example I have a switch() inside of a while(), and in one the the cases of ...

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    switch() inside while()

    hello all,
    say for example I have a switch() inside of a while(), and in one the the cases of my switch(), I use a break statement:

    Code:
    while(blah) {
    	switch(blah) {
    	case BLAH:
    		break;
    	}
    }
    that would break from the while loop, would it not? I'm in a situation here, because I don't know how I can break the case without breaking the loop. my cases are all very different, I must break from each of them somehow (execution can't "fall through"). I am using the switch() statement instead of a bunch of else ifs, which I would like to avoid using as much as possible (I have alot of conditions to test against).

    I am thinking maybe I can change the loops conditional test (to something that will remain true after each case) and use a continue at the end where I would use the break in the case to jump to the loops conditional test and continue with the switch() (if of course the loop's test is still TRUE). but I am wondering if the continue will work on the loop if I place it inside a switch case like that?

    if anyone could please help me here, it would be appreciated. thankyou in advance!

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > that would break from the while loop, would it not?
    No it wouldn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    run this code to understand better

    Code:
    #include<stdio.h>
    
    int main()
    {
    	int ch;
    
    	printf("Enter 4 to exit\n");
    
    	while((ch=getchar())!=4)
    	{
    		switch(ch)
    		{
    			case 1:
    				printf("Inside the case 1\n");
    				break;
    			case 2:
    				printf("Inside the case 2\n");
    				break;
    			case 3:
    				printf("Inside the case 3\n");
    				break;
    			default:
    				printf("You entered %d but still inside the switch\n",ch);
    		}
    		printf("Enter 4 to exit\n");
    	}
    	printf("Out of While loop\n");
    	return 0;
    }
    ssharish2005
    Last edited by ssharish2005; 04-22-2006 at 08:08 PM.

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    while(blah) {
    	switch(blah) {
    	case BLAH:
    		break;
    	}
    }
    The break and continue statements will work only for their directly related process. You can also break out of nested functions too.


    Code:
    while( 1 ) {
    	for ( i = 0; i < 10; i++ )
    	{
    		switch(blah) {
    			case 1:
    			break; //break out of the switch
    			
    			case 2:
    			break;
    			break; //break from the switch and the for
    			
    			case 3:
    			break;
    			break:
    			break; //break from the switch, for and while
    			}
    			
    	}
    }
    Last edited by bivhitscar; 04-22-2006 at 10:29 PM.

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    really? dunno why I thought it would break the while(), I remember reading somewhere that a break inside any loop nesting will break out of the closest loop's block.

    anyways, good to know that that will not break my loop, and here I was doing it the hard way (I got it to work with continue, but just using break would be a better idea IMO).

    thanks guys!

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    >I remember reading somewhere that a break inside any loop nesting will break out of the closest loop's block.

    Make that "closest loop or switch" and it would be correct.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    case 2:
    break;
    break; //break from the switch and the for
    Incorrect. The second break statement will never be reached since the first break statement transfers execution to the end of the switch block.

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Thanks bithub. For some reason I really thought I read that somewhere. If I did, it was obviously wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 09:29 PM
  2. A nested if inside the switch
    By Extropian in forum C Programming
    Replies: 20
    Last Post: 08-15-2005, 01:23 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 05:40 PM
  4. switch{}
    By Draco in forum C Programming
    Replies: 1
    Last Post: 05-04-2003, 10:11 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

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