Thread: Swich/Case Question

  1. #1
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107

    Swich/Case Question

    Is there any way to make a switch/case statement work for values that aren't known at runtime. I don't see why it couldn't work with variables, but my complier only seems to allow constants. I'm not picky about the method that's used but I'd perfer it would be something a little more efficient than and endless bunch of if/elses, which is the only thing I could come up with as an alternative.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Xzyx987X,
    Is this what you mean?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
                    /* use a character as the switch variable */
    	char c = '1';
    	unsigned int done = 0;
    
    	while (!done)
    	{
    		switch (c)
    		{
    		case '1':
    			printf("case 1\n");
    			c = '2';
    			break;
    
    		case '2':
    			printf("case 2\n");
    			c = '4';
    			break;
    
    		case '3':
    			printf("case 3 (doesn't print)\n");
    			c = '4';
    			break;
    
    		case '4':
    			printf("case 4\n");
    			c = '5';
    			break;
    
    		default:
    			done = 1;
    			break;
    		}
    	}
    }
    
    
    /*   similar to above except the switch variable is an integer
    
    int main(void)
    {
    	unsigned int x = 1;
    	unsigned int done = 0;
    
    	while (!done)
    	{
    		switch (x)
    		{
    		case 1:
    			printf("case 1\n");
    			x++;
    			break;
    		case 2:
    			printf("case 2\n");
    			x = 4;
    			break;
    		case 3:
    			printf("case 3 (doesn't print)\n");
    			x++;
    			break;
    		case 4:
    			printf("case 4\n");
    			x++;
    			break;
    		default:
    			done = 1;
    			break;
    		}
    	}
    }
    
    */

  3. #3
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    No, not really. What I meant was something like this, only that worked:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    
    	int n = rand() % 10;
    	int i;
    	int numArray[4];
    
    	for(i = 0; i < 4; i++)
    		numArray[i] = rand() % 10;
    
    	switch (n)
    	{
    	case numArray[0]:
    		printf("case 1\n");
    		break;
    
    	case numArray[1]:
    		printf("case 2\n");
    		break;
    
    	case numArray[2]:
    		printf("case 3\n");
    		break;
    
    	case numArray[3]:
    		printf("case 4\n");
    		break;
    	}
    
    	return 0;
    
    }
    Edit: One thing I'm definately aware of is that this would problbly take some dynamically generated ASM code to do, so don't tell me this is impossible just because it can't be done in pure C :P.
    Last edited by Xzyx987X; 05-05-2004 at 12:23 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You would need to use if/else type logic for this
    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.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    An alternative approach
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     
     #define SIZE  10
     
     int main(void)
     {
       int n;
       int i;
       int numArray[SIZE];
     
       srand(time(NULL));
       
       n = rand() % 10;
       
       for (i = 0; i < SIZE; i++)
       {
         numArray[i] = rand() % 10;
       }
     
       printf("Looking for %d\n", n);
       for (i = 0; i < SIZE; i++)
       {
         printf("Checking element %d (value:%d)\n", i, numArray[i]);
         if (numArray[i] == n)
         {
           break;
         }
       }
     
       if (i < SIZE)
       {
         printf("Found %d in element number %d\n", n, i);
       }
       else
       {
         printf("Failed to find it!\n");
       }
     
       return(0);
     }
     
     /* Sample Output:
     Looking for 1
     Checking element 0 (value:9)
     Checking element 1 (value:8)
     Checking element 2 (value:0)
     Checking element 3 (value:1)
     Found 1 in element number 3
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM