april 8, 1998cs102-02lecture 2-2 control structures in java i cs 102-02 lecture 2-2 controlling java
TRANSCRIPT
![Page 1: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/1.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Control Structures in Java I
CS 102-02
Lecture 2-2
Controlling Java
![Page 2: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/2.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Control Structures Control Flow
• Program inertia– Java programs start with the first statement– Jump to the next, and the next
until...
• Control structures change the program flow
![Page 3: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/3.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Selection Structures
• Two kinds of selection– If…then: Only do it if the condition’s true– If...then...else: Do one thing or the other
![Page 4: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/4.jpg)
April 8, 1998 CS102-02 Lecture 2-2
If I Had a Hammer...
• In Java syntax:if ( Expression ) Statement
• Expression MUST be a Boolean expression
Statement can be a block of statements
• There’s no then in them thar hills
![Page 5: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/5.jpg)
April 8, 1998 CS102-02 Lecture 2-2
If I Had an Example..
if (pulse >= 0) System.out.println(“Passed”);
if (pulse >= 200)
System.out.println(“Slow down!”);
![Page 6: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/6.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Compound Statements
• If Java expects a statement, you can also use a compound statementif ( Expression ) Statement
• Compound statement is a group of statements, enclosed in {}
![Page 7: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/7.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Compounding a Statement
if (temperature <= 30 && windSpeed > 10) { me.bundleUp(); me.complain(“Chicago”, badWeather); me.tryToGraduate(now); me.returnToWarmerClimes(“I love L.A.!”);}
![Page 8: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/8.jpg)
April 8, 1998 CS102-02 Lecture 2-2
The Declaration of Independence
• When is a Compound Statement a Block? When it contains a declaration.
• A declaration says to Java: “I’m going to use a variable, and it’s going to be of this type.”Graphics graphicsObject;
• Not a particular Graphics object yet
![Page 9: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/9.jpg)
April 8, 1998 CS102-02 Lecture 2-2
A Defining Moment
• Declarations are good, but there’s more
• Definitions associate an initial value with a name
Graphics graphicsObject = new Graphics();
![Page 10: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/10.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Why Do We Care?Block = CompoundStatement+Declaration(s)
• Declarations in a block are special because they have block scope
{ Graphics graphicsObject;// some code goes here
// graphicsObject still exists
}// now it’s gone
![Page 11: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/11.jpg)
April 8, 1998 CS102-02 Lecture 2-2
It’s Minty Fresh
• Scope is where a variable is visible
• More details on scope when we get to methods
• And now, back to our regularly scheduled lecture...
![Page 12: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/12.jpg)
April 8, 1998 CS102-02 Lecture 2-2
A Little Medical Diagnosis
if (pulse >= 200 || pulse <= 0)System.out.println(“Heart beats gang aft agley”);if (respiration > 75)
System.out.println(“You don’t look so good”);else System.out.println(“You look a little flush.”);
Pulse Respiration What’s printed? 68 35 You look a little flush.
203 78 Heart beats gang aft agleyYou don’t look so good
205 40 Heart beats gang aft agleyYou look a little flush.
![Page 13: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/13.jpg)
April 8, 1998 CS102-02 Lecture 2-2
I’d Hammer in the Morning Else I’d...
• In Java, If...Then...Else:
if ( Expression )
IfStatement
else
ElseStatement
![Page 14: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/14.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Pile on the If Statements
if (grade >= 90)
System.out.println(“Wow, an A!”);
else if (grade >= 80)
System.out.println(“Not bad, a B!”);
else if (grade >= 70)
System.out.println(“Hanging in with a C.”);
else if (grade >= 60)
System.out.println(“Oh my, a D.”);
else
System.out.println(“It doesn’t look good”);
![Page 15: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/15.jpg)
April 8, 1998 CS102-02 Lecture 2-2
While You Were Sleeping
• While you were sleeping, I:– Told your family we were engaged– Hit on your brother– Fell in love with him
• See what happens when you doze off for a few days...
![Page 16: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/16.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Variety Might be the Spice of Life...
• Repetition is the meat and potatoes
• Keep going until:– Fixed number of times (count up, count down)– A condition becomes true– A condition becomes false– An exception/error occurs
![Page 17: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/17.jpg)
April 8, 1998 CS102-02 Lecture 2-2
While is a Close Cousin to If
• In Java, repeat with while is:while ( Expression ) Statement
• A brief exampleint product = 2; // Def’n or declaration?
while (product <= 1000)
product = 2 * product;
// What’s product here?
![Page 18: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/18.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Determining a Class Average
• The problem from the book:Develop a class-averaging program that will
process an arbitrary number of letter grades each time the program is run
![Page 19: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/19.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Everything’s a Class
• Build a ClassAverage class in Java
• Create a ClassAverage object
• Invoke its methods
![Page 20: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/20.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Java Applications
• Java: It’s not just for applets anymore
• Browsers need not apply– Applications are programs which can run on
their own.– User interface: console or graphical
![Page 21: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/21.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Data Needs?
• What information does the program need to work?
• What information will be created in running the program?
![Page 22: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/22.jpg)
April 8, 1998 CS102-02 Lecture 2-2
Programming with Verbs
• What actions need to be performed?
• Do we need to break them down into methods?
![Page 23: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/23.jpg)
April 8, 1998 CS102-02 Lecture 2-2
The Data We Needimport java.io.*;
public class Average { public static void main( String args[] ) throws IOException { double average; // number with decimal point int counter, grade, total; // initialization phase total = 0; counter = 0; // processing phase System.out.print( "Enter letter grade, Z to end: " ); grade = System.in.read();
![Page 24: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/24.jpg)
April 8, 1998 CS102-02 Lecture 2-2
What We’re Doing With It while ( grade != 'Z' ) { if ( grade == 'A' ) total = total + 4; else if ( grade == 'B' ) total = total + 3; else if ( grade == 'C' ) total = total + 2; else if ( grade == 'D' ) total = total + 1;
System.in.skip( 2 ); counter = counter + 1; System.out.print( "Enter letter grade, Z to end: " ); grade = System.in.read(); }
![Page 25: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/25.jpg)
April 8, 1998 CS102-02 Lecture 2-2
The End Result
// Termination phase if ( counter != 0 ) { average = (double) total / counter; System.out.println( "Class average is " + average ); } else System.out.println( "No grades were entered" ); }}
![Page 26: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/26.jpg)
April 8, 1998 CS102-02 Lecture 2-2
The Average Program// Fig. 2.9: Average.java// Class average application with// sentinel-controlled repetition.import java.io.*;
public class Average { public static void main( String args[] ) throws IOException { double average; // number with decimal point int counter, grade, total; // initialization phase total = 0; counter = 0; // processing phase System.out.print( "Enter letter grade, Z to end: " ); grade = System.in.read();
while ( grade != 'Z' ) { if ( grade == 'A' ) total = total + 4; else if ( grade == 'B' ) total = total + 3; else if ( grade == 'C' ) total = total + 2; else if ( grade == 'D' ) total = total + 1;
System.in.skip( 2 ); counter = counter + 1; System.out.print( "Enter letter grade, Z to end: " ); grade = System.in.read(); } // termination phase if ( counter != 0 ) { average = (double) total / counter; System.out.println( "Class average is " + average ); } else System.out.println( "No grades were entered" ); }}
![Page 27: April 8, 1998CS102-02Lecture 2-2 Control Structures in Java I CS 102-02 Lecture 2-2 Controlling Java](https://reader036.vdocuments.us/reader036/viewer/2022070401/56649f1b5503460f94c30870/html5/thumbnails/27.jpg)
April 8, 1998 CS102-02 Lecture 2-2
That’s It Until Next Time
• Choosing one branch or another: use if– Watch out for dangling else’s
• Looping with condition: can use while
• Other ways to loop & branch– Use the most specific