Presentation is loading. Please wait.

Presentation is loading. Please wait.

Solving Problems that involve decisions. Topics Normal execution order Conditional statements Relational operators & Boolean expressions Enumerations.

Similar presentations


Presentation on theme: "Solving Problems that involve decisions. Topics Normal execution order Conditional statements Relational operators & Boolean expressions Enumerations."— Presentation transcript:

1 Solving Problems that involve decisions

2 Topics Normal execution order Conditional statements Relational operators & Boolean expressions Enumerations

3 Objectives At the completion of this topic, students should be able to: Describe the normal flow of control through a C# program Correctly use if statements in a C# program Explain how to use relational operators to write a Boolean expression and use them correctly in a C# program Correctly use if/else statements in a C# program Correctly use blocks in if/else statements Correctly use a switch statement in a C# program Describe enumerations and use them in a C# program Correctly use the logical operators to make complex Boolean expressions

4 The order in which statements in a program are executed is called the flow of control.

5 Methods that we have written so far begin with the first line in the method, and continue line by line until control reaches the end of the method.

6 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 controlFlow of control Example of Sequential Flow

7 There are many problems that cannot be solved using methods that execute in this line-by-line fashion from beginning to end. Some problems require the program to take different actions, depending upon conditions that exist in the program at the time.

8 Examples

9 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:00amno increase 6:01am to 9:30am30% increase 9:31am to 3:00am10% increase 3:01pm to 7:00pm 30% increase 7:01pm to 10:00pm10% increase 10:01pm to midnightno increase

10 The U.S. Widget company runs its payroll 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 a salary, that employee’s gross pay is calculated differently yearlySalary / 26.0;

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

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

13 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

14 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

15 If the delivery truck Then the delivery leaves between time increases by midnight and 6:00am0% 6:01am and 9:30am30% 9:31am and 3:00am10% 3:01pm and 7:00pm 30% 7:01pm and 10:00pm10% 10:01pm and midnight0% GoodGuys

16 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.

17 Conditional statements are shown in an activity diagram by using a diamond. ( balance < LIMIT ) balance < LIMIT ?

18 Conditional statements are shown in an activity 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

19 Conditional statements are shown in an activity 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.

20 The if Statement The 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 indented the statement to be executed when the condition is true. Although the compiler doesn’t care about indentation, it makes it easier to read the code.

21 The if Statement The 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 actions Are taken when the condition is true

22 Problem Statement Write a program that prompts the user to enter in his or her age. the person is under 21, print a message that says “Youth is a wonderful thing … enjoy it.” If

23 Prompt user to Enter in their age age < 21 ? Print “Youth is a …” true false Store input in the variable age end

24 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 is executed if the condition is true, but the block is skipped if the condition is false.

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

26 Relational Operators relational operators are used to write Boolean expressions OperatorMeaning ==equal !=not 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 arithmetic operations are done first.

27 Note that the following expression will not compile in C#… if ( age = myNewAge) {... } This is an assignment, not a comparison Warning: Don’t confuse = and ==

28 The if/else statement This construct allows us to do one thing if a condition is 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

29 Problem Statement Write a program that prompts the user to enter in his or her age. 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

30 Prompt user to Enter in their age age < 21 ? Print “Youth is a …” true false Store input in the variable age end Print Old age is a…”

31 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

32 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, delimited by { and } anyplace where we would normally use a single statement. if ( a < b ) { a = 0; b = 0; Console.WriteLine(“Sorry …”); }...

33 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

34 Nested if/else Statements In C#, the statement to be executed as the result of an if statement could be another if statement. In a nested if statement, an else clause is always matched to the closest unmatched if.

35 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!

36 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

37 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

38 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();

39 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 exactly the same. The only difference is how the code is indented.

40 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 the difference?

41 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.

42 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 and all of the others are skipped.

43 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 statements is executed in turn. There is no else clause to cause control to skip these.

44 Dangling else clauses An else is always matched to the closest unmatched if

45 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 hours a week, then make sure that they don’t work more than 20 hours of overtime

46 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.”);

47 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.”);

48 Practice: Write a program that takes a temperature in degrees Celsius. Then output whether or not water is a solid, liquid, or vapor at that temperature at sea level. Example: Input: -5 Output: Water is solid

49 Designing programs that use conditional statements

50 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.

51 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.

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

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

54 Example Slick Guys Used Car Sales gives their sales people a commission, based on the value of the sale. If the sale was 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 the blue book price, the sales person gets a 5% commission. Slick Guys never sells a car for less than the blue book price.

55 How many conditions are there? What are the conditions?

56 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

57 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

58 Sticker Price Blue Book Price 5% 20% 10% Boundary Conditions no sale

59 Sticker Price Blue Book Price 5% 20% 10% Boundary Condition Put your conditions in order … start with the most likely no sale

60 is sale >= sticker ? commission = 0.20 is sale > bluebk ? commission = 0.10 is Sale = bluebk ? commission = 0.05 No sale true false

61 is sale >= bluebk ? commission = 0.10 is sale > sticker ? commission = 0.20 is sale = bluebk ? commission = 0.05 No sale true false What happens if you put the conditions in the wrong order? 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?

62 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!”);

63 The Switch Statement The 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 get a compile error. if numValues is not 0, 1, or 2, execution goes here.

64 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 };

65 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(); }

66 Handling Complex Conditions

67 Consider the following situation: If it is Friday night and you have no homework then go to the movies. These two conditions can be combined using logical operators.

68 Logical Operators OperatorDescriptionExample !logical NOT !a &&logical AND a && b ||logical OR a || b the results of logical operations are defined in truth tables

69 a !a falsetrue truefalse Logical NOT

70 Logical AND a ba && b falsefalse false falsetrue false truefalse false truetrue true the evaluation of && is short circuited if a is false.

71 Logical OR a ba || b falsefalse false falsetrue true truefalse true truetrue true the evaluation of || is short circuited if a is true.

72 Consider the following situation: If it is Friday night and you have no homework then go to the movies. if ( today == FRIDAY && homework == NO) goToMovie( );

73 Simplifying complex relationships using logical operators

74 Consider the following problem: Given three values, x, y, and z, find out if y lies in between x and z. z x y < x y > x and y < z y > z

75 Consider the following problem: Given three values, x, y, and z, find out if y lies in between x and z. z x 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.”);

76 Consider the following problem: Given three values, x, y, and z, find out if y lies in between x and z. z x 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.”);

77 Operator Precedence (again) +unary plus - unary minus !Not *multiplication /division %remainder +addition -subtraction higher precedence lower precedence

78 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

79 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 are greater in value than upper case characters.

80 The ASCII Code Table

81 c a t c a r 99 97 116 99 97 114 C a t CAR 67 97 116 67 65 82

82 Comparing Real Numbers Because of the way that real numbers are represented in the computer, you cannot be guaranteed that two real numbers are exactly equal to one another. The best way to handle this situation is to subtract one Real number from the other and see if the absolute value of their difference is smaller than some tolerance value.

83 const double EPS = 0.0001;... if ( (a – b) < EPS) ) Console.WriteLine(“\nThey are equal…”); Testing to see if two floating point values are equal

84 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 7) true

85 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 different lengths is called a scalene triangle. Write a program that asks for the lengths of the three sides of a triangle, and then tells the user what kind of triangle it is.

86 Groups of four * Design and code these programs

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

88 Practice Obe Wan Kenobi is in charge of reviewing the applications of this years candidates for Jedi Knight school. Candidates must be at least 3’ high, but less than 8’. However, an exception has been made for Federation pilots, who can be of 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.

89 Practice The 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.

90 Write a tollbooth program that calculates the toll required to use the highway, based on the class of vehicle. Passenger cars$2.00 Busses$3.00 Trucks$5.00 Use a switch statement and an enumeration. Practice


Download ppt "Solving Problems that involve decisions. Topics Normal execution order Conditional statements Relational operators & Boolean expressions Enumerations."

Similar presentations


Ads by Google