Branching Chapter 5 11/14/16 & 11/15/16 1 1
Chapter Contents Flow of Control The if Statement Basic Comparisons Compound Statements Basic Comparisons Comparing Primitives Comparing Objects 2 2
Chapter Contents The if-else Statement Logical Operators 3 3 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 3 3
Flow of Control Statements normally executed in sequence From first to last This sequence can be altered A statement may call another method Decision statements cause different sequences to be followed Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 4 4
Flow of Control Figure 7-1 Invoking a method 5 5 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 5 5
If Statement int broncos = 24; int lobos = 31; if(broncos < lobos) System.out.println("Broncos lose!"); System.out.println("Good Night!"); 6 6
The if Statement Specify a condition Syntax If condition is true one or more statements execute If condition is false these statements skipped Syntax Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 7 7
The if Statement Figure 7-2 The logic of an if statement 8 8
Basic Comparisons Figure 7-3 Relational operators that compare primitive values Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 9 9
Write a Program Write a program to ask the user for her age. Output “Old Enough to Join the Army” if she is 18 or older. Next output a message that says “Army Strong.”. 10 10
Participation Write a class called Thermometer. Give the class a parameterized constructor that initializes the temperature. If the value passed to the constructor is greater that 135, output “Record High!” and set the temperature to the passed value, otherwise just the temperature to the value with no output. No need to write any more methods. Write a tester to test the constructor. 11 11
Compound Statements When if statement requires multiple statements to execute Enclose those multiple statements within braces { } after if (condition) if(resp == YES){ System.out.println("Evaluate Launch Conditions."); System.out.println("Respond as prompted"); System.out.println("call 888-555-5555"); } Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 12 12
Comparing Objects Methods equals for String Thus, == operator Object variable does not contain object Object variable contains reference to it Thus, == operator Compares references Not values Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 13 13
Comparing Objects String friend = "Sam"; //String with same data String name = new String("Sam"); if(friend != name) //Compares addresses System.out.println("Not the same object!"); if(friend.equals(name)) System.out.println ("Words in objects are equal."); 14 14
Comparing Objects Distinct references to strings whose values are the same 15 15
Comparing Objects Two variables containing equal references to the same string 16 16
If-else & Logical Operators 11/30/15 & 12/01/15
A Program with else A program that asks the user whether the user wants a quote by Turing or another quote. For Turing output:"Machines take me by surprise with great frequency.- Turing" For other output:"Intelligence is the ability to adapt to change.-Hawking" BrainyQuotes.java 18 18
import java.util.Scanner; public class BrainyQuotes { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Would you like a Turing quote?"); String ans = scan.next(); if(ans.equals("yes")) System.out.println("Machines take me" + " by surprise with great frequency.--Turing"); else System.out.println("Intelligence is" + " the ability to adapt to change.--Hawking"); scan.close(); }
The if-else Statement When you wish to do one thing if the condition is true But you want to do something else when the condition is false Syntax Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 20 20
The if-else Statement The logic of an if-else statement 21 21 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 21 21
Write an if-else Statement Write an if-else statement that assigns 0.1 to commission unless sales is greater that or equal to 50000.0, in which case it assigns 0.2 to commission.
Logical Operators 23 23
Logical Operators Logical operators Used to combine boolean expressions Extra parenthesis are optional. 20 <= age < 30 doesn't work Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 24 24
Logical Operators The operator && 25 25 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 25 25
Logical Operators The operator || 26 26 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 26 26
Logical Operators The operator ! ! true -> false !("Joe".equals("Joe")) ! false -> true !("Sam".equals("Joe")) Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 27 27
Leap Year A year is a leap year if it is Divisible by 4 but not divisible by 100 Or divisible by 400 Write a program to input the year. It should output a message about whether it is a leap year.
Precedence for Ops Order of operations in a logical expression 29 29 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 29 29
Wrap Up Question How do you best compare two Strings to see if they are equal? Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 30 30
Participation Write a program to input the measures of three sides of a triangle. Output a statement the says it is either “equilateral” or “not equilateral” after comparing the sides. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 31 31
Branching Chapter 5 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 32 32