csci 144 introduction to
play

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


  1. 1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018

  2. Nested If Statements and Comparing Strings

  3. if-else-if statements

  4. 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.

  5. 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

  6. if - else - if Flowchart

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

  8. Nested if statements

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

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

  11. 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."); }

  12. 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.

  13. 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.

  14. 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

  15. Comparing Strings

  16. Comparing Strings name1 String name1; ? name1 = "Fred Flintstone"; "Fred Flintstone" reference variable

  17. Comparing Strings name1 String name1; 0X0045 0X0045 name1 = "Fred Flintstone"; "Fred Flintstone"

  18. 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) …

  19. 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

  20. 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"

  21. 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

  22. 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!

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

Recommend


More recommend