Thread: switch statements

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    switch statements

    is there anyway to make a switch statement run forever instead of just breaking if one case is satisfied

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    JUst don't use a break statement?

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Sample Code

    Code:
    #include<stdio.h>
    
    int main()
    {
     	switch(1)
     	{
    	 		 case 1:
    			 	  printf("One\t");
    	 	     case 2:
    			 	  printf("Two\t");
     		     case 3:
    			 	  printf("Three\t");
    	  		 case 4:
    			 	  printf("Four\t");
        }
    
    	getchar();
    	return 0;
    } 
    /* myoutput
    One     Two     Three   Four
    */
    ssharish2005

  4. #4
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    Quote Originally Posted by ssharish2005
    Sample Code

    Code:
    #include<stdio.h>
    
    int main()
    {
     	switch(1)
     	{
    	 		 case 1:
    			 	  printf("One\t");
    	 	     case 2:
    			 	  printf("Two\t");
     		     case 3:
    			 	  printf("Three\t");
    	  		 case 4:
    			 	  printf("Four\t");
        }
    
    	getchar();
    	return 0;
    } 
    /* myoutput
    One     Two     Three   Four
    */
    ssharish2005
    That is called "fall through" and I am not sure if that is what he really wants with "run forever" as this will run one time only.
    However, this will run forever. The switch block is inside of the while block that runs forever.. that gives you a chance to read a new value into n or compute n, etc. Otherwise, what would be the point?
    Code:
    while ( 1 ) {    /* Run forever. . . */
        /* variable n can be computed here, etc. */
        
        switch ( n ) {
        case 0:
            /* code */
            break;
        
        case 1:
            /* code */
            break;
        
        case 2:
            /* code */
            break;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Files Inside Switch Statements
    By arealfind08 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 04:49 PM
  2. Explanation of switch statements
    By ammochck21 in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2006, 02:59 PM
  3. arrays within switch statements
    By divinyl in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2003, 01:56 PM
  4. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM
  5. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM