0% found this document useful (0 votes)
28 views

Lesson 5-Switch Case

Switch case statements provide an alternative to long if-else statements when comparing an integral value. A switch statement contains a value to compare and multiple case statements that specify code to execute if the value matches. The break keyword is used to exit each case block and prevent code from executing in subsequent cases. A default case can optionally handle any values not matched in the other cases. Switch statements allow simple processing of user input or other integral values.

Uploaded by

Jorax
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lesson 5-Switch Case

Switch case statements provide an alternative to long if-else statements when comparing an integral value. A switch statement contains a value to compare and multiple case statements that specify code to execute if the value matches. The break keyword is used to exit each case block and prevent code from executing in subsequent cases. A default case can optionally handle any values not matched in the other cases. Switch statements allow simple processing of user input or other integral values.

Uploaded by

Jorax
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lesson 5: switch case

Switch case statements are a substitute for long if statements that compare to an integral
value. The basic format for using switch case is outlined below.
switch ( value ) {
case this:
Code to execute if value == this
break;
case that:
Code to execute if value == that
break;
...
default:
Code to execute if value != this or that
break;
}

The condition of a switch statement is a value. The case says that if it has the value of
whatever is after that case then do whatever follows the colon. The break is used to break
out of the case statements. Break is a keyword that breaks out of the code block, usually
surrounded by braces, which it is in. In this case, break prevents the program from falling
through and executing the code in all the other case statements. An important thing to
note about the switch statement is that the case values may only be constant integral
expressions. Sadly, it isn't legal to use case like this:
int a = 10;
int b = 10;
int c = 20;
switch ( a ) {
case b:
// Code
break;
case c:
// Code
break;
default:
// Code
break;
}

The default case is optional, but it is wise to include it as it handles any unexpected cases.
Switch statements serves as a simple way to write long if statements when the
requirements are met. Often it can be used to process input from a user.
Below is a sample program, in which not all of the proper functions are actually declared,
but which shows how one would use switch in a program.

#include <iostream>
using namespace std;
void playgame();
void loadgame();
void playmultiplayer();
int main()
{
int input;
cout<<"1. Play game\n";
cout<<"2. Load game\n";
cout<<"3. Play multiplayer\n";
cout<<"4. Exit\n";
cout<<"Selection: ";
cin>> input;
switch ( input ) {
case 1:
// Note the colon, not a semicolon
playgame();
break;
case 2:
// Note the colon, not a semicolon
loadgame();
break;
case 3:
// Note the colon, not a semicolon
playmultiplayer();
break;
case 4:
// Note the colon, not a semicolon
cout<<"Thank you for playing!\n";
break;
default:
// Note the colon, not a semicolon
cout<<"Error, bad input, quitting\n";
break;
}
cin.get();
}

This program will compile, but cannot be run until the undefined functions are given
bodies, but it serves as a model (albeit simple) for processing input. If you do not
understand this then try mentally putting in if statements for the case statements. Default
simply skips out of the switch case construction and allows the program to terminate
naturally. If you do not like that, then you can make a loop around the whole thing to
have it wait for valid input. You could easily make a few small functions if you wish to
test the code.

You might also like