Académique Documents
Professionnel Documents
Culture Documents
1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann
A program that is written using simple control structures including: Sequence Selection Repetition
This is an example of logical expressions. They are used to make assertions. Also called Boolean expressions. Three logical operators: ! && ||
A false false true true
ENGR 1200U Winter 2013 - UOIT
Short-circuit evaluation?
5 or 6?
For simplicity and to minimize number of branches in this example, we will assume that grade < 73 is passing but marginal !
Grade >= 90
true
A+
false
Grade >= 85
true
false
Grade >= 80
true
A-
false
Grade >= 77
true
B+
false
Grade >= 73
true
false
Pass, Marginal
if (grade >=90) (false ) { cout<<"YourlettergradeisA+"; } else if (grade>=85) { cout<<"YourlettergradeisA"; } else if (grade>=80) { cout<<"YourlettergradeisA"; } else if (grade>=77) { cout<<"YourlettergradeisB+"; } else if (grade>=73) { cout<<"YourlettergradeisB"; } else { cout<<"Gradeistoolowtohavealettergrade!"; } ...
If a test is false,
that block is not executed (skipped)
. . .
if (grade>=90) { cout<<"YourlettergradeisA+"; } else if ( (grade >=85) true ) { cout<<"YourlettergradeisA"; } else if (grade>=80) { cout<<"YourlettergradeisA"; } else if (grade>=77) { cout<<"YourlettergradeisB+"; } else if (grade>=73) { cout<<"YourlettergradeisB"; } else { cout<<"Gradeistoolowtohavealettergrade!"; } ...
As soon as one of the 6 tests is successful (i.e. evaluates to true), that block is executed
if (grade >=73) (true ) { cout<<"YourlettergradeisB"; } (true ) else if (grade >=77) { cout<<"YourlettergradeisB+"; } (true ) else if (grade >=80) { cout<<"YourlettergradeisA"; } else if (grade>=85) { cout<<"YourlettergradeisA"; } else if (grade>=90) { cout<<"YourlettergradeisA+"; } else { cout<<"Gradeistoolowtohavealettergrade!"; } ...
ENGR 1200U Winter 2013 - UOIT
Wrong order of tests Suppose grade is 84. Expected output: AActual output: B
this test is true!
That code block is executed!
if () { if () { if () { } } }
When an if statement is nested inside another if statement, the following error can happen: Example
doubleship_rate=5.00;//standardshippingacrossCanada if (Country==Canada) Indentation level if (Province==PEI) suggests that the else is ship_rate=9.00; grouped with the test country ==Canada else ship_rate=30.00;
Sowhatisthesolution?
Example
doubleship_rate=5.00;//standardshippingacrossCanada if (Country==Canada) if (Province==PEI) This is actually how the ship_rate=9.00; code is executed else ship_rate=30.00; This is not what we want.
This is the
Example
Solution:Simple,putone statementinablock{}
The switch statement is used for multiple selection decision making Thisisabitofamess! Example
int digit; ... if (digit==1){digit_name="one";} else if (digit==2){digit_name="two";} else if (digit==3){digit_name="three";} else if (digit==4){digit_name="four";} else if (digit==5){digit_name="five";} else if (digit==6){digit_name="six";} else if (digit==7){digit_name="seven";} else if (digit==8){digit_name="eight";} else if (digit==9){digit_name="nine";} else {digit_name="";}
Example
int digit; ... switch (digit) { case 1:digit_name="one";break; case 2:digit_name="two";break; case 3:digit_name="three";break; case 4:digit_name="four";break; case 5:digit_name="five";break; case 6:digit_name="six";break; case 7:digit_name="seven";break; case 8:digit_name="eight";break; case 9:digit_name="nine";break; default:digit_name="";break; }
Moreorganized
Syntax switch (control_expression) { case constant: statement(s); break; [ case constant: statement(s); break; [] ] [ default: statement(s); ] }
Control expression must be an integral type (e.g. char, int, etc) Break statements are not required; without a break statement execution runs through other case labels.
Every branch of the switch must be terminated by a break statement If the break is missing, execution falls through to the next branch, and so on, until finally a break or the end of the switch is reached If you accidentally forget the break statement, your program compiles but executes unwanted code
10
We would like to print out an appropriate activity depending on the outdoor temperature. Given the following information:
Activity Swimming Tennis Soccer Skiing Other (Study for Exams) Temperature Temperature > 85 70 < temperature <=85 32 < temperature <= 70 0 < temperature <=32 Temperature <= 0
Write a program that prompts the user to enter the outdoor temperature. Then, the program prints out the appropriate activity.
Create a program that prints out the appropriate comment based on a students grade (grade is of type char).
Prompt the user for a letter grade.
Grade Character A B C D F Message Excellent Work Good Work Average Work Poor Work Poor Work
11