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

Slides:



Advertisements
Similar presentations
Control Structures Corresponds with Chapters 3 and 4.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Alice in Action with Java Chapter 10 Flow Control in Java.
Chapter 5: Control Structures II (Repetition)
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
C++ for Engineers and Scientists Third Edition
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
2Object-Oriented Program Development Using C++ 3 Basic Loop Structures Loops repeat execution of an instruction set Three repetition structures: while,
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Calvin College Controlling Behavior The if, switch and for Statements.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Controlling Behavior The if and for Statements.
Chapter 3: Decisions and Loops
The switch Statement, and Introduction to Looping
Control Structures II (Repetition)
Java Programming: Guided Learning with Early Objects
Outline Altering flow of control Boolean expressions
Chapter 5: Control Structures II (Repetition)
Controlling Behavior The if and for Statements.
Presentation transcript:

Alice in Action with Java Chapter 10 Flow Control in Java

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

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

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

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

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

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

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

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

Alice in Action with Java10

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

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

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

Alice in Action with Java14 while Statement Flow

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

Alice in Action with Java16 Initials class

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

Alice in Action with Java18 Counting Up

Alice in Action with Java19 NinetyNineBottles

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

Alice in Action with Java21 TextGraphics

Alice in Action with Java22 for Statement Flow

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

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

Alice in Action with Java25 do Statement Flow

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

Alice in Action with Java27

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

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

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

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

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