lecture 7.1 selection - the if instruction. © 2006 pearson addison-wesley. all rights reserved...

10
Lecture 7.1 Selection - the if Instruction

Upload: archibald-rogers

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

Lecture 7.1

Selection - the if Instruction

Page 2: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.2

An algorithm often needs to make choices…

• choose to print a special warning if the bank balance is negative

• choose whether to move a bouncing ball up or down

• choose how many dots to place on the dice face

Selection instructions allow code to make choices.

Java selection instructions:

Page 3: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.3

Syntaxif (some_condition) { then_clause}

Notes• some_condition must be a valid boolean expression.• then_clause is a sequence of zero or more instructions.• then_clause should be indented as shown• The { ... } enclosure can be eliminated if then_clause is a single instruction.

Semantics some_condition is evaluated and then_clause is executed if and only if some_condition is true

Example (Assume that pay, wage, hours and bonus are double instance variables.)

pay = wage * hours; if (hours > 40) {

pay = pay + (hours - 40) * wage * 0.5; } pay = pay + bonus;

Page 4: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.4

Syntax if (some_condition) { then_clause} else { else_clause}

Notes• some_condition must be a valid boolean expression.• then_clause and else_clause are a sequences of zero or more instructions.• then_clause and else_clause should be indented as shown• The { ... } enclosures can be eliminated for single instruction clauses.

Semantics some_condition is evaluated and then_clause is executed if and only if some_condition is true, otherwise else_clause is executed

Example (Assume score, exam, homework are int instance variables and grade is a char variable.)

score = exam * 25 + homework;if (score > 59) { grade = 'P';} else { grade = 'F';}System.out.println( “Grade is “ + grade );

Page 5: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.5

Finding the maximum of two int variables

Only create a rectangle if width and height are positiveif (theWidth > 0 && theHeight > 0) { theRect = new Rectangle(10, 10, theWidth, theHeight);}

Color an oval green 30% of the time and red 70%if ( ) { theOval.setBackground( Color.green );} else { theOval.setBackground( Color.red );}

Page 6: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.6

Any two primitive expressions of compatible type can be compared using a relational operator, and the result is a boolean (true or false) value.

Operators== equal to (warning: don’t confuse with

=)!= not equal to< less than<= less than or equal to> greater than>= greater than or equal to

Exampleif ( count != 0 ) { average = total / count;}

Page 7: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.7

boolean is a primitive data type for storing logical values.

Infix Operators and (warning: don’t confuse with &) logical (inclusive) or

Constantstruefalse

Prefix Operator not (logical negation)

Operator Precedence (highest to lowest) ++ --

! - (unary negation)

* / %+ -

< <= > >=== !=

&&

||=

Page 8: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.8

Some declarationsprivate int int1, int2;private double dbl1, dbl2;private char letter;

Valid boolean expressions int1 < 100.3

true && (letter == ‘Z’)

! (dbl1 == int2)

(0 < int1) && (int1 < 100)

letter==‘a’ || letter==‘e’ || letter==‘i’ || letter==‘o’ || letter == ‘u’

dbl2 != 0.0 && dbl1/dbl2 < 100.0

Invalid boolean expressions int1 < = int2

‘a’ < letter < ‘z’

int1 = int2

Page 9: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.9

2-Way SelectionBoth if-then and if-then-else forms of the if instruction exhibit 2-way selection, because there are two options at execution time.

Exampleif (examScore > 90) { letterGrade = ‘A’;} else if (examScore <=90 && examScore > 80) { letterGrade = ‘B’;} else if (examScore <=80 && examScore > 70) { letterGrade = ‘C’;} else if (examScore <=70 && examScore > 60) { letterGrade = ‘D’;} else { letterGrade = ‘F’;}

Multi-Way SelectionJava includes a switch instruction that provides for

limited multi-way selection, but using nested if instructions is the

more general solution

Page 10: Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved 7.1.2 An algorithm often needs to make choices… choose

© 2006 Pearson Addison-Wesley. All rights reserved 7.1.10

An else line applies to the most recent if instruction that is not terminated.

Example (Don’t write code this way...)

if (someInt < 10) { System.out.println(“small”); if (secondInt < 10) {

System.out.println(“small again”);} else { System.out.println(“big”);}

The Moral...Indentation of code is important. In general, clauses should be

indented and right parentheses aligned with the notation from which they began.