Docsity

Repetition - Business Application Programming - Lecture Slides, Slides of Applications of Computer Sciences

Partial preview of the text

Download Repetition - Business Application Programming - Lecture Slides and more Slides Applications of Computer Sciences in PDF only on Docsity!

Chapter 6:

Repetition

Object-Oriented Program

Development Using Java: A

Class-Centered Approach

Objectives

  • Basic Loop Structures
  • The while Statement
  • Interactive while Loops
  • The for Statement
  • The do-while Statement
  • Recursion

Basic Loop Structures

  • Repetition statements:
    • Provide the capability to type a set of instructions once
    • Have instructions repeated continuously
    • Until some preset condition is met

Basic Loop Structures

(continued)

  • Elements required for repetition:
    • Repetition statement
    • Condition that must be evaluated
    • Statement that initially sets the condition
    • Statement within repeating section of code that allows condition to become false

Pretest and Posttest Loops

  • Pretest loop
    • The condition being tested is evaluated at the beginning of the repeating section of code - If the condition is true the executable statements within the loop are executed - If the initial value of the condition is false the executable statements within the loop are never executed at all
    • Also referred to as entrance controlled loops

Pretest and Posttest Loops

(continued)

  • Posttest loop
    • A loop that evaluates a condition at the end of a repeating section of code
    • Always executes loop statements at least once before the condition is tested
    • The do-while construct is an example of a posttest loop

Fixed-Count Versus Variable-

Condition Loops

  • Fixed-count loop
    • The condition is used to keep track of how many repetitions have occurred
    • All of Java’s repetition statements can be used
  • Variable-condition loop
    • The tested condition depends on a variable that can change interactively with each pass through the loop
    • All of Java’s repetition statements can be used

Evaluating a while Statement

  • Test condition
  • If condition has nonzero (true) value
    • Execute statement following parentheses
    • Go back to step 1
  • Else
    • Exit the while statement

Interactive while Loops

  • Combining interactive data entry with the repetition capabilities of while statements produces adaptable and powerful programs
  • A user-entered value is used in a conditional statement

Sentinels

  • Sentinels:
    • Data values that signal either the start or end of data series
    • Must be selected so as not to conflict with legitimate data values

The break and continue

Statements (continued)

  • continue statement
    • Applies only to loops created with:
      • while
      • do-while
      • for
    • When encountered in a loop the next iteration of the loop begins immediately
    • Not often used in practice

The null Statement

  • Semicolon with nothing preceding it
  • Do-nothing statement
    • Used where statement is syntactically required but no action is called for