solving problems that involve decisions. topics normal execution order conditional statements...

90
Solving Problems that involve decisions

Upload: reilly-chumley

Post on 16-Dec-2015

233 views

Category:

Documents


0 download

TRANSCRIPT

Solving Problems thatinvolve decisions

Topics

Normal execution orderConditional statements

Relational operators & Boolean expressions

Enumerations

ObjectivesAt the completion of this topic, students should be able to:

Describe the normal flow of control through a C# programCorrectly use if statements in a C# programExplain how to use relational operators to write a Boolean expression and use them correctly in a C# programCorrectly use if/else statements in a C# programCorrectly use blocks in if/else statementsCorrectly use a switch statement in a C# programDescribe enumerations and use them in a C# programCorrectly use the logical operators to make complex Boolean expressions

The order in which statements in a program areexecuted is called the flow of control.

Methods that we have written so far beginwith the first line in the method, andcontinue line by line until control reaches the end of the method.

using System;class Program{ static void Main() { const int MINS_PER_HOUR = 60, SPLITTER = 100, PERCENT_BETTER = 1.25;;

int startTime, endTime; int endTime;

// Prompt for input and get start time Console.WriteLine("Enter in the start time for this trip in 24 hour clock time"); Console.Write("in the form hhmm: "); startTime = int.Parse(Console.ReadLine());

// Prompt for input and get end time Console.WriteLine("Enter in the end time for this trip in 24 hour clock time"); Console.Write("in the form hhmm: "); endTime = int.Parse(Console.ReadLine());

// calculate the start and end time with minutes as integers int startMmm = startTime / SPLITTER * MINS_PER_HOUR + startTime % SPLITTER; int endMmm = endTime / SPLITTER * MINS_PER_HOUR + endTime % SPLITTER;

…etc

} // end of Main}

Flow

of control

Example of Sequential Flow

There are many problems that cannotbe solved using methods that executein this line-by-line fashion from beginningto end.

Some problems require the program to takedifferent actions, depending upon conditionsthat exist in the program at the time.

Examples

A More Accurate GoodGuys Program

GoodGuy's Delivery service operates a fleet of delivery vehicles that operate between Provo and Salt Lake City. With the I-15 construction work scheduled for Utah County, Goodguys wants you to create a program for them that will compute the new arrival times for deliveries. Once the work on the interstate begins, GoodGuys estimates that their delivery times will increase based on the following table:

midnight to 6:00am no increase6:01am to 9:30am 30% increase9:31am to 3:00am 10% increase3:01pm to 7:00pm 30% increase7:01pm to 10:00pm 10% increase10:01pm to midnight no increase

The U.S. Widget company runs itspayroll program at the end of every two week period.

If an employee gets paid by the hour, that employee’s gross pay is equal to

hoursWorked * hourlyWage;

However, if an employee gets paid asalary, that employee’s gross pay iscalculated differently

yearlySalary / 26.0;

In a computer chess game, the computermight calculate its move in a number of different ways, depending upon where allof the various pieces on the chessboard.

How can you tell if a problem requiresa program that includes this kind of logic?

When the problem statement includes the word “if”

If this employee gets an hourly wage

If the King is in check

If today is a weekday

If the balance in the account is less than $1,000

Sometimes the problem statement will use the word “when”

When an employee gets an hourly wage

When the King is in check

When the day is a weekday

When the balance in the account is less than $1,000

If the delivery truck Then the deliveryleaves between time increases by

midnight and 6:00am 0%6:01am and 9:30am 30%9:31am and 3:00am 10%3:01pm and 7:00pm 30%7:01pm and 10:00pm 10%10:01pm and midnight 0%

GoodGuys

These are all examples of Conditional Statements,i.e. do something if a condition is true. In programming we often take a different action based on whether or not a condition is true.

Conditional statements are shown in anactivity diagram by using a diamond.

( balance < LIMIT ) balance <LIMIT

?

Conditional statements are shown in anactivity diagram by using a diamond.

Arrows show the execution path that the program takes when the statement is true and when it is false.

if ( balance < LIMIT ) balance <LIMIT

?

true

false

Conditional statements are shown in anactivity diagram by using a diamond.

Arrows show the execution path that the program takes when the statement is true and when it is false.

if ( balance < LIMIT ) balance <LIMIT

?

true

false

The “if” statement tests the condition.

The if StatementThe if statement allows us to execute a

statement only when the condition is true.

if ( balance < LIMIT ) balance = balance – CHARGE;

balance <LIMIT

?

true balance = balance – CHARGE;

false

note how we have indentedthe statement to be executedwhen the condition is true. Althoughthe compiler doesn’t care about indentation,it makes it easier to read the code.

The if StatementThe if statement allows us to execute a

statement only when the condition is true.

if ( balance < LIMIT ){ balance = balance – CHARGE;}

balance <LIMIT

?

true balance = balance – CHARGE;

false

Use curly braces to clearly define what actionsAre taken when the condition is true

Problem Statement

Write a program that prompts the user to enter in his or herage. the person is under 21, print a message that says

“Youth is a wonderful thing … enjoy it.”

If

Prompt user toEnter in their

age

age < 21?

Print“Youth is a …”

true

false

Store inputin the variable

age

end

using System;

class Program{ const int MINOR = 21;

static void Main() { int age = 0; Console.Write("How old are you? "); age = int.Parse(Console.ReadLine());

if (age < MINOR) { Console.WriteLine("Youth is a wonderful thing ... enjoy it."); } Console.WriteLine(“Goodbye!”); Console.ReadLine(); }//End Main()}//End class Program

Notice how this block of code isexecuted if the condition is true,but the block is skipped if the condition is false.

The expression inside the if( … )statement is called a Boolean expression,because its value must be either true or false.

Relational Operatorsrelational operators are used to write Boolean expressions

Operator Meaning

== equal != not equal < less than <= less than or equal > greater than >= greater than or equal

(not greater than)

(not less than)

Note: The precedence of relational operators is lower than arithmetic operators, so arithmeticoperations are done first.

Note that the following expressionwill not compile in C#…

if ( age = myNewAge){ . . .}

This is an assignment,not a comparison

Warning: Don’t confuse = and ==

The if/else statementThis construct allows us to do one thing if a conditionis true, and something else if the condition is false.

if (height > MAX) adjustment = MAX – height;else adjustment = 0;

Console.WriteLine(adjustment);

height > MAX?

adjustment =MAX - height

adjustment= 0

true

false

Problem Statement

Write a program that prompts the user to enter in his or herage. the person is under 21, print a message that says

“Youth is a wonderful thing … enjoy it.”

Otherwise, print a message that says

“Old age is a state of mind.”

If

Prompt user toEnter in their

age

age < 21?

Print“Youth is a …”

true

false

Store inputin the variable

age

endPrint

Old age is a…”

using System;

class Program{ const int MINOR = 21;

static void Main() { int age; Console.Write("How old are you? "); age = int.Parse(Console.ReadLine());

if (age < MINOR) { Console.WriteLine("Youth is a wonderful thing ... enjoy it."); } else { Console.WriteLine("Old age is a state of mind...!"); }

Console.ReadLine(); }//End Main()}//End class Program

Executing a Block of Statements

Sometimes we want to execute more than one statement when a condition is true ( or false ).

In C#, we can use a block of statements, delimitedby { and } anyplace where we would normally use asingle statement.

if ( a < b ){ a = 0; b = 0; Console.WriteLine(“Sorry …”);}. . .

using System;

class Program{ const int MINOR = 21;

static void Main() { int age; Console.Write("How old are you? "); age = int.Parse(Console.ReadLine());

if (age < MINOR) { Console.WriteLine("**********************************"); Console.WriteLine("* Youth is a wonderful thing *"); Console.WriteLine("* Enjoy it! *"); Console.WriteLine("**********************************"); }

else { Console.WriteLine("***********************************"); Console.WriteLine("* Old age is a *"); Console.WriteLine("* state of mind...! *"); Console.WriteLine("***********************************"); }

Console.ReadLine(); }//End Main()}//End class Program

Nested if/else StatementsIn C#, the statement to be executed as theresult of an if statement could be another ifstatement.

In a nested if statement, an else clause isalways matched to the closest unmatched if.

using System;

class Program{ static void Main() { int a, b, c, min = 0;

Console.WriteLine("Enter three integer values - each on a separate line."); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = int.Parse(Console.ReadLine());

if (a < b) if (a < c) min = a; else min = c; else if (b < c) min = b; else min = c;

Console.WriteLine("The smallest value entered was {0}", min); Console.ReadLine(); }//End Main()}//End class Program

keep track of which if an else goes with!

using System;

class Program{ static void Main() { int a, b, c, min = 0;

Console.WriteLine("Enter three integer values - each on a separate line."); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = int.Parse(Console.ReadLine());

if (a < b) if (a < c) min = a; else min = c; else if (b < c) min = b; else min = c;

Console.WriteLine("The smallest value entered was {0}", min); Console.ReadLine(); }//End Main()}//End class Program

Hmmm … it’s hard without indenting code

using System;

class Program{ static void Main() { int a, b, c, min = 0;

Console.WriteLine("Enter three integer values - each on a separate line."); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = int.Parse(Console.ReadLine());

if (a < b) if (a < c) min = a; else min = c; else if (b < c) min = b; else min = c;

Console.WriteLine("The smallest value entered was {0}", min); Console.ReadLine(); }//End Main()}//End class Program

Hmmm … it’s worse if you indent wrong

Another Example

char direction = ‘n’;

Console.WriteLine("Press a direction key(n, s, e, or w) and Enter"); direction = char.Parse(Console.ReadLine()); if (direction == 'n') Console.WriteLine("You are now going North."); else if (direction == 's') Console.WriteLine("You are now going South."); else if (direction == 'e') Console.WriteLine("You are now going East."); else if (direction == 'w') Console.WriteLine("You are now going West."); else Console.WriteLine("You don't know where you are going!");

Console.ReadLine();

if (direction == 'n') Console.WriteLine(“…going North."); else if (direction == 's') Console.WriteLine(“…going South."); else if (direction == 'e') Console.WriteLine(“…going East."); else if (direction == 'w') Console.WriteLine(“…going West."); else Console.WriteLine("You don't know…!");

if (direction == 'n') Console.WriteLine(“…going North.");else if (direction == 's') Console.WriteLine(“…going South.");else if (direction == 'e') Console.WriteLine(“…going East."); else if (direction == 'w') Console.WriteLine(“…going West.");else Console.WriteLine("You don't know…!");

These are exactlythe same. The onlydifference is how

the code is indented.

if (direction == 'n') Console.WriteLine(“…going North.");else if (direction == 's') Console.WriteLine(“…going South.");else if (direction == 'e') Console.WriteLine(“…going East."); else if (direction == 'w') Console.WriteLine(“…going West.");

if (direction == 'n') Console.WriteLine(“…going North.");if (direction == 's') Console.WriteLine(“…going South.");if (direction == 'e') Console.WriteLine(“…going East.");if (direction == 'w') Console.WriteLine(“…going West.");

What’s thedifference?

if (direction == 'n') Console.WriteLine(“…going North.");else if (direction == 's') Console.WriteLine(“…going South.");else if (direction == 'e') Console.WriteLine(“…going East."); else if (direction == 'w') Console.WriteLine(“…going West.");

if (direction == 'n') Console.WriteLine(“…going North.");if (direction == 's') Console.WriteLine(“…going South.");if (direction == 'e') Console.WriteLine(“…going East.");if (direction == 'w') Console.WriteLine(“…going West.");

Suppose that ‘n’was entered.

if (direction == 'n') Console.WriteLine(“…going North.");else if (direction == 's') Console.WriteLine(“…going South.");else if (direction == 'e') Console.WriteLine(“…going East."); else if (direction == 'w') Console.WriteLine(“…going West.");

Suppose that ‘n’was entered.

This statement is executed andall of the others are skipped.

if (direction == 'n') Console.WriteLine(“…going North.");if (direction == 's') Console.WriteLine(“…going South.");if (direction == 'e') Console.WriteLine(“…going East.");if (direction == 'w') Console.WriteLine(“…going West.");

Suppose that ‘n’was entered.

The first output statement is executed,then each of the other if statementsis executed in turn. There is no elseclause to cause control to skip these.

Dangling else clauses

An else is always matched to the closest unmatched if

const int R_LIMIT = 40;const int O_LIMIT = 20;

. . .

if (regTime > R_LIMIT) if (overTime > O_LIMIT) Console.WriteLine(“Overtime hours exceed the limit.”);else Console.WriteLine(“no overtime worked.”);

Will this code do what you expect?

If an employee works more than 40 hoursa week, then make sure that they don’twork more than 20 hours of overtime

Will this code do what you expect?

This else goes with The second if statement

. . . If regTime were 50 and overTime were 10, the message “no overtime worked” would be displayed.

const int R_LIMIT = 40;const int O_LIMIT = 20;

. . .

if (regTime > R_LIMIT) if (overTime > O_LIMIT) Console.WriteLine(“Overtime hours exceed the limit.”);else Console.WriteLine(“no overtime worked.”);

The code should be written this way.

const int R_LIMIT = 40;Const int O_LIMIT = 20;

. . .

if (regTime > R_LIMIT){ if (overTime > O_LIMIT) Console.WriteLine(“Overtime hours exceed the limit.”);}else Console.WriteLine(“no overtime worked.”);

Practice:Write a program that takes a temperature in degreesCelsius. Then output whether or not water is a solid, liquid, or vapor at that temperature at sea level.

Example:Input: -5Output: Water is solid

Designing programs that use conditional statements

1. Carefully inspect the problem statement. If word “if” is used or the word “when” is used to describe different situations that might exist, then the program probably will need to use conditional statements.

2. After determining that several different situations might exist, try to identify each unique situation. Write down the condition that makes this situation unique.

Carefully check that each condition is unique - there should be no overlaps. Be sure that the boundary conditions are correctly stated.

3. Write down exactly what the program should do differently in each of the unique situations that you have identified.

4. Formulate the if/else statements that reflect this set of conditions. Make them as simple as possible. Watch for dangling else’s.

Example

Slick Guys Used Car Sales gives their sales people acommission, based on the value of the sale. If the salewas made at or above the sticker price, the sales person gets a 20% commission. If the sale was less than the full sticker price, but greater than the blue book price, the sales commission is 10%. If the sale price is equal to theblue book price, the sales person gets a 5% commission. Slick Guys never sells a car for less than the blue book price.

How many conditions are there?

What are the conditions?

1. The sales price is GREATER than or EQUAL to the sticker price.

2. The sales price is LESS than the sticker price but GREATER than the blue book price.

3. The sales price EQUALS the blue book price.

4. The sales price cannot be less than blue book.

4 Conditions

1. The sales price is GREATER than or EQUAL to the sticker price.

2. The sales price is LESS than the sticker price but GREATER than the blue book price.

3. The sales price EQUALS the blue book price.

4. The sales price cannot be less than blue book

What do you do for each situation?

Set commission = 20% of sales price

Set commission = 10% of sales price

Set commission = 5% of sales price

Message: Cannot sell for less than blue book

Sticker PriceBlue Book Price

5%

20%10%

Boundary Conditions

no sale

Sticker PriceBlue Book Price

5%

20%10%

Boundary Condition

Put your conditions in order … start with the most likely

no sale

issale >= sticker

?

commission = 0.20

issale > bluebk

?

commission = 0.10

isSale = bluebk

?

commission = 0.05

No sale

true

true

true

false

false

false

issale >= bluebk

?

commission = 0.10

issale > sticker

?

commission = 0.20

issale = bluebk

?

commission = 0.05

No sale

true

true

true

false

false

false

What happens if you putthe conditions in the wrongorder?

Suppose that the sticker price = 20,000, the blue book price = 19,000, and the sale price = 21,000. What will the commission be?What should it be?

if ( salesPrice >= STICKER_PRICE) commissionRate = 0.20;else if ( salesPrice > BLUE_BOOK) commissionRate = 0.10;else if (salesPrice == BLUE_BOOK) commissionRate == 0.05;else Console.WriteLine(“No Sale!”);

The Switch StatementThe switch statement allows a program to take one of several different paths, based on the value given to the switch statement.

switch ( numValues ){ case 0: Console.WriteLine(“No values were entered”); break;

case 1: Console.WriteLine(“One value was entered”); break;

case 2: Console.WriteLine(“Two values were entered”); break;

default: Console.WriteLine(“Too many values were entered”); break;}

must be an integer or a character literal

without the break statement, you will geta compile error.

if numValues is not0, 1, or 2, execution goes here.

Enumerations

An enumeration type is a user defined data type whose valid values are defined by a list of constants of type int.

enum DayOfWeek { SUN=0, MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6 };

if values are not explicitly given, then the first constant is assigned zero, and then each is incremented by one. So the following is equivalent to the above example:

enum DayOfWeek { SUN, MON, TUE, WED, THU, FRI, SAT };

Enumerations are often used to label the cases in a switch statement:enum Days { MON = 1, TUE = 2, WED = 3};{ - - -

switch ( (Days)whichDay ) { case Days.MON: Console.WriteLine("Today is Monday"); break;

case Days.TUE: Console.WriteLine("Today is Tuesday"); break;

case Days.WED: Console.WriteLine("Today is Wednesday"); break;

default: Console.WriteLine("What day is it?"); break; } Console.ReadLine(); }

Handling Complex Conditions

Consider the following situation:

If it is Friday night and you have no homeworkthen go to the movies.

These two conditions can be combined using logical operators.

Logical Operators

Operator Description Example

! logical NOT !a && logical AND a && b || logical OR a || b

the results of logical operations are defined in truth tables

a !a

false true

true false

Logical NOT

Logical AND

a b a && b

false false false

false true false

true false false

true true true

the evaluation of &&is short circuited if ais false.

Logical OR

a b a || b

false false false

false true true

true false true

true true true

the evaluation of ||is short circuited ifa is true.

Consider the following situation:

If it is Friday night and you have no homeworkthen go to the movies.

if ( today == FRIDAY && homework == NO) goToMovie( );

Simplifying complex relationshipsusing logical operators

Consider the following problem:

Given three values, x, y, and z, find out ify lies in between x and z.

zx

y < x y > x and y < z y > z

Consider the following problem:

Given three values, x, y, and z, find out ify lies in between x and z.

zx

y < x y > x and y < z y > z

We could write the conditions as follows:

if ( y > x) if ( y < z) Console.WriteLine(“\n the value of y lies between x and z.”); else Console.WriteLine( “\n the value of y does not lie between x and z.”);else Console.WriteLine( “\n the value of y does not lie between x and z.”);

Consider the following problem:

Given three values, x, y, and z, find out ify lies in between x and z.

zx

y < x y > x and y < z y > z

But the following is simpler and eliminates one else clause:

if ( y > x && y < z ) Console.WriteLine( “\n the value of y is between x and z.”);else Console.WriteLine( “\n the value of y is not between x and z.”);

Operator Precedence (again)

+ unary plus- unary minus! Not

* multiplication/ division% remainder

+ addition- subtraction

higher precedence

lower precedence

< less than> greater than<= less than or equal>= greater than or equal

== equal!= not equal

&& and

|| or

higher precedence

lower precedence

= assignment+= add and assign-= subtract and assign*= multiply and assign/= divide and assign%= modulo and assign

Comparing Characters

Characters in the ASCII standard are arranged so that they compare in alphabetical order (collating sequence). Lower case characters are not equal to their upper case counterparts. As a matter of fact lower case characters aregreater in value than upper case characters.

The ASCII Code Table

c a t c a r

99 97 116 99 97 114

C a t CAR

67 97 116 67 65 82

Comparing Real Numbers

Because of the way that real numbers are representedin the computer, you cannot be guaranteed that tworeal numbers are exactly equal to one another.

The best way to handle this situation is to subtract oneReal number from the other and see if the absolute value oftheir difference is smaller than some tolerance value.

const double EPS = 0.0001;

. . .

if ( (a – b) < EPS) ) Console.WriteLine(“\nThey are equal…”);

Testing to see if two floating point values are equal

Practice

What is the value of the following, given that the value of count = 0,and the value of limit = 10? We don’t know what x and y are.

(count == 0 ) && (limit < 20) true

(count == 0 && limit < 20) true

(limit > 20 || count < 5) true

!(count == 12) true

(count == 1 && x < y) false

(count < 10 || x < y) true

(limit / count > 7 && limit < 0) divide by zero error!

(limit < 20 || limit / count > 7) true

Not all problems requiring decisions will use the word“if” or “when” in the problem description.

Learn to identify problems that may contain choices.For example …

A triangle whose sides are all of equal length is called an equilateral triangle.

A triangle with two equal sides is called an isosceles triangle.

A triangle whose sides are all of differentlengths is called a scalene triangle.

Write a program that asks for the lengths of the three sides of a triangle, and thentells the user what kind of triangle it is.

Groups of four

* Design and code these programs

Practice

Problem: Allow the user to type in two integers.Print out the value of the highest number. Numbersmay be negative. If they are equal, say so.

Practice

Obe Wan Kenobi is in charge of reviewing the applicationsof this years candidates for Jedi Knight school. Candidatesmust be at least 3’ high, but less than 8’. However, an exception has been made for Federation pilots, who can beof any height. Candidates under 3’ can go to Jr. Jedi Knight School.

Write a program that * gets a candidate’s height * asks if they are a federation pilot, * Then prints one of the following messages, as appropriate: - This candidate is accepted into Jedi Knight School - This candidate is accepted into Jr. Jedi Knight School - This candidate is not accepted.

PracticeThe DeepPockets Savings Bank computes the monthly interest rate on an account, based on the following:

Super Saver Account Standard Account

5% interest on the balance Always pay 3% interest as long as it is more than on the balance. $5,000. Otherwise pay 3%.

Write a program that computes the interest on an account,given the balance on the account and the type of account.

Write a tollbooth program that calculates the toll requiredto use the highway, based on the class of vehicle.

Passenger cars $2.00Busses $3.00Trucks $5.00

Use a switch statement and an enumeration.

Practice