1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018
Nested If Statements and Comparing Strings
if-else-if statements
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 if ( expression ) statement or block else if ( expression ) else statements statement or block match up with immediately // Put as many else ifs as needed here preceding else unmatched if statement or block
if - else - if Flowchart
Guided Worked Example Enter your score: 86 Your grade on the assignment is: B
Nested if statements
Nested if Flowchart No Yes Is it cold outside? Wear shorts. Is it Yes No snowing? Wear a jacket. Go skiing.
Nested if LoanQualifier Example No Yes Are you employed? Not qualified Yes No A recent graduate? Not qualified You qualify
Nested if LoanQualifier Example if (employed == ′y′) { Curly braces if (recentGrad == ′y′) not required { for single System.out.println("You qualify for the " + statements "special interest rate."); } improve else { readability System.out.println("You must be a recent " + watch "college graduate to qualify."); indentation! } } else { System.out.println("You must be employed to qualify."); }
if - else Matching if (employed == ′y′) This else { matches if (recentGrad == ′y′) { with this System.out.println("You qualify for the " + if. "special interest rate."); } else { System.out.println("You must be a recent " + "college graduate to qualify."); } } else This else { matches System.out.println("You must be employed to qualify."); with this } if.
CSCI Departmental Scholarship Eligibility No Yes CS or CE major? Not eligible Yes No GPA at least 3.2? Not eligible 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 or CE major? (yes or no): no I’m sorry, you must be a CS or CE major to qualify Are you a CS or CE major? (yes or no): yes What is your GPA: 2.15 Ineligible due to GPA Are you a CS or CE major? (yes or no): yes What is your GPA: 3.5 You are eligible for scholarship consideration
Comparing Strings
Comparing Strings name1 String name1; ? name1 = "Fred Flintstone"; "Fred Flintstone" reference variable
Comparing Strings name1 String name1; 0X0045 0X0045 name1 = "Fred Flintstone"; "Fred Flintstone"
Comparing Strings name1 String name1; 0X0045 name1 = "Fred Flintstone"; "Fred Flintstone" String name2; name2 name2 = name1; Only true if they refer to the same object if (name1 == name2) …
Comparing Strings name1 String name1; name1 = "Fred Flintstone"; "Fred Flintstone" String name2; name2 name2 = "Fred Flintstone"; Otherwise it is false even if the strings contain the same if (name1 == name2) characters … "Fred Flintstone" if (name1.equals(name2)) … Use the String class method equals instead It compares the characters in the string
Comparing Strings name1 String name1; name1 = "Leslie Knope"; "Fred Flintstone" String name2; name2 compares strings name2 = "LESLIE KNOPE"; but ignores case if (name1.equalsIgnoreCase(name2)) … "FRED FLINTSTONE"
Comparing Strings returns an integer value indicating the alphabetical ordering of the two strings String name1, name2; name1.compareTo(name2) name1 = "Fred Flintstone"; if name1 is if name1 is if name1 is name2 = "Betty Rubble" alphabetically less than alphabetically equal alphabetically greater than name2 compareTo to name2 compareTo name2 compareTo returns a negative value returns zero returns a positive value name1 = "Barney Rubble" -4 -3 -2 -1 0 1 2 3 4 There is also a compareToIgnoreCase
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.
Recommend
More recommend