default (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Keywords, Alphabetical Listing Index


Category

Statements

Syntax

switch ( <switch variable> ){casebreakdefault
case <constant expression> : <statement>; [break;]
    .
    .
    .
default : <statement>;
}

Description

Use the default statement in switch statement blocks.

  • If a case match is not found and the default statement is found within the switch statement, the execution continues at this point.
  • If no default is defined in the switch statement, control passes to the next statement that follows the switch statement block.


Example

This example illustrates the use of keywords break, case, default, return, and switch.


#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
  char ch;

  cout << "PRESS a, b, OR c. ANY OTHER CHOICE WILL TERMINATE THIS PROGRAM." << endl;
  for ( /* FOREVER */; cin >> ch; )
    switch (ch)
    {
      case 'a' :    /* THE CHOICE OF a HAS ITS OWN ACTION. */
        cout << endl << "Option a was selected." << endl;
        break;
      case 'b' :    /* BOTH b AND c GET THE SAME RESULTS. */
      case 'c' :
        cout << endl << "Option b or c was selected." << endl;
        break;
      default :
        cout << endl << "NOT A VALID CHOICE!  Bye ..." << endl;
        return(-1);
    }
}

See Also