Lec 7
Lec 7
CPP
CONDITIONAL
STATEMENTS
PART 4
SWITCH STATEMENTS
SWITCH STATEMENTS
The switch statement is convenient for handling multiple
branches based on the the value of one decision variable
The program looks at the value of the decision variable
The program jumps directly to the matching case label
The statements following the case label are executed
92
SWITCH STATEMENTS
switch ( decision variable )
{
case value1 :
// Statements to execute if variable equals value1
break;
case value2:
// Statements to execute if variable equals value2
break;
...
default:
// Statements to execute if variable not equal to any value
}
93
SWITCH STATEMENT
94
SWITCH STATEMENTS
This is how it works:
• The switch expression is evaluated once
• Expression must evaluate to byte, short, char, or int primitive
data type.
• The value of the expression is compared with the values of
each case
• If there is a match, the associated block of code is executed
• The break keyword, breaks out of the switch block.
• The default keyword specifies some code to run if there is no
case match
95
SWITCH STATEMENTS
int num = 0;
cin >> num;
switch (num)
{
case 10:
cout << “Number is equal to 10” << endl; break;
case 25:
cout << “Number is equal to 25” << endl;
break;
96
SWITCH STATEMENTS
case 37:
cout << “Number is equal to 37” << endl;
break;
case 100:
cout << “Number is equal to 100” << endl;
break;
default:
cout << “Number is not equal to 10, 25, 37 or 100”;
}
97
SWITCH STATEMENTS
The main advantage of switch statement over a sequence
of if-else statements is that it is much faster
Jumping to blocks of code is based on a lookup table
instead of a sequence of variable comparisons
98
MENU EXAMPLE
How can we create a user interface for banking?
Assume user selects commands from a menu
We need to see read and process user commands
99
MENU EXAMPLE
// Simulate bank deposits and withdrawals
#include <iostream>
using namespace std;
int main()
{
// Local variable declarations For the first version
// Print command prompt of program we just
write comments in
// Read user input the main program to
// Handle banking command explain our approach
return 0;
}
100
MENU EXAMPLE
…
// Local variable declarations
int Command = 0;
For the next version
// Print command prompt
of program we add
cout << “Enter command number:\n”; the code to read the
// Read user input user command
101
MENU EXAMPLE
// Handle banking command
switch (Command)
{
case 0: // Quit code
break;
case 1: // Deposit code Then we add the
break; skeleton of the switch
statement to handle
case 2: // Withdraw code the user command
break;
case 3: // Print balance code
break;
}
102
MENU EXAMPLE
// Simulate bank deposits and withdrawals
#include <iostream>
using namespace std;
int main()
{
// Local variable declarations
int command = 0;
int amount = 0;
int balance = 100;
// Print command prompt In the final version
cout << “Enter command number:\n” add bank account
<< “ 0 - quit\n” variables and add
<< “ 1 - deposit money\n”
code to perform
<< “ 2 - withdraw money\n”
banking operations
<< “ 3 - print balance\n”;
103
MENU EXAMPLE
// Read and handle banking commands
cin >> command;
switch (command)
{
case 0: // Quit code
cout << “See you later!” << endl;
break;
case 1: // Deposit code
cout << “Enter deposit amount: “;
cin >> amount;
balance = balance + amount;
break;
104
MENU EXAMPLE
case 2: // Withdraw code
cout << “Enter withdraw amount: “;
cin >> amount;
balance = balance - amount;
break;
case 3: // Print balance code
cout << “Current balance = “ << balance << endl;
break;
default: // Handle other values
cout << “Ooops try again” << endl;
break;
}
// Print final balance
cout << “Final balance = “ << balance << endl;
}
105
MENU EXAMPLE
How should we test this program?
Test the code by entering all valid menu commands
What happens if we enter an invalid menu command?
What happens if we enter an invalid deposit or withdraw
amount (e.g. negative input values)?
106
SOFTWARE
ENGINEERING TIPS
There are many ways to write conditional code
Your task is to find the simplest correct code for the task
107
SOFTWARE
ENGINEERING TIPS
Common programming mistakes
Missing or unmatched ( ) brackets in logical expressions
Missing or unmatched { } brackets in conditional statement
Missing break statement at bottom of switch cases
Never use & instead of && in logical expressions
Never use | instead of || in logical expressions
Never use = instead of == in logical expressions
Never use “;” directly after logical expression
108
SUMMARY
In this section we have studied the syntax and use of the
C++ switch statement
We also showed an example where a switch statement
was used to create a menu-based banking program
Finally, have discussed several software engineering tips
for creating and debugging conditional programs
109