Flow of Control Recitation – 09/(11,12)/2008
description
Transcript of Flow of Control Recitation – 09/(11,12)/2008
![Page 1: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/1.jpg)
Flow of Control Recitation – 09/(11,12)/2008
CS 180Department of Computer Science, Purdue University
![Page 2: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/2.jpg)
• Now posted on the class webpage.
• Due Wed, Sept. 17 at 10 pm.
• Start early. All questions on the class newsgroup.
• Evening consulting hours.
LWSN B146. MTW 7-10 pm.
Project 2
![Page 3: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/3.jpg)
• If and else parts in braces are preferred for even a single statement.if (count < 3){
total = 0;}is better thanif (count < 3)
total = 0;
Statements in Braces
![Page 4: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/4.jpg)
Statements in BracesBenefits to do this:• Decreases the chance of introducing a bug when
adding a new statement.• Makes the program a little more “self-
documenting” (more readable and easier to maintain).
![Page 5: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/5.jpg)
== Is NOT Always Equal • == should not be used to determine if two
objects have the same value, including two strings
• Even though (s1 == s2) might appear to work, do not use this.
• Use (s1.equals(s2)) instead to compare two strings.
![Page 6: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/6.jpg)
Multibranch if-else Statements• if-else if-else is different from a series of
if statements.
if (score >= 60){
result = ‘P’;
}else if (score > 0){
result = ‘F’;
}else{
result = ‘Z’;
}
if (score >= 60){
result = ‘P’;
}if (score > 0){
result = ‘F’;
}else{
result = ‘Z’;
}
![Page 7: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/7.jpg)
Short-circuit Evaluation• Can prevent problems.if ((number != 0) && (sum/number > 5))
• Can produce problems!if ((x.evaluate()) && (y.reassign()))
• How to run y.reassign() every time?• Use & and | to replace && and || . if ((x.evaluate()) & (y.reassign()))
• But notice & and && are different!
![Page 8: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/8.jpg)
Input & Output of Boolean ValuesExample:
boolean booleanVar = false;System.out.println(booleanVar);System.out.println("Enter a boolean value:");Scanner keyboard = new Scanner(System.in);booleanVar = keyboard.nextBoolean();System.out.println("You entered " + booleanVar);
![Page 9: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/9.jpg)
Input & Output of Boolean ValuesDialog:falseEnter a boolean value: trueYou entered true
![Page 10: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/10.jpg)
The switch StatementSyntax:
switch (Controlling_Expression){
case Case_Label:Statement(s);break;
case Case_Label:…default:…
}
![Page 11: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/11.jpg)
The switch Statement
View sample program Listing 3.4 class MultipleBirths
Sample screen output
![Page 12: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/12.jpg)
The switch Statement• The action for each case typically ends with the
word break.• The optional break statement prevents the
consideration of other cases.• The controlling expression can be anything that
evaluates to an integral type.
![Page 13: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/13.jpg)
The switch Statement
Example without break:switch (grade){
case ‘P’:System.out.print(“You have”);
case ‘F’:System.out.print(“…No, you have not”);
default:}System.out.println(“passed.”);
![Page 14: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/14.jpg)
Enumerations• Consider a need to restrict contents of a variable
to certain values.• An enumeration lists the values a variable can
have.• Example
enum MovieRating {E, A, B}MovieRating rating;rating = MovieRating.A;
![Page 15: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/15.jpg)
Enumerations• Now possible to use in a switch statement
![Page 16: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/16.jpg)
Enumerations• An even better choice of descriptive identifiers for
the constants.
enum MovieRating {EXCELLENT, AVERAGE, BAD}rating = MovieRating.AVERAGE;
case EXCELLENT: ...
![Page 17: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/17.jpg)
Specifying a Drawing Color• When drawing a shape inside an applet’s paint
method, think of the drawing being done with a pen that can change colors.
• The method setColor changes the color of the "pen."canvas.setColor(Color.YELLOW);
• Drawings done later appear on top of drawings done earlier.
![Page 18: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/18.jpg)
Specifying a Drawing ColorView sample program, Listing 3.5
class YellowFace
Sample screen output
![Page 19: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/19.jpg)
Specifying a Drawing ColorPredefined Colors for the setColor Method:
![Page 20: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/20.jpg)
Dialog Box for a Yes/No Question• Used to present the user with a yes/no question.• The window contains
The question text Two buttons labeled yes and no.
![Page 21: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/21.jpg)
Dialog Box for a Yes/No Question
Example:int answer = JOptionPane.showConfirmDialog(null, "End program?", "Click Yes or No:",
JOptionPane.YES_NO_OPTION);if (answer == JOptionPane.YES_OPTION) System.exit(0);else if (answer == JOptionPane.NO_OPTION) System.out.println("One more time");
![Page 22: Flow of Control Recitation – 09/(11,12)/2008](https://reader035.fdocuments.net/reader035/viewer/2022062521/56814bce550346895db8a5e4/html5/thumbnails/22.jpg)
Dialog Box for a Yes/No Question
A Yes-or No-Dialog Box: