1 / 50

Chapter 5 Making Decisions

Chapter 5 Making Decisions. Control Structures. You can control the flow of a program by using one of the following three control structures :. Sequence (also known as straight line programming) Decision (also known as selection) Iteration (also known as repetition). Boolean Decisions.

adamdaniel
Télécharger la présentation

Chapter 5 Making Decisions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5Making Decisions Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  2. Control Structures You can control the flow of a program by using one of the following three control structures: • Sequence (also known as straight line programming) • Decision (also known as selection) • Iteration (also known as repetition) Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  3. Boolean Decisions All computer decisions reduce to Boolean decisions because of the digital nature of the CPU. A Boolean decision is based on a Boolean result of true or false. Boolean decisions are made in Java using Boolean operators. There are two types of Boolean operators used in Java: relational operators and logical operators. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  4. Java Relational Operators == Equal to != Not equal to < Less than <= Less than or equal to > Greater than >= Greater than or equal to Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  5. Java Relational Operators Relational operators can be combined with arithmetic operators: Example: 5 + 3 < 4 What is the result of this operation? false because 8 is not < 4 Relational operators are always performed last! Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  6. Java Relational Operators It is not a good idea to use the == or != operators to test floating-point values. Rather, you should use <, >, <=, or >= when testing floating-point values. The reason is due to the way that the computer represents floating point values in memory. Unless you have infinite precision, two floating-point values cannot, theoretically, ever be equal. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  7. Java Logical Operators ! NOT ¦¦ OR && AND Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  8. Java Logical Operators The NOT operator, !, is used to negate or invert a Boolean value. !true is false and !false is true Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  9. Java Logical Operators The OR operator, ¦¦, is applied to multiple Boolean values. If A and B are Boolean variables, then the expression A¦¦B is true when either A “or” B are true. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  10. Java Logical Operators As you can see, any true results in true! Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  11. Java Logical Operators The AND operator, &&, also operates on multiple Boolean values. If A and B are Boolean variables, then the expression A&&B is true only when both A “and” B are true. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  12. Java Logical Operators As you can see, any false will result in false! Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  13. Java Logical Operators What value is generated as a result of the following operation? (5!=5) && (3==3) false false && true is false Both conditions must evaluate to true for an AND condition to be true Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  14. String Comparisons When comparing two string objects, you must use the compareTo() method, not the Boolean relational operators. String myName = “Andy”; String yourName = “Sandy”; myName.compareTo(yourName); Here, the result would be negative, since the string “Andy” is less than the string “Sandy”. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  15. Decision Control Structure The decision control structure allows the program flow to be altered depending upon conditions. It is implemented in Java using the if, if/else, and switch statements and is the topic of discussion for this chapter. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  16. The Flow of the if Statement The if statement is a decision control structure. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  17. if Statement Syntax if(test expression) { statement 1; statement 2; • • //COMPOUND STATEMENT • statement n; }//END IF Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  18. Pseudocode If balance < 500 monthlyInterest = balance .02 balance = balance + monthlyInterest Next statement Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  19. Java Code if(balance < 500) note: no semicolon here { monthlyInterest = balance .02; balance = balance + monthlyInterest; }//END if • Note • Test expression must be in () • { } are used for the statements • Optional, but still recommended if there’s only one statement Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  20. Pseudocode If (x <y ) AND (y 10 ) sum = x + y Write x, y , sum Next statement What is the implication of the AND in the if statement? If either test condition is false the entire statement is false. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  21. Java Code intx = 2; inty = 3; intsum = 0; if((x<y)&&(y!=10)) { sum = x + y; System.out.println("x = “ + x + "\ny = “ + y + "\nsum = " + sum); }//END IF What is the output? Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  22. Output The value of x is less than yand the value of y is not equal to 10. As a result, the if statements are executed, the two values are added and the output is: x = 2 y = 3 sum = 5 Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  23. if Statement intx = 3; inty = 3; What happens when x = 3? intsum = 0; if((x<y)&&(y!=10)) { sum = x + y; System.out.println("x = "+x+"\ny = "+y + "\nsum = " + sum); }//END IF x is not less than y. Therefore, the two values are not added and display would not occur. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  24. The Flow of the if/else Statement The if/else statement allows for two sets of statements. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  25. if/else Statement Syntax if(test expression) { statement 1; statement 2; • • • statement n; }//END IF else { statement 1; statement 2; • • • statement n; }//END else Notice that both the if statements and the else statements are framed with braces. Also, notice that indentation is used for clarity. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  26. What is wrong with the logic here? If today is Friday Write “It’s Payday”Write “It’s not Payday” This will result in a logic error since it will write both statements when today is Friday. How do we fix it? By constructing an If/Else statement, like this … Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  27. Pseudocode If today is Friday Write “It’s payday!” Else Write “It’s not payday” Write “Wow!” How would you actually code this in Java? Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  28. Java Code if(today.equals(“Friday”)) System.out.println(“It’s payday!”); else System.out.println(“It’s not payday”); System.out.println(“Wow”); Note that the equals() method must be used to compare two strings for equality. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  29. When will the if statement be executed? When the else statement will be executed? if(x != 0) sum = x + y; else difference = x  y; When x is nonzero. When x is zero. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  30. Consider the following code: if((x < y) && (2x  y == 0)) sum = x + y; else difference = x  y; Notice the && operator is used here! The if is executed when the value of x is less than the value of y “and” the value of 2x - y is equal to zero. The else is executed when the value of x is greater than or equal to the value of y “or” the value of 2x-y is not equal to zero. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  31. Consider the following code: if((x < y) ¦¦ (2x  y == 0)) sum = x + y; else difference = x  y; Notice the || operator is used here! The if is executed when the value of x is less than the value of y “or” the value of 2x-y is equal to zero The else is executed when the value of x is greater than or equal to the value of y “and” the value of 2x-y is not equal to zero Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  32. Consider the following code: if(x = 0) sum = x + y; else difference = x  y; There is a syntax error here! Where? The Boolean test is coded as x = 0 and should be?? x == 0 Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  33. Consider the following code: if(x > 2y) sum = x + y; product = x  y; else difference = x  y; This won’t compile! Why? A dangling else! How to fix it? Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  34. if(x > 2y) { sum = x + y; product = x  y; } else difference = x  y; You must frame multiple lines of an if statement within braces to eliminate the dangling else! The if is executed when the value of x is greater than the value of 2y The else will execute when the value of x is less than or equal to the value of 2y Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  35. Nested if/else’s Nothing more than an if/else statement within an if/else statement. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  36. Convert this series if’s to nested if/else’s. if(year == 1) System.out.println("Freshman"); if(year == 2) System.out.println("Sophomore"); if(year == 3) System.out.println("Junior"); if(year == 4) System.out.println("Senior"); if(year > 4) System.out.println("Graduate"); Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  37. If-else-If-else if(year > 4) System.out.println("Graduate"); else if(year > 3) System.out.println("Senior"); else if(year > 2) System.out.println("Junior"); else if(year > 1) System.out.println("Sophomore"); else System.out.println("Freshman"); Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  38. If-If-else-else if(year > 1) if(year > 2) if(year > 3) if(year > 4) System.out.println("Graduate"); else System.out.println("Senior"); else System.out.println("Junior"); else System.out.println("Sophomore"); else System.out.println("Freshman"); Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  39. The Switch statement The switch statement allows the program to select one of many options, also knows as cases. The selection of a particular case is controlled by a matching process where a selector variable is compared against a series of case values. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  40. The flow of the Switch statement Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  41. The Switch statement If a match is found (the selector value == case value) the corresponding case statements are then executed. If no match is found, the program continues in a straight line fashion with those statements following the switch statement. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  42. Switch statement syntax switch(selector variable) { case case 1 value : case 1 statements; break; case case 2 value : case 2 statements; break; • • • case case n value : case n statements; }//END SWITCH Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  43. What happens if letterGrade is a ‘B’? switch(letterGrade) { case'A' : io.writeInfo("Excellent"); break; case'B' : io.writeInfo("Superior"); break; case'C' : io.writeInfo("Average"); break; case'D' : io.writeInfo("Poor"); break; case'F' : io.writeInfo("Try Again"); }//END SWITCH What if we leave out the break statements? And ‘A’ is selected? Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  44. The Switch statement Notice that the selector variable must follow the keyword switch, within parentheses. The selector variable must be an integral data type character or integer. The selector variable and case values must be the same data type. If you define a selector variable as a floating-point or string object you’ll get a compile error! Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  45. The Switch statement The switch block is comprised of several cases identified by using the keyword case. Note that a given case statement block can be any number of statements and does not require framing. You must insert the keyword break as the last statement in a given case statement block. If you do not, any subsequent cases will be executed after a given case match has occurred until a break is encountered. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  46. The Switch statement Be aware that there are times when you will want to leave out the break statement. Consider the situation where the selector variable could be either a lower- or upper-case character value. The break could follow the last of those two case options, as follows… Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  47. switch(letterGrade) { case'a' : case'A' : io.writeInfo("Excellent"); break; case'b' : case'B' : io.writeInfo("Superior"); break; case'c' : case'C' : io.writeInfo("Average"); break; case'd' : case'D' : io.writeInfo("Poor"); break; case'f' : case'F' : io.writeInfo("Try Again"); }//END SWITCH Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  48. The default option What would happen if there were no matches found in the switch? You create a default action should this situation occur. If a match is not found the default is executed. The default option is an excellent tool to protect against invalid entries from the users when using the switch to produce menu driven programs. Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  49. switch(letterGrade) { case'a' : case'A' : io.writeInfo("Excellent"); break; case'b' : case'B' : io.writeInfo("Superior"); break; case'c' : case'C' : io.writeInfo("Average"); break; case'd' : case'D' : io.writeInfo("Poor"); break; case'f' : case'F' : io.writeInfo("Try Again"); break; default : io.writeInfo("No match was found for the entry “ + letterGrade); }//END SWITCH Adding a default Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

  50. Here, a default has been added to catch an invalid letter grade. Notice that a break must also be added to the last case. Why? Information Systems Programming with Java, 2nd edition Andrew C. Staugaard, Jr.

More Related