ifelseif Statements ifelseif statements can become very complex
if-else-if Statements • if-else-if statements can become very complex. • Imagine the following decision set. if it is very cold, wear a heavy coat, else, if it is chilly, wear a light jacket, else, if it is windy wear a windbreaker, else, if it is hot, wear no jacket. Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #1
if-else-if Statements if (expression) statement or block else if (expression) statement or block // Put as many else ifs as needed here else statement or block • Care must be used since else statements match up with the immediately preceding unmatched if statement. • Example: Test. Results. java Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #2
if-else-if Flowchart Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #3
Nested if Statements • If an if statement appears inside of another if statement (single or block) it is called a nested if statement. • The nested if is only executed if the if statement it is in results in a true condition. • Nested if statements can get very complex, very quickly. • Example: Loan. Qualifier. java Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #4
Nested if Statement Flowcharts No “You must earn at least…” Yes Salary >= 30000? No years. On. Job >= 2 “. . . you must have been on job 2 years…” Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Yes “You qualify. . Chapter 3 Slide #5
if-else Matching This else matches with this if. if (employed == 'y') { if (recent. Grad == 'y') { System. out. println("You qualify for the special interest rate. "); } else { This else matches with this if. System. out. println("You must be a recent college graduate to qualify. "); } } else { System. out. println("You must be employed to qualify. "); Copyright © 2005, Starting Out With Java 5 } Control Structures to Objects Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #6
Logical Operators • Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. • Java also provides one unary (!) logical operator to reverse the truth of a boolean expression. Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #7
Logical Operators Operator Meaning Effect AND Connects two boolean expressions into one. Both expressions must be true for the overall expression to be true. OR Connects two boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which one. NOT The ! operator reverses the truth of a boolean expression. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true. && || ! Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #8
The && Operator • The logical AND operator (&&) takes two operands that must both be boolean expressions. • The resulting combined expression is true iff (if and only if) both operands are true. • Example: Logical. And. java Expression 1 Expression 2 Expression 1 && Expression 2 true false false true Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #9
The || Operator • The logical OR operator (||) takes two operands that must both be boolean expressions. • The resulting combined expression is false iff (if and only if) both operands are false. • Example: Logical. Or. java Expression 1 Expression 2 Expression 1 || Expression 2 true false false true Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #10
The ! Operator • The ! operator performs a logical NOT operation. • If an expression is true, !expression will be false. if (!(temperature > 100)) System. out. println(“Below the maximum temperature. "); • If temperature evaluates to false, then the output statement will be run. > 100 Expression 1 !Expression 1 true false true Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #11
Order of Precedence Operators 1 (unary negation) ! 2 */% 3 +- 4 < > <= >= 5 == != 6 && Logical AND 7 || Logical NOT 8 Starting Out With Java 5 Control Structures to Objects = += -= *= /= %= Description Unary negation, logical NOT Multiplication, Division, Modulus Addition, Subtraction Less-than, Greater-than, Less-than or equal to, Greater-than or equal to Is equal to, Is not equal to Assignment and combined assignment operators. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #12
Comparing String Objects • In most cases, you cannot use the relational operators to compare two String objects. • Reference variables contain the address of the object they represent. • Unless the references point to the same object, the relational operators will not return true. • Example: String. Compare. java • Example: String. Compare. To. java Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #13
Ignoring Case in String Comparisons • In the String class the equals and compare. To methods are case sensitive. • In order to compare two String objects that might have different case, use: • equals. Ignore. Case, or • compare. To. Ignore. Case • Example: Secret. Word. java Starting Out With Java 5 Control Structures to Objects Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 3 Slide #14
void Methods and Value. Returning Methods • A void method is one that simply performs a task and then terminates. System. out. println(“Hi!”); • A value-returning method not only performs a task, but also sends a value back to the code that called it. int number = Integer. parse. Int(“ 700”); Starting Out With Java 5 Control Structures to Objects Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Chapter 5 Slide #15
Two Parts of Method Declaration header public static void display. Messsage() { System. out. println(“Hello”); body } Starting Out With Java 5 Control Structures to Objects Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Chapter 5 Chapter Slide #165 Slide #16
Parts of a Method Header Method Modifiers Return Type Method Name Parentheses public static void display. Message () { System. out. println(“Hello”); } Starting Out With Java 5 Control Structures to Objects Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Chapter 5 Slide #17
Passing 5 to the display. Value Method display. Value(5); The argument 5 is copied into the parameter variable num. public static void display. Value(int num) { System. out. println(“The value is “ + num); } The method will display Starting Out With Java 5 Control Structures to Objects The value is 5 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Chapter 5 Slide #18
Passing Multiple Arguments The argument 5 is copied into the num 1 parameter. The argument 10 is copied into the num 2 parameter. show. Sum(5, 10); NOTE: Order matters! public static void show. Sum(double num 1, double num 2) { double sum; //to hold the sum = num 1 + num 2; System. out. println(“The sum is “ + sum); } Starting Out With Java 5 Control Structures to Objects Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Chapter 5 Slide #19
- Slides: 19