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