Vous êtes sur la page 1sur 11

ENGR 1200U Introduction to Programming Lecture 10 Control Structures: Selection (Chapter 3) (contd)

Dr. Eyhab Al-Masri

1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann

What is a top down design?


Begin with a big picture description of a problem solution in sequential steps. Sequential steps are refined until the steps are detailed enough to translate to language statements Refined steps can be described using pseudocode or flowcharts
ENGR 1200U Winter 2013 - UOIT

What is a structured program?

A program that is written using simple control structures including: Sequence Selection Repetition

ENGR 1200U Winter 2013 - UOIT

What is a Conditional Expression?


A Boolean expression that evaluates to true or false Example
if (x>y) { ... } conditional expression

ENGR 1200U Winter 2013 - UOIT

What is the difference between = and == operators?


= is called an assignment operator. It is used to assign values to identifiers. == denotes equality testing (tests whether an identifier is equal to a given value or expression)
intc(10); if (c=5) {...} intc(10); if (c==5) {...}

ENGR 1200U Winter 2013 - UOIT

What does the following expression mean

(a > 5) && (y < 10)

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

B false true false true

A&&B false false false true

A||B false true true true

!A true true false false

!B true false true false

Short-circuit evaluation?

Can we combine multiple if statements to evaluate complex decisions?


Yes, we can.
ENGR 1200U Winter 2013 - UOIT

Consider the following UOIT GPA Grade Scale

How many branches do we have?

5 or 6?

For simplicity and to minimize number of branches in this example, we will assume that grade < 73 is passing but marginal !

ENGR 1200U Winter 2013 - UOIT

Grade >= 90

true

(4.3 Grade Points)

A+

false
Grade >= 85

true

(4.0 Grade Points)

false
Grade >= 80

true

(3.7 Grade Points)

A-

false
Grade >= 77

true

(3.3 Grade Points)

B+

false
Grade >= 73

true

(3.0 Grade Points)

false
Pass, Marginal

ENGR 1200U Winter 2013 - UOIT

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)

the next test is evaluated

. . .

ENGR 1200U Winter 2013 - UOIT

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

Pay attention to the order of the conditions

no further tests are required

ENGR 1200U Winter 2013 - UOIT

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!

this test is also true!


That code block is not executed!

this test is also true!


That code block is not executed!

Nested if statements: if statements inside other if statements Example


if (grade>=80) { if (grade>=90) { cout<<"YourlettergradeisA+"; } elseif(grade>=85) { cout<<"YourlettergradeisA"; } else { cout<<"YourlettergradeisA"; } }

if () { if () { if () { } } }

ENGR 1200U Winter 2013 - UOIT

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;

This is not the case!


ENGR 1200U Winter 2013 - UOIT

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.

dangling else problem


ENGR 1200U Winter 2013 - UOIT

This is the

Example

Solution:Simple,putone statementinablock{}

doubleship_rate=5.00;//standardshippingacrossCanada if (Country==Canada) { if (Province==PEI) ship_rate=9.00; } else ship_rate=30.00;

ENGR 1200U Winter 2013 - UOIT

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="";}

ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

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.

ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

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.

ENGR 1200U Winter 2013 - UOIT

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

ENGR 1200U Winter 2013 - UOIT

11

Vous aimerez peut-être aussi