Flow of Control Flow of Control Recitation 09/(11,12)/2008 CS 180 - - PowerPoint PPT Presentation

flow of control flow of control
SMART_READER_LITE
LIVE PREVIEW

Flow of Control Flow of Control Recitation 09/(11,12)/2008 CS 180 - - PowerPoint PPT Presentation

Flow of Control Flow of Control Recitation 09/(11,12)/2008 CS 180 CS 180 Department of Computer Science, Purdue University Project 2 j Now posted on the class webpage. Due Wed, Sept. 17 at 10 pm. Start early All


slide-1
SLIDE 1

Flow of Control Flow of Control

Recitation – 09/(11,12)/2008

CS 180 CS 180 Department of Computer Science, Purdue University

slide-2
SLIDE 2

Project 2

  • Now posted on the class webpage.

j

  • Due Wed, Sept. 17 at 10 pm.
  • Start early All questions on the class

Start early. All questions on the class newsgroup.

  • Evening consulting hours
  • Evening consulting hours.

LWSN B146. MTW 7-10 pm.

22

slide-3
SLIDE 3

Statements in Braces

  • If and else parts in braces are preferred for even

a single statement a single statement.

if (count < 3) { total = 0; }

is better than is better than

if (count < 3) total = 0;

slide-4
SLIDE 4

Statements in Braces

Benefits to do this: Decreases the chance of introducing a bug when

  • Decreases the chance of introducing a bug when

adding a new statement.

  • Makes the program a little more “self-

p g documenting” (more readable and easier to maintain).

slide-5
SLIDE 5

== Is NOT Always Equal y q

  • == should not be used to determine if two
  • bjects have the same value including two
  • bjects have the same value, including two

strings

  • Even though (s1 == s2) might appear to work,

d t thi do not use this.

  • Use (s1.equals(s2)) instead to compare two

strings. strings.

slide-6
SLIDE 6

Multibranch if-else Statements

  • if-else if-else is different from a series of

if statements if statements.

if (score >= 60) { if (score >= 60) { { result = ‘P’; } else if (score > 0) { result = ‘P’; } if (score > 0) { result = ‘F’; } else { result = ‘F’; } else else { result = ‘Z’; } else { result = ‘Z’; }

slide-7
SLIDE 7

Short-circuit Evaluation

  • Can prevent problems.

if ((number != 0) && (sum/number > 5)) if ((number != 0) && (sum/number > 5))

  • Can produce problems!

if ((x evaluate()) && (y reassign())) if ((x.evaluate()) && (y.reassign()))

  • How to run y.reassign() every time?
  • Use & and | to replace && and || .

if ((x.evaluate()) & (y.reassign())) (( ()) (y g ()))

  • But notice & and && are different!
slide-8
SLIDE 8

Input & Output of Boolean Values p p

Example:

boolean booleanVar = false; boolean booleanVar = false; System.out.println(booleanVar); System.out.println("Enter a boolean value:"); Scanner keyboard = new Scanner(System in); Scanner keyboard = new Scanner(System.in); booleanVar = keyboard.nextBoolean(); System.out.println("You entered " + booleanVar);

slide-9
SLIDE 9

Input & Output of Boolean Values p p

Dialog:

f l false Enter a boolean value: true Y t d t You entered true

slide-10
SLIDE 10

The switch Statement

Syntax:

switch (Controlling Expression) switch (Controlling_Expression) { case Case_Label: Statement(s); break; case Case_Label: _ … default: … }

slide-11
SLIDE 11

The switch Statement

View sample program Listing 3.4 class MultipleBirths class MultipleBirths

Sample Sample p screen

  • utput

p screen

  • utput
slide-12
SLIDE 12

The switch Statement

  • The action for each case typically ends with the

word break word break.

  • The optional break statement prevents the

consideration of other cases.

  • The controlling expression can be anything that

evaluates to an integral type.

slide-13
SLIDE 13

The switch Statement

Example without break:

switch (grade) switch (grade) { case ‘P’: System.out.print(“You have”); case ‘F’: System.out.print(“…No, you have not”); default: } System out println(“passed ”); System.out.println( passed. );

slide-14
SLIDE 14

Enumerations

  • Consider a need to restrict contents of a variable

to certain values to certain values.

  • An enumeration lists the values a variable can

have.

  • Example

enum MovieRating {E, A, B} MovieRating rating; rating = MovieRating.A;

slide-15
SLIDE 15

Enumerations

  • Now possible to use in a switch statement
slide-16
SLIDE 16

Enumerations

  • An even better choice of descriptive identifiers for

the constants the constants.

enum MovieRating {EXCELLENT, AVERAGE, BAD} { , , } rating = MovieRating.AVERAGE; case EXCELLENT: ...

slide-17
SLIDE 17

Specifying a Drawing Color p y g

g

  • When drawing a shape inside an applet’s paint

method think of the drawing being done with a 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
  • Drawings done later appear on top of drawings

done earlier.

slide-18
SLIDE 18

Specifying a Drawing Color p y g

g

View sample program, Listing 3.5 class YellowFace class YellowFace

Sample screen

  • utput

Sample screen

  • utput
slide-19
SLIDE 19

Specifying a Drawing Color p y g g

Predefined Colors for the setColor Method:

slide-20
SLIDE 20

Dialog Box for a Yes/No Question g

  • Used to present the user with a yes/no question.

The window contains

  • The window contains
  • The question text
  • Two buttons labeled yes and no.

Two buttons labeled yes and no.

slide-21
SLIDE 21

Dialog Box for a Yes/No Question g

Example:

int answer = int answer = JOptionPane.showConfirmDialog(null, "End program?", "Click Yes or No:" "Click Yes or No:", JOptionPane.YES_NO_OPTION); if (answer == JOptionPane.YES_OPTION) System.exit(0); System.exit(0); else if (answer == JOptionPane.NO_OPTION) System.out.println("One more time");

slide-22
SLIDE 22

Dialog Box for a Yes/No Question g

A Yes-or No-Dialog Box: