Nested If Statements and Comparing Strings ifelseif statements

  • Slides: 23
Download presentation
Nested If Statements and Comparing Strings

Nested If Statements and Comparing Strings

if-else-if statements

if-else-if statements

if it is very cold, wear a heavy coat, else, if it is chilly,

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.

if-else-if Syntax else statements match up with immediately preceding unmatched if if (expression) statement

if-else-if Syntax else statements match up with immediately preceding unmatched if if (expression) statement or block else if (expression) statement or block // Put as many else ifs as needed here else statement or block

if-else-if Flowchart

if-else-if Flowchart

Guided Worked Example Enter your score: 86 Your grade on the assignment is: B

Guided Worked Example Enter your score: 86 Your grade on the assignment is: B

Nested if statements

Nested if statements

Nested if Flowchart No Yes Is it cold outside? Wear shorts. No Wear a

Nested if Flowchart No Yes Is it cold outside? Wear shorts. No Wear a jacket. Is it snowing? Yes Wear a parka.

Nested if Loan. Qualifier Example No Yes Are you employed? Not qualified No Not

Nested if Loan. Qualifier Example No Yes Are you employed? Not qualified No Not qualified A recent graduate? Yes You qualify

Nested if Loan. Qualifier Example Curly braces not required for single statements improve readability

Nested if Loan. Qualifier Example Curly braces not required for single statements improve readability watch indentation! if (employed == ′y′) { if (recent. Grad == ′y′) { System. out. println("You qualify for the " + "special interest rate. "); } else { System. out. println("You must be a recent " + "college graduate to qualify. "); } } else { System. out. println("You must be employed to qualify. "); }

if-else Matching This else matches with this if. if (employed == ′y′) { if

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 { System. out. println("You must be a recent " + "college graduate to qualify. "); } } else { System. out. println("You must be employed to qualify. "); }

CS Departmental Scholarship Eligibility No Yes CS major? Not eligible No Not eligible GPA

CS Departmental Scholarship Eligibility No Yes CS major? Not eligible No Not eligible GPA at least 3. 2? Yes Eligible Note: the 3. 2 GPA is just an example and not an actual cutoff used by the department.

Guided Worked Example Are you a CS major? (yes or no): no I’m sorry,

Guided Worked Example Are you a CS major? (yes or no): no I’m sorry, you must be a CS major to qualify Are you a CS major? (yes or no): yes What is your GPA: 2. 15 Ineligible due to GPA Are you a CS major? (yes or no): yes What is your GPA: 3. 5 You are eligible for scholarship consideration

Comparing Strings

Comparing Strings

Comparing Strings name 1 String name 1; ? reference variable "Leslie Knope" name 1

Comparing Strings name 1 String name 1; ? reference variable "Leslie Knope" name 1 = "Leslie Knope";

Comparing Strings name 1 0 X 0045 String name 1; 0 X 0045 "Leslie

Comparing Strings name 1 0 X 0045 String name 1; 0 X 0045 "Leslie Knope" name 1 = "Leslie Knope";

Comparing Strings name 1 String name 1; "Leslie Knope" name 2 Only true if

Comparing Strings name 1 String name 1; "Leslie Knope" name 2 Only true if they refer to the same object name 1 = "Leslie Knope"; String name 2; name 2 = name 1; if (name 1 == name 2) …

Comparing Strings name 1 String name 1; "Leslie Knope" name 2 Otherwise it is

Comparing Strings name 1 String name 1; "Leslie Knope" name 2 Otherwise it is false even if the strings contain the same characters "Leslie Knope" name 1 = "Leslie Knope"; String name 2; name 2 = "Leslie Knope"; if (name 1 == name 2) …

Comparing Strings name 1 String name 1; "Leslie Knope" name 2 returns true if

Comparing Strings name 1 String name 1; "Leslie Knope" name 2 returns true if the strings contain same characters "Leslie Knope" name 1 = "Leslie Knope"; String name 2; name 2 = "Leslie Knope"; if (name 1. equals(name 2)) …

Comparing Strings name 1 String name 1; name 1 = "Leslie Knope"; "Leslie Knope"

Comparing Strings name 1 String name 1; name 1 = "Leslie Knope"; "Leslie Knope" String name 2; name 2 compares strings but ignores case name 2 = "LESLIE KNOPE"; if (name 1. equals. Ignore. Case(name 2)) … "LESLIE KNOPE"

Comparing Strings returns an integer value indicating the alphabetical ordering of the two strings

Comparing Strings returns an integer value indicating the alphabetical ordering of the two strings String name 1, name 2; name 1. compare. To(name 2) name 1 = "Leslie Knope"; name 2 = "Ann Perkins" name 1 = "Andy Dwyer" if name 1 is alphabetically less than name 2 compare. To returns a negative value -4 -3 if name 1 is alphabetically equal to name 2 compare. To returns zero -2 -1 0 1 There is also a compare. To. Ignore. Case if name 1 is alphabetically greater than name 2 compare. To returns a positive value 2 3 4

Guided Worked Example Enter the first name: Connor Enter the second name: Claire The

Guided Worked Example Enter the first name: Connor Enter the second name: Claire The name Claire comes before the name Connor Enter the first name: Connor Enter the second name: Grant The name Connor comes before the name Grant Enter the first name: Grant Enter the second name: Grant The two names are equal!

Reference: Flowcharts adapted from Starting Out with Java by Tony Gaddis Chapter 3 Slides.

Reference: Flowcharts adapted from Starting Out with Java by Tony Gaddis Chapter 3 Slides.