Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alice in Action with Java Chapter 10 Flow Control in Java.

Similar presentations


Presentation on theme: "Alice in Action with Java Chapter 10 Flow Control in Java."— Presentation transcript:

1 Alice in Action with Java Chapter 10 Flow Control in Java

2 Alice in Action with Java2 Objectives Learn how to use the if statement Learn how to use the switch statement Learn how to use the while loop Learn how to use the for loop Learn how to use the do loop

3 Alice in Action with Java3 Flow Control In Java Purpose of using selective and repetitive execution –Implement methods that produce complex behaviors Selective flow control statements: if and switch Repetitive flow control statements: while, for, do

4 Alice in Action with Java4 Selective Execution Directing flow based on the value of a condition Two statements that provide selective execution –if statement: general-purpose selection structure –switch statement: multi-branch selection structure

5 Alice in Action with Java5 Java’s if Statement General-purpose selection structure Selects from one or more groups of statements The else portion of the if statement is optional One-branch if : envisioned as branching flow Two-branch if : flow follows one of two branches

6 Alice in Action with Java6 Java’s if Statement (continued)

7 Alice in Action with Java7 Java’s if Statement (continued)

8 Alice in Action with Java8 Java’s if Statement (continued) Pattern for Java’s if statement: if(Condition)Statement 1 [else Statement 2 ] –Condition : any boolean expression –Statement i : set of Java statements within { } –Brackets are optional if only one statement is in path Multi-branch if statements (aka if-else chain) –Flow can move along multiple paths –Nest additional if statements in the else clause

9 Alice in Action with Java9 Java’s if Statement (continued)

10 Alice in Action with Java10

11 Alice in Action with Java11 Repetitive Execution Program execution flows sequentially by default if and switch perform statements selectively Repetitive execution: control flows in loops Three repetition statements: while, for, and do

12 Alice in Action with Java12 Java’s while Statement Used for processing a series of values Input loops: read and process a series of values Sentinel-controlled input loops –Utilize a sentinel (invalid value) to falsify a condition Problem: extract the initials in a name Members implemented in the Initials class –Instance variables called myName and myInitials –Constructor to initialize the instance variables –Methods to return myName and myInitials

13 Alice in Action with Java13 Java’s while Statement (continued) General pattern for Java’s while statement while(Condition)Statement –Statement comprises one or more statements –Curly braces ({ }) required with multiple statements How it works –Condition is evaluated – If true Statement is executed Go back to evaluate the Condition –If false Go to next statement in program

14 Alice in Action with Java14 while Statement Flow

15 Alice in Action with Java15 String Tokenizer StringTokenizer class : used to split String s Overview of the Initials class constructor –String argument ( name ) is passed to constructor –Instance variables are initialized –StringTokenizer object called names is initialized –names and while loop are used to extract initials

16 Alice in Action with Java16 Initials class

17 Alice in Action with Java17 Java’s for Statement Repetition structure for solving counting problems Counting input loop –Simpler design than the sentinel-controlled input loop –Provides repetition when number of inputs is fixed Illustration: computing the city’s air pollution index –A for loop counts from 1 to NUM_READINGS –Each iteration gets a reading and adds it to sum –After loop terminates, index is computed and output Java’s for loop is very flexible –Example: the Java for loop can count down

18 Alice in Action with Java18 Counting Up

19 Alice in Action with Java19 NinetyNineBottles

20 Alice in Action with Java20 Nested Loops TextGraphics class illustrates nested for loops Understanding drawBox() in TextGraphics class –Method takes two arguments for height and width –Outer for loop counts the rows (builds the height) –Inner for loop prints asterisk symbol through width General pattern for Java’s for loop for (InitialExpr; Condition; ChangeExpr) Statement –Curly braces ({ }) required with multiple statements –Scope of loop control variable goes to end of loop only

21 Alice in Action with Java21 TextGraphics

22 Alice in Action with Java22 for Statement Flow

23 Alice in Action with Java23 Post-Test Loop In while and for statements, the condition is evaluated before the loop body is executed Sometimes you have code that needs to get executed at least once –Execute the body –Evaluate the condition Example GuessingGame The do-while statement can be used in this case

24 Alice in Action with Java24 Java’s do Statement Pattern for Java’s do statement: do Statement while(Condition); –Loop provides one-trip behavior with posttest condition –You must type a semicolon after the condition How it works –Statement is executed –Condition is evaluated If true, go back and execute statement If false –Go to next statement in program

25 Alice in Action with Java25 do Statement Flow

26 Alice in Action with Java26 Choosing the Right Loop Solving a problem with fixed counting –Recommendation: use the for loop Solving a problem without fixed counting –If one-trip behavior is needed, use a do loop –If zero-trip behavior is needed, use a while loop

27 Alice in Action with Java27

28 Alice in Action with Java28 Java’s switch Statement Objective: create a PetLicense class Instance variables used in PetLicense class –char type named myCode stores license code –double type named myFee stores license fee Constructor for a PetLicense object –Takes a single char type argument –Uses multi-branch if to select appropriate fee –If data is valid, instance variables are initialized –If data is invalid, an error message is displayed switch : concise alternative to the multi-branch if

29 Alice in Action with Java29 Java’s switch Statement Pattern for Java’s switch statement switch(IntegerCompatibleExpression){ CaseList 1 StatementList 1... CaseList N StatementList N default: StatementList N+1 } –The condition is an integer-compatible expression –Each case corresponds to a literal value –The use of default statement optional –Use of break statement below a case is recommended PetLicense class meets criteria for use of switch

30 Alice in Action with Java30 Nested switch Statements One switch statement can be nested within another A nested switch statement is used in TShirt class

31 Alice in Action with Java31 Summary By default, program execution flows sequentially Selective execution: directing flow based on the value of a boolean condition if statement: general-purpose selection structure switch statement: structure designed for certain types of multi-branch selection Repetitive execution: performing the same group of statements while a condition is true

32 Alice in Action with Java32 Summary (continued) while loop: provides fixed and indefinite looping Infinite loop: fails to terminate due to a faulty or missing condition for loop: counter-controlled loop that exhibits zero- trip behavior do loop: repetition structure that provides one-trip behavior through the use of a posttest condition java.util.Random : class used to generate pseudo-random numbers


Download ppt "Alice in Action with Java Chapter 10 Flow Control in Java."

Similar presentations


Ads by Google