Branching Chapter 5 11/14/16 & 11/15/16
Chapter Contents Flow of Control The if Statement Compound Statements Basic Comparisons Comparing Primitives Comparing Objects
Chapter Contents The if-else Statement Logical Operators
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
Flow of Control Figure 7-1 Invoking a method
If Statement int broncos = 24; int lobos = 31; if(broncos < lobos) System.out.println("Broncos lose!"); System.out.println("Good Night!");
The if Statement Specify a condition If condition is true one or more statements execute If condition is false these statements skipped Syntax
The if Statement Figure 7-2 The logic of an if statement
Basic Comparisons Figure 7-3 Relational operators that compare primitive values
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..
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!. No need to write any more methods. Write a tester to test the constructor.
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"); }
Comparing Objects Methods equals for String Object variable does not contain object Object variable contains reference to it Thus, == operator Compares references Not values
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.");
Comparing Objects Distinct references to strings whose values are the same
Comparing Objects Two variables containing equal references to the same string
If-else & Logical Operators
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
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(); } //Note:next( ) inputs a String up to the next whitespace.
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
The if-else Statement The logic of an if-else statement
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
Logical Operators Logical operators Used to combine boolean expressions Extra parenthesis are optional. 20 <= age < 30 doesn't work
Logical Operators The operator &&
Logical Operators The operator
Logical Operators The operator!! true -> false!("joe".equals("joe"))! false -> true!("sam".equals("joe"))
Precedence for Ops Order of operations in a logical expression
Wrap Up Question 1)How do you best compare two Strings to see if they are equal?
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.
import java.util.scanner; public class Equilateral { public static void main(string[] args) { Scanner scan = new Scanner(System.in); //Input the side measures System.out.print("Enter Side A:"); double sidea = scan.nextdouble(); System.out.print("Enter Side B:"); double sideb = scan.nextdouble(); System.out.print("Enter Side C:"); double sidec = scan.nextdouble(); //Find out if it is equilateral } }
Branching Chapter 5