1 / 32

Alice in Action with Java

Alice in Action with Java. Chapter 10 Flow Control in Java. 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. Flow Control In Java.

akiko
Download Presentation

Alice in Action with Java

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Objectives • Learn how to use the ifstatement • Learn how to use the switchstatement • Learn how to use the whileloop • Learn how to use the forloop • Learn how to use the doloop Alice in Action with Java

  3. 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 Alice in Action with Java

  4. 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 Alice in Action with Java

  5. 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 Alice in Action with Java

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

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

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

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

  10. Alice in Action with Java

  11. 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 Alice in Action with Java

  12. 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 Initialsclass • Instance variables called myName and myInitials • Constructor to initialize the instance variables • Methods to return myName and myInitials Alice in Action with Java

  13. Java’s while Statement (continued) • General pattern for Java’s while statement while(Condition)Statement • Statementcomprises 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 Alice in Action with Java

  14. while Statement Flow Alice in Action with Java

  15. String Tokenizer • StringTokenizerclass:used to splitStrings • 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 Alice in Action with Java

  16. Initials class Alice in Action with Java

  17. 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 Alice in Action with Java

  18. Counting Up Alice in Action with Java

  19. NinetyNineBottles Alice in Action with Java

  20. Nested Loops • TextGraphics class illustrates nested for loops • Understanding drawBox()in TextGraphicsclass • Method takes two arguments for height and width • Outer forloop counts the rows (builds the height) • Inner forloop 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 Alice in Action with Java

  21. TextGraphics Alice in Action with Java

  22. for Statement Flow Alice in Action with Java

  23. 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 Alice in Action with Java

  24. 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 Alice in Action with Java

  25. do Statement Flow Alice in Action with Java

  26. 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 Alice in Action with Java

  27. Alice in Action with Java

  28. Java’s switch Statement • Objective: create a PetLicense class • Instance variables used in PetLicense class • chartype namedmyCode stores license code • doubletype namedmyFee stores license fee • Constructor for a PetLicenseobject • Takes a single chartype 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 Alice in Action with Java

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

  30. Nested switch Statements • Oneswitch statement can be nested within another • A nested switch statement is used in TShirt class Alice in Action with Java

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

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

More Related