Presentation is loading. Please wait.

Presentation is loading. Please wait.

if-else-if Statements

Similar presentations


Presentation on theme: "if-else-if Statements"— Presentation transcript:

1 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. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

2 if-else-if Statements
if (expression) statement or block else // Put as many else ifs as needed here Care must be used since else statements match up with the immediately preceding unmatched if statement. Example: TestResults.java Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

3 if-else-if Flowchart Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

4 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: LoanQualifier.java Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

5 Nested if Statement Flowcharts
“...you must have been on job 2 years…” Yes Salary >= 30000? “You must earn at least…” yearsOnJob >= 2 “You qualify.... No Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

6 if-else Matching if (employed == 'y') This else { matches with this
if (recentGrad == 'y') System.out.println("You qualify for the special interest rate."); } else System.out.println("You must be a recent college graduate to qualify."); System.out.println("You must be employed to qualify."); This else matches with this if. This else matches with this if. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

7 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. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

8 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. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

9 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: LogicalAnd.java Expression 1 Expression 2 Expression1 && Expression2 true false Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

10 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: LogicalOr.java Expression 1 Expression 2 Expression1 || Expression2 true false Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

11 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 > 100 evaluates to false, then the output statement will be run. Expression 1 !Expression1 true false Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

12 Order of Precedence Order of Precedence Operators Description 1
(unary negation) ! Unary negation, logical NOT 2 * / % Multiplication, Division, Modulus 3 + - Addition, Subtraction 4 < > <= >= Less-than, Greater-than, Less-than or equal to, Greater-than or equal to 5 == != Is equal to, Is not equal to 6 && Logical AND 7 || Logical NOT 8 = += -= *= /= %= Assignment and combined assignment operators. Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

13 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: StringCompare.java Example: StringCompareTo.java Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

14 Ignoring Case in String Comparisons
In the String class the equals and compareTo methods are case sensitive. In order to compare two String objects that might have different case, use: equalsIgnoreCase, or compareToIgnoreCase Example: SecretWord.java Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

15 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.parseInt(“700”); Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

16 Two Parts of Method Declaration
header public static void displayMesssage() { System.out.println(“Hello”); } body Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Chapter 5 Slide #16 Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

17 Parts of a Method Header
Method Modifiers Return Type Method Name Parentheses public static void displayMessage () { System.out.println(“Hello”); } Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

18 Passing 5 to the displayValue Method
public static void displayValue(int num) { System.out.println(“The value is “ + num); } The argument 5 is copied into the parameter variable num. The method will display The value is 5 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis

19 Passing Multiple Arguments
showSum(5,10); public static void showSum(double num1, double num2) { double sum; //to hold the sum sum = num1 + num2; System.out.println(“The sum is “ + sum); } The argument 5 is copied into the num1 parameter. The argument 10 is copied into the num2 parameter. NOTE: Order matters! Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Starting Out With Java 5 Control Structures to Objects By Tony Gaddis


Download ppt "if-else-if Statements"

Similar presentations


Ads by Google