DOC PREVIEW
BOISE STATE CS 120 - ch10

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Alice in Action with JavaObjectivesFlow Control In JavaSelective ExecutionJava’s if StatementPowerPoint PresentationSlide 7Java’s if Statement (continued)Slide 9Slide 10Repetitive ExecutionJava’s while StatementJava’s while Statement (continued)Slide 14String TokenizerSlide 16Java’s for StatementSlide 18Slide 19Nested LoopsSlide 21Slide 22Post-Test LoopJava’s do StatementSlide 25Choosing the Right LoopSlide 27Java’s switch StatementSlide 29Nested switch StatementsSummarySummary (continued)Alice in Action with JavaChapter 10Flow Control in JavaAlice in Action with Java 2Objectives•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 loopAlice in Action with Java 3Flow 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, doAlice in Action with Java 4Selective 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 structureAlice in Action with Java 5Java’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 branchesAlice in Action with Java 6Java’s if Statement (continued)Alice in Action with Java 7Java’s if Statement (continued)Alice in Action with Java 8Java’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 clauseAlice in Action with Java 9Java’s if Statement (continued)Alice in Action with Java 10Alice in Action with Java 11Repetitive 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 doAlice in Action with Java 12Java’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 myInitialsAlice in Action with Java 13Java’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 programAlice in Action with Java 14while Statement FlowAlice in Action with Java 15String 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 initialsAlice in Action with Java 16Initials classAlice in Action with Java 17Java’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 downAlice in Action with Java 18Counting UpAlice in Action with Java 19NinetyNineBottlesAlice in Action with Java 20Nested 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 onlyAlice in Action with Java 21TextGraphicsAlice in Action with Java 22for Statement FlowAlice in Action with Java 23Post-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 caseAlice in Action with Java 24Java’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 programAlice in Action with Java 25do Statement FlowAlice in Action with Java 26Choosing 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 loopAlice in Action with Java 27Alice in Action with Java 28Java’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 ifAlice in Action with Java


View Full Document

BOISE STATE CS 120 - ch10

Download ch10
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?