Friday, January 13, 2012

The Cplusplus Programming Language Switch Statement

I orignally published this article on Yahoo Voices under the pen name John Mario.

This Tutorial covers the C++ switch statement.

The basic form of the switch statement is shown below. The names of the variables in this switch statement describe their function. The case_identifier_value defines which case will be executed. Each case begins with a case statement ends with a break statement as shown below. Each time the switch statement is called, only one case is executed. The C++ statements between the case statement and the break statement comprise the response for that particular input. The case_identifier_value can be an integer or a letter of the alphabet. If the switch statement receives an invalid case_identifier_value, the default case is executed. The default case starts with 'default:' and ends with a break statement.

BASIC FORM OF SWITCH STATEMENT

switch (case_identifier)

{

case(case_identifier_value) // case for a specific value of case_identifier_value

// case_identifier_value value must be number or letter

//C++ statements for responding to a specific case_identifier_value

break; // end of case

break;

// one case exists for each

// valid case_identifier_value

default:

// The default case contains C++ statements

// that respond to

// invalid case_identifier_values

break; //end of default case

}

FIRST C++ PROGRAM CONTAINING SWITCH STATEMENT

A sample program containing a switch statement is shown below. In this program, the switch statement excutes a case based on an integer.

// place include iostream statement here

using namespace std;

int main(void)

{

int count; //declare the variable count

cout << "Type a number less than 6 " << endl; //instruct user

cin >> count; // await keyboard entry

switch(count)

/******************************

This switch statement executes

one of the cases below based on the

value of count.

Note that each time the switch statement

is executed only one case is executed

or the default case is executed.

******************************/

{

case 0: //If count equals 0, this case is executed

cout << "count = " << count << endl; //display count of 0

break; // exit from switch statement

case 1: //If count equals 1, this case is executed

cout << "count = " << count << endl; //display count of 1

break; // exit from switch statement

case 2: //If count equals 2, this case is executed

cout << "count = " << count << endl; // display count of 2

break; // exit from switch statement

case 3: //If count equals 3, this case is executed

cout << "count = " << count << endl; //display count of 3

break; // exit from switch statement

case 4: //If count equals 4, this case is executed

cout << "count = " << count << endl; // display count of 4

break; // exit from switch statement

case 5: //If count equals 5, this case is executed

cout << "count = " << count << endl; // display count of 5

break; // exit from switch statement

default: // default case: execute if count is not between 1 and 5

cout << "count out of range" << endl; // display count out of range

break; //exit switch statement

} //end of switch statement

} // end of main

SECOND C++ PROGRAM USING SWITCH STATEMENT

The switch statement can also execute cases based on a character input. In the following program we use the switch statement to accept one of the following letters: a, b, c, d, e.

//place include iostream statement here

using namespace std;

int main(void)

{

char answer; //declare the variable answer

cout << "Type in one of the first five letters of the alphabet" << endl;

cin >> answer; // await keyboard entry

switch(answer) //Execute case based on answer

// Note that each time the switch statement

// is executed only one case is executed

// or the default case is executed.

{

case 'a': //If answer equals 'a', this case is executed

cout << "answer = a" << endl; //display answer

break; // exit from switch statement

case 'b': //If answer equals 'b', this case is executed

cout << "answer = b" << endl; // display answer

break; // exit from switch statement

case 'c': //If answer equals 'c', this case is executed

cout << "answer = c" << endl; //display answer

break; // exit from switch statement

case 'd': //If answer equals 'd', this case is executed

cout << "answer = d" << endl; // display answer

break; // exit from switch statement

case 'e': //If answer equals 'e', this case is executed

cout << "answer = e" << endl; // display answer

break; // exit from switch statement

default: // default case: execute if answer is not a, b, c, d or e

cout << "invalid answer" << endl; // display error message

break; // end of default case

} //end of switch statement

} //end of main function

A PRACTICAL USE OF THE C++ SWITCH STATEMENT

The following is a practicle example. The actual C++ statements in each case are replaced with an explanation of what those C++ statements do.

//Place include iostream statement here

using namespace std;

int main(void)

{

int command_input;

cout << "select item number from menu" << endl;

cout << "1 Print list of employees" << endl;

cout << "2 Print list of employee wages" << endl;

cout << "3 Print list of new employees" << endl;

cout << "4 Print list of managers" << endl;

cout << "5 Print list of manager salaries" << endl;

cin >> command_input;

switch (command_input)

{

case 1:

//The C++ statements for this case

// print a list of employees

break;

case 2:

//The C++ statements for this case

//print a list of employee wages

break;

case 3:

//The C++ statements for this case

//prints a list of new employees

break;

case 4:

//The C++ statements for this case

//Prints a list of managers

break;

case (5):

//The C++ statements for this case

//Print a list of manager salaries

default:

//The C++ statements for this case

//responds to the invalid command_input

break;

} //end of switch statement

} //end of main function

References:

My own experiences during my career as a embedded systems software engineer.

The book 'C++ THE COMPLETE REFERENCE' by Herbert Schildt.

ISBN 0-07-222680-3



No comments: