Flow of Control
Download
1 / 22

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


  • 90 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Flow of Control Recitation – 09/(11,12)/2008 ' - melina


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Flow of Control Recitation – 09/(11,12)/2008

CS 180

Department of Computer Science,

Purdue University


Project 2

  • 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.


If and else parts in braces are preferred for even a single statement.

if (count < 3)

{

total = 0;

}

is better than

if (count < 3)

total = 0;

Statements in Braces


Statements in braces
Statements in Braces

Benefits 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).


Is not always equal
== 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.


Multibranch if else statements
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’;

}


Short circuit evaluation
Short-circuit Evaluation

Can prevent problems.

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

Can produce problems!

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

How to runy.reassign()every time?

Use& and|to replace&& and || .

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

But notice& and && aredifferent!


Input output of boolean values
Input & Output of Boolean Values

Example:

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);


Input output of boolean values1
Input & Output of Boolean Values

Dialog:

false

Enter a boolean value: true

You entered true


The switch statement
The switch Statement

Syntax:

switch (Controlling_Expression)

{

case Case_Label:

Statement(s);

break;

case Case_Label:

default:

}


The switch statement1
Theswitch Statement

View sample program Listing 3.4class MultipleBirths

Sample screen output


The switch statement2
Theswitch Statement

The action for each case typically ends with the word break.

The optionalbreakstatement prevents the consideration of other cases.

The controlling expression can be anything that evaluates to an integral type.


The switch statement3
The switch Statement

Example withoutbreak:

switch (grade)

{

case ‘P’:

System.out.print(“You have”);

case ‘F’:

System.out.print(“…No, you have not”);

default:

}

System.out.println(“passed.”);


Enumerations
Enumerations

Consider a need to restrict contents of a variable to certain values.

An enumeration lists the values a variable can have.

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


Enumerations1
Enumerations

Now possible to use in a switch statement


Enumerations2
Enumerations

An even better choice of descriptive identifiers for the constants.enum MovieRating {EXCELLENT, AVERAGE, BAD}rating = MovieRating.AVERAGE;case EXCELLENT: ...


Specifying a drawing color
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.


Specifying a drawing color1
Specifying a Drawing Color

View sample program, Listing 3.5class YellowFace

Sample screen output


Specifying a drawing color2
Specifying a Drawing Color

Predefined Colors for the setColor Method:


Dialog box for a yes no question
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 labeledyesand no.


Dialog box for a yes no question1
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");


Dialog box for a yes no question2
Dialog Box for a Yes/No Question

A Yes-or No-Dialog Box:


ad