Upload
archibald-rogers
View
212
Download
0
Embed Size (px)
Citation preview
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 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:
© 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;
© 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 );
© 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 );}
© 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;}
© 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)
* / %+ -
< <= > >=== !=
&&
||=
© 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
© 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
© 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.