Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.

Slides:



Advertisements
Similar presentations
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
©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.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Chapter 4: Control Structures II
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
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.
Chapter 5: Control Structures II
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
JavaScript, Fourth Edition
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
A First Book of ANSI C Fourth Edition Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
A First Book of ANSI C Fourth Edition
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 5: Control Structures II
Java Programming: Guided Learning with Early Objects
JavaScript: Control Statements.
JavaScript: Control Statements I
C++ for Engineers and Scientists Second Edition
MSIS 655 Advanced Business Applications Programming
A First Book of ANSI C Fourth Edition
Chapter 2.1 Repetition.
Chapter 6 Control Statements: Part 2
Objectives You should be able to describe: The while Statement
Presentation transcript:

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

2 Objectives You should be able to describe: Basic Loop Structures The while Statement Interactive while Loops The for Statement The do-while Statement

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition3 Objectives (continued) Recursion Program Design and Development: UML State Diagrams Applications: Random Numbers and Simulations Common Programming Errors

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition4 Basic Loop Structures Repetition statements –Provide capability to type set of instructions once –Have instructions repeated continuously –Until some preset condition is met

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition5 Basic Loop Structures (continued) Elements required for repetition: –Repetition statement –Condition that must be evaluated –Statement that initially sets condition –Statement within repeating section of code that allows condition to become false

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition6 Basic Loop Structures (continued) Java repetition structures: –while structure –for structure –do-while structure

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition7 Pretest and Posttest Loops Pretest loop –Condition being tested evaluated at beginning of repeating section of code –If condition is true executable statements within loop are executed –If initial value of condition is false executable statements within loop are never executed at all –Also called entrance controlled loops

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition8 Pretest and Posttest Loops (continued) Posttest loop –Loop that evaluates condition at end of repeating section of code –Always execute loop statements at least once before condition is tested –do-while construct is example of posttest loop

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition9 Pretest and Posttest Loops (continued) Figure 6.1: A pretest loop

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition10 Pretest and Posttest Loops (continued) Figure 6.2: A posttest loop

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition11 Fixed-Count Versus Variable- Condition Loops Fixed-count loop –Condition is used to keep track of how many repetitions have occurred –All of Java’s repetition statements can be used Variable-condition loop –Tested condition depends on variable that can change interactively with each pass through loop –All of Java’s repetition statements can be used

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition12 The while Statement General repetition statement –Can be used in variety of programming situations Syntax: while (condition) statement; Statement following condition is executed repeatedly as long as condition remains true

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition13 The while Statement (continued) 1.Test condition 2.If condition has nonzero (true) value a.Execute statement following parentheses b.Go back to step 1 Else Exit the while statement

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition14 The while Statement (continued) Figure 6.3: The anatomy of a while loop

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition15 The while Statement (continued) Must assign values to each variable in tested condition –Before while statement is encountered Infinite loop –Loop that never ends –To exit infinite loop: Pressing Ctrl and C keys at same time will break program execution

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition16 Interactive while Loops Combining interactive data entry with repetition capabilities of while statement –Produces adaptable and powerful programs User entered value used in conditional statement

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition17 Sentinels Sentinel –Data values that signal either start or end of data series –Must be selected so as not to conflict with legitimate data values

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition18 The break and continue Statements break statement –Forces immediate break, or exit, from: switch while for do-while

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition19 The break and continue Statements (continued) break statement (continued) –Violates pure structured programming principles: Provides second nonstandard exit from loop Extremely useful and valuable for breaking out of loops when unusual condition is detected

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition20 The break and continue Statements (continued) continue statement –Applies only to loops created with: while do-while for –When encountered in loop next iteration of loop begins immediately –Not often used in practice

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition21 The null Statement Semicolon with nothing preceding it –; Do-nothing statement –Used where statement is syntactically required but no action is called for

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition22 The for Statement Performs same functions as while statement –Uses different form Syntax: for (initializer; condition; increment) statement; Condition –Middle item in parentheses –Any Java expression that yields Boolean value

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition23 The for Statement (continued) Allows one or more initializing expressions to be grouped together –First set of items within for’s parentheses –Commas used to separate multiple initializations Last item within for’s parentheses –Condition-altering expressions –Typically increment or decrement Initializer, condition, increment and statement can be replaced with null statement

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition24 The for Statement (continued) Figure 6.11: The for statement’s flow of control

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition25 Interactive for Loops Using showInputDialog() method inside for loop –Produces same effect as when method is used within while loop

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition26 Do You Use a for or a while Loop? Each can construct both fixed-count and variable- condition loops Matter of style

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition27 Nested Loops Have loop within another loop Entire inner loop executed for each outer loop iteration

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition28 Nested Loops (continued) Figure 6.12: For each i, j loop

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition29 The do-while Statement Execute statements before expression is evaluated –Posttest Syntax: do statement; while(condition);

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition30 The do-while Statement (continued) Figure 6.13: The do statement’s flow of control

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition31 Validity Checks Data validity checks –Validate user input –Use do-while statement Read value inside loop Check entered value Exit loop when value correct

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition32 Recursion Method calls itself –Receives new copies of arguments and local variables Direct recursion –Method invokes itself Indirect or mutual recursion –Method can invoke second method, which in turn invokes first method

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition33 Mathematical Recursion Recursive concept –Solution to problem can be stated in terms of “simple” versions of itself Factorial of number n: –Denoted as n!, where n is a positive integer 1! = 1 n! = n * (n * 1)! for n > 1

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition34 Mathematical Recursion (continued) General considerations that must be specified: –What is first case? –How is nth case related to (n - 1) case?

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition35 Mathematical Recursion (continued) Pseudocode for factorial method: If n = 1 factorial = n Else factorial = n * factorial(n - 1)

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition36 How the Computation Is Performed Java method can call itself due to: –Java’s allocation of new memory locations for all method arguments and local variables As each method is called Made dynamically in memory area called stack Stack –Memory for rapidly storing and retrieving data

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition37 Recursion Versus Iteration Recursive method can be applied to any problem where solution represented in terms of solutions to simpler versions of same problem Recursive methods –Can always be written in non-recursive manner using iterative solution

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition38 Recursion Versus Iteration (continued) If problem solution can be expressed iteratively or recursively with equal ease iterative solution is preferable –Executes faster –Uses less memory Times when recursive solutions preferable: –Some problems are easier to visualize using recursive algorithm –Sometimes provides much simpler solution

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition39 Program Design and Development: UML State Diagrams Present transition of object’s state over time Considered dynamic models Show: –Different states that object can have –Events that cause states to appear

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition40 Program Design and Development: UML State Diagrams (continued) Event –Defined as individual signal (stimulus) from one object to another –Always one-way signals State –Defined by values of attributes –Activities are associated with states

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition41 Program Design and Development: UML State Diagrams (continued) Can represent either: –Continuously operating system –Or finite, one-time life cycle Almost always have initial state May have one or more final states or no final state

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition42 Program Design and Development: UML State Diagrams (continued) Figure 6.17: The state model identifies operations to be included in the class diagram

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition43 Program Design and Development: UML State Diagrams (continued) Figure 6.18: State diagram notation

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition44 Applications: Random Numbers and Simulations Random numbers –Series of numbers whose order cannot be predicted –Hard to find in practice Pseudorandom numbers –Sufficiently random for task at hand Java compilers provide general-purpose method for creating random numbers –Math.random()

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition45 Scaling Method for adjusting random numbers produced by random number generator to reside within ranges, such as: –1 to 100 –Accomplished using expression: (int) (Math.random() * N) Where number falls between 0 and N-1

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition46 Simulations Common use of random numbers: –Simulate events rather than going through time and expense of constructing real-life experiment Coin Toss Simulation –Use random number generator to simulate coin tosses Elevator Simulation –Simulate the operation of an elevator

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition47 Simulations (continued) Figure 6.27: Class diagram for a CoinToss class

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition48 Simulations (continued) Figure 6.28: A state diagram for an elevator

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition49 Common Programming Errors Creating loop that is “off by one” Repetition statements should not test for equality when testing floating-point (real-values) operands Placing semicolon at end of either while or for’s parentheses Using commas to separate items in for statement instead of required semicolons Omitting final semicolon from do-while statement

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition50 Summary Section of repeating code is referred to as loop –Types of loops: while for do-while while statement –Checks condition before any other statement in loop is executed

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition51 Summary (continued) for statement –Extremely useful in creating loops that must be executed fixed number of times do-while statement –Checks condition at end of loop Recursion –Method calls itself with new copy of arguments and local variables