Week 3 - Friday What did we talk about last time? Wrapper classes - - PowerPoint PPT Presentation

week 3 friday what did we talk about last time wrapper
SMART_READER_LITE
LIVE PREVIEW

Week 3 - Friday What did we talk about last time? Wrapper classes - - PowerPoint PPT Presentation

Week 3 - Friday What did we talk about last time? Wrapper classes Examples if statements So far we have only considered Java programs that do one thing after another, in sequence Our programs have not had the ability to


slide-1
SLIDE 1

Week 3 - Friday

slide-2
SLIDE 2

 What did we talk about last time?  Wrapper classes  Examples  if statements

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6

 So far we have only considered Java programs that do one

thing after another, in sequence

 Our programs have not had the ability to choose between

different possibilities

 Now, they will!

slide-7
SLIDE 7

 The if-statement:  x is small will only print out if x is less than 5  In this case, we know that it is, but x could come from user

input or a file or elsewhere

int x = 4; if( x < 5 ) { System.out.println("x is small!"); }

slide-8
SLIDE 8

The if part Any boolean expression Executable statements

if( condition ){ statements; }

slide-9
SLIDE 9

 A very natural if-then sort of relationship  If the condition is true, then do something  For example:

  • If I win a million dollars,
  • Then I’ll yodel like an insane Swiss monkey
slide-10
SLIDE 10

 Write a program that prompts the user for the secret

password

 If they enter the word "eggplant", congratulate them for

knowing the password

slide-11
SLIDE 11
slide-12
SLIDE 12

 Any statement that evaluates to a boolean is legal  Examples:

  • x == y
  • true
  • Character.isDigit('r')
  • s.equals("Help me!") && (z < 4)
slide-13
SLIDE 13

 The most common condition you will find is a comparison

between two things

 In Java, that comparison can be:

  • ==

equals

  • !=

does not equal

  • <

less than

  • <=

less than or equal to

  • >

greater than

  • >=

greater than or equal to

 These are called relational operators

slide-14
SLIDE 14

 You can use the == operator to compare any two things of the

same type

 Different numerical types can be compared as well (3 ==

3.0)

 Be careful with double types, 0.33333333 is not equal to

0.33333332

int x = 3; if( x == 4 ) { System.out.println("This doesn't print"); }

slide-15
SLIDE 15

 Any place you could have used the == operator, you can use

the != operator

 If == gives true, the != operator will always give false, and

vice versa

 If you want to negate a condition, you can always use the ! as

a not is the same as

if( x != 4 ) if( !(x == 4) )

slide-16
SLIDE 16

 Remember, a single equal sign (=) is the assignment operator

(think of a left-pointing arrow)

 A double equals (==) is a comparison operator

int y = 10; if( y = 6 ) // compiler error! boolean b = false; if( b = false ) // no error but wrong

slide-17
SLIDE 17

 Inequality is very important in programming  You may want to take an action as long as a value is below a

certain threshold

 For example, you might want to keep bidding at an auction

until the price is greater than what you can afford

 Watch for strict inequality (<) vs. non-strict inequality (<=)

if( x <= 4 ) { System.out.println("x is less than 5"); }

slide-18
SLIDE 18

 Just like less than or equal to, except the opposite  Note that (because of the All-Powerful Math Gods) the

  • pposite of <= is > and the opposite of >= is <

 Thus,

  • !( x <= y ) is equivalent to ( x > y )
  • !( x >= y ) is equivalent to ( x < y )
slide-19
SLIDE 19
slide-20
SLIDE 20

 Sometimes you have to make a decision  If a condition is true, you go one way, if not, you go the other  For example:

  • If I pass COMP 1600,

▪ Then I throw a kegger to celebrate

  • Otherwise,

▪ I punch Dr. Wittman in the face

slide-21
SLIDE 21

 Notice the nature of this kind of condition  Both outcomes cannot happen  Either a kegger gets thrown or Dr. Wittman gets punched in

the face

 For these situations, we use the else construct

slide-22
SLIDE 22

Two different

  • utcomes

if( condition ) { statements1; } else { statements2; }

slide-23
SLIDE 23

Scanner in = new Scanner(System.in); int balance = in.nextInt(); if( balance < 0 ) { System.out.println("You are in debt!"); } else { System.out.println("You have $" + balance); }

slide-24
SLIDE 24
slide-25
SLIDE 25

 You can have one or many statements inside the braces of an if or an else  However, you can optionally leave off the braces if there's only a single

statement

 It's probably a good idea to use the braces until you really know what you're

doing

 Some programmers always use them

if( x == 4 ) { System.out.println("I hate 4"); } if( x == 4 ) System.out.println("I hate 4"); // braces not needed

slide-26
SLIDE 26

 Sometimes you want to make one set of decisions based on

another set of decisions

 if-statements can be nested inside the bodies of other if-

statements

 You can put if-statements inside of if-statements inside of

if-statements… going arbitrarily deep

slide-27
SLIDE 27

if( condition1 ){ statement1;

if( condition2 ) {

if( condition3 ) statement2; …

}

}

slide-28
SLIDE 28

 For the next example, recall the 4 quadrants of the Cartesian

coordinate system

x

  • x

y

  • y

(0,0) 1

2 3 4

slide-29
SLIDE 29

 Find which quadrant the point (x,y) is in

if( x >= 0.0 ) { if( y >= 0.0 ) System.out.println("Quadrant 1"); else System.out.println("Quadrant 4"); } else { if( y >= 0.0 ) System.out.println("Quadrant 2"); else System.out.println("Quadrant 3"); }

slide-30
SLIDE 30

 You can list a sequence of exclusive possibilities using nesting:

if( index == 1 ) System.out.println("First"); else if( index == 2 ) System.out.println("Second"); else if( index == 3 ) System.out.println("Third"); else System.out.println(index + "th");

slide-31
SLIDE 31

 A block of code is treated just like one statement  A whole if-else is treated the same

if( … ) statement1; else if( … ) statement2; else statement3; if( … ){ statement1; } else { if( … ) statement2; else statement3; }

=

slide-32
SLIDE 32
slide-33
SLIDE 33

 Examples  Pitfalls with if statements  switch statements

slide-34
SLIDE 34

 Keep reading Chapter 4 of the textbook  Keep working on Project 1 (due tonight!)