Thread: Switch statements??

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    Switch statements??

    Hi,
    Can I use an if else statement withing a Case??
    e.g Case 'C': if(*roundHr < 3)
    .
    .
    .
    .
    .
    etc
    how could I implement that??
    Thanks
    A

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    switch (c)
    {
    	case 'A':
    		if (something == TRUE)
    		{
    			printf("blurb\n");
    		}
    		else
    		{
    			printf("No blurb\n");
    		}
    		break;
    	case 'B':
    	 .....
    	default:
    	 .....
    }
    Don't put too much inside one case though, your program will get confusing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int choices ( void );
    
    int main ( void )
    {
            int Number = 100;
            char Choice;
            while( choices() == 0 )
            {
                    Choice=getch();
                    switch(Choice)
                    {
                            case 'n':
                            if(Number == 100)
                            {
                                    printf("%d\n\n", Number);
                            }
                            else
                            {
                                    printf("You changed Number's value!\n\n");
                            }
                            break;
                    
                            case 27:
                            exit(0);
                            break;
                            
                            default:
                            break;
                    }
            }
            return 0;
    }
    
    int choices ( void )
    {
            printf("N = show number\nEscape = exit\n\n");
            return 0;
    }
    The world is waiting. I must leave you now.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Don't put too much inside one case though, your program will get confusing.
    I agree.
    Functions! functions! functions!
    The world is waiting. I must leave you now.

  5. #5
    Frankiepoon
    Guest

    Talking I agree!!

    Use more function and pointer!!I think it is very important for the speed o fprogram....

  6. #6
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb Ummm...

    People I don't quite get what you mean by function pointers.

    Well, I do use a switch statement like this:

    /*Start sample*/

    void first(void);
    void second(void);
    void third(void);

    ...

    switch(choice) {
    case '1': first(); break;
    case '2': second(); break;
    case '3': third(); break;
    default: break;
    }

    /*End Sample*/

    I don't expand processes inside switch statements.

    So what do you think about this style?
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: Ummm...

    Originally posted by billholm
    People I don't quite get what you mean by function pointers.
    Dont know if he actually meant function pointers........but you could do something like...

    Code:
    #include <stdio.h>
    void first(void){printf("First\n");}
    void second(void){printf("Second\n");}
    void third(void){printf("Third\n");}
    typedef void (*MYFUNC)(void);
    
    int main(int argc, char *argv[])
    {
      MYFUNC MyFunc = NULL;
      int x = 0;
    
      while(x!=4){
      printf("Enter 1,2 or 3 (4 to end)\n");
    
      scanf("%d",&x);
    
      if(x == 4)break;
    
      switch(x){
      case 1:
           MyFunc = (MYFUNC)first;
           break;
      case 2:
           MyFunc = (MYFUNC)second;
           break;
      case 3:
           MyFunc = (MYFUNC)third;
           break;
      }
    
      MyFunc();
      }
      return 0;
    }

  8. #8
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Just for fun......
    Code:
    #include <stdio.h>
    
    void first(void){printf("First\n");}
    void second(void){printf("Second\n");}
    void third(void){printf("Third\n");}
    void (*func[])(void) = { first, second, third };
    
    int main(int argc, char *argv[])
    {
      int x = 0;
    
      while(x!=4)
      {
        printf("Enter 1,2 or 3 (4 to end)\n");
        scanf("%d",&x);
        if(x == 4) break;
        if(x > 0 && x < 4) func[x-1]();
      }
      return 0;
    }

  9. #9
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Lightbulb

    Well I didn't know that there are other techniques like those... till today that is.

    I think I'll practice using those. It could actually shorten some of the codes i have already written.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In my contest entry, I did a menu based on function pointers, similar to Monsters version, but slightly more dynamic (It worked out the number of items in the menu by itself).

    Here's a section of the code that made it work:

    Code:
    struct menu_item
    {
    	char	*Text;
    	void 	(*fptr) (void *);
    	void 	*args;
    };
    
    static struct menu_item Page2Menu[] =
    {
    	{ "Search By Chosen Field", ui_DisplayMenu, SearchByFieldMenu },
    	{ "Display All Entries", ui_DisplayAll, NULL },
    	{ "Set Sort Order", ui_DisplayMenu, SortByFieldMenu },
    	{ "Undelete An Entry", ui_UndeleteEntry, NULL },
    	{ "Save Changes", ui_Save, NULL },
    	{ "Purge Entries Marked For Deletion", ui_PurgeDeleted, NULL },
    	{ "Application Options", ui_DisplayMenu, OptionsMenu },
    	{ NULL, NULL, NULL }
    };
    
    struct menu_item MainMenu[] =
    {
    	{ "Add New Entry", ui_AddNewEntry, NULL },
    	{ "Delete Entry", ui_DeleteEntry, NULL },
    	{ "Update Entry", ui_UpdateEntry, NULL },
    	{ "Display Entry", ui_SearchAll, (void *)RECF_ID},
    	{ "Search By Name", ui_SearchAll, (void *)RECF_NAME },
    	{ "Save Changes", ui_Save, NULL },
    	{ "More Options", ui_DisplayMenu, Page2Menu },
    	{ "Help/About", ui_ShowInfo, NULL },
    	{ NULL, NULL, NULL }
    };
    
    void ui_DisplayMenu(void *pmenu)
    {
    	int i, choice;
    	struct menu_item *menu = pmenu;
    	struct menu_item *item;
    
    	for ( ; ; )
    	{
    		printf ("---> PhoneBook Menu <---\n");
    		for (i = 1, item = menu; item->Text != NULL; item++, i++)
    		{
    			printf("%2d %s\n", i, item->Text);
    		}
    
    		printf(" x Exit Menu\nEnter choice >");
    
    		if ((choice = io_GetInt(i-1)) == -1)
    			break;
    		item = menu + choice - 1;
    		if (item->Text == NULL) break;
    		(void) (*item->fptr) (item->args);
    	}
    }
    The io_GetInt() function is in another library, but all it does is ask the user for an int between 1 and the value in the parameter you pass. If they entered the letter x, the function would return -1, and we'd break out of the infinite loop in menu.

    The full source is in a zip file on the contest thread if you want to look at this in more detail.
    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. 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. Switch Statements
    By blackgingr in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 02:36 PM
  5. Switch statements for strings
    By cxs00u in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2002, 03:38 PM