Swich/Case Question

This is a discussion on Swich/Case Question within the C Programming forums, part of the General Programming Boards category; Is there any way to make a switch/case statement work for values that aren't known at runtime. I don't see ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 12:47 AM

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