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

Lec 7

Uploaded by

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

Lec 7

Uploaded by

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

EXAMPLE: WAGES.

CPP

Write a C++ program that calculates weekly wages for hourly


employees.

 Regular hours 0 - 40 are paid at $10/hours.


 Overtime (> 40 hours per week) is paid at 150%

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

 Special features of the switch statement:


 The “break” command at the end of a block of statements
will make the program jump to the end of the switch
 The program executes the statements after the “default”
label if no other cases match the decision variable

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

 The main disadvantage of switch statements is that the


decision variable must be an integer (or a character)
 We can not use a switch with a float or string decision
variable or with complex logical expressions

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

 We can use a switch statements to handle menu


 Ask user for numerical code for user command
 Jump to the code to process that banking operation
 Repeat until the user quits the application

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

cin >> 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)?

 What is wrong with this program?


 We might end up with a negative account balance
 There is only one bank account and zero security

106

SOFTWARE
ENGINEERING TIPS
 There are many ways to write conditional code
 Your task is to find the simplest correct code for the task

 Make your code easy to read and understand


 Indent your program to reflect the nesting of blocks of code

 Develop your program incrementally


 Compile and run your code frequently

 Anticipate potential user input errors


 Check for normal and abnormal input values

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

You might also like