Menu

Little SWITCH doubt

2002-12-24
2012-09-26
  • Nobody/Anonymous

    When working with a SWITCH, the "default" label will ALWAYS be executed or it will only be executed if theres no other CASE match?

    Example:

    int a=1;
    switch(a) {
      case a=1:
      something;
      case a=2:
      something;
      case a=3:
      something;
      default:
      somethingelse;
    }

    One more, in the above example, as theres no BREAK inside any of the cases, my program will try to match "a" against the second and he third cases and probably the default too?

     
    • qWake

      qWake - 2002-12-24

      Ouch!

      You need to review the syntax for the switch statement.  Yes, you should have a break at the end of each clause unless you INTEND for the code to fall through.  But also your 'case' clauses should look like 'case 1:', not 'case a=1:'

      qWake

       
    • Nobody/Anonymous

      switch(cases)     // ............ start switch
      {
      case 1: // ..... Checks for first value
         sfunct(); // ... calls the function
        break; // If value one then ends here
      case 2: // ..... Checks for second value
         mfunct(); // ... calls the function
        break; // If value two then ends here
      case 3: // ..... Checks for thrid value
         dfunct(); // ... calls the function
        break; // If value three then ends here

      default: // If not any of the above then this massage will print to screen.
        cout << "The Number  "<< system << " Is not a Function. \n" ;
      break;
      }               // ........................ end switch

      N@N!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.