BOISE STATE CS 120 - ch10 (32 pages)

Previewing pages 1, 2, 15, 16, 31, 32 of 32 page document View the full content.
View Full Document

ch10



Previewing pages 1, 2, 15, 16, 31, 32 of actual document.

View the full content.
View Full Document
View Full Document

ch10

94 views


Pages:
32
School:
Boise State University
Course:
Cs 120 - Introduction to Programming Concepts
Introduction to Programming Concepts Documents
Unformatted text preview:

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 Alice in Action with Java 2 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 3 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 4 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 5 Java s if Statement continued Alice in Action with Java 6 Java s if Statement continued Alice in Action with Java 7 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 if statements aka if else chain Flow can move along multiple paths Nest additional if statements in the else clause Alice in Action with Java 8 Java s if Statement continued Alice in Action with Java 9 Alice in Action with Java 10 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 11 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 Alice in Action with Java 12 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 Alice in Action with Java 13 while Statement Flow Alice in Action with Java 14 String Tokenizer StringTokenizer class used to split Strings 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 15 Initials class Alice in Action with Java 16 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 17 Counting Up Alice in Action with Java 18 NinetyNineBottles Alice in Action with Java 19 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 Alice in Action with Java 20 TextGraphics Alice in Action with Java 21 for Statement Flow Alice in Action with Java 22 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 23 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 24 do Statement Flow Alice in Action with Java 25 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 26 Alice in Action with Java 27 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 Alice in Action with Java 28 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 break statement below a case is recommended PetLicense class meets criteria for use of switch Alice in Action with Java 29 Nested switch Statements One switch statement can be nested within another A nested switch statement is used in TShirt class Alice in Action with Java 30 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


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view ch10 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view ch10 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?