CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation

csci 144 introduction to
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CSCI 144 - Introduction to Computer Science

Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018

1

slide-2
SLIDE 2

Nested If Statements and Comparing Strings

slide-3
SLIDE 3

if-else-if statements

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

if-else-if Flowchart

slide-7
SLIDE 7

Guided Worked Example

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

slide-8
SLIDE 8

Nested if statements

slide-9
SLIDE 9

Nested if Flowchart

Wear a jacket. Yes Is it cold

  • utside?

Wear shorts. Is it snowing? Go skiing. No No Yes

slide-10
SLIDE 10

Nested if LoanQualifier Example

Not qualified Yes Are you employed? Not qualified A recent graduate? You qualify No No Yes

slide-11
SLIDE 11

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!

slide-12
SLIDE 12

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.

slide-13
SLIDE 13

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.

slide-14
SLIDE 14

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

slide-15
SLIDE 15

Comparing Strings

slide-16
SLIDE 16

Comparing Strings

String name1; name1 ? name1 = "Fred Flintstone";

"Fred Flintstone" reference variable

slide-17
SLIDE 17

Comparing Strings

0X0045

name1

0X0045

String name1; name1 = "Fred Flintstone";

"Fred Flintstone"

slide-18
SLIDE 18

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"

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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"

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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!

slide-23
SLIDE 23

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