ISM 3232 - Intermediate Programming. 

Chapter 6  - Loops (Repetition)

Student Learning Objectives. You should be able to:

  • Design and implement loop structures to solve programming problems with repetition in Java.
  • Specify the differences between loop structures.
  • Prevent infinite loops.
  • Distinguish between counting and conditional loops.
  • List and demonstrate principles of loop design.
  • Describe and demonstrate design principles of modularity and localization.
  • Explain the role of structured programming in OO program design.
  • Use TextArea

 

Index:

 

Review: Flow-of-Control - Repetition Structures

You are already familiar with the purpose and varieties of loops from your study of other programming languages such as Visual BASIC or C. This chapter will be largely review of numerous looping concepts. What will be new is how loops are implemented in Java.

For a simple example, what if we wanted to print "Hello" 100 times?

Solution: Write a method containing 100 println() statements,

or (much better)

write a method that contains 1 println(), then place it inside a loop structure:

What defines the loop structure?

Counting Loop

For example, here is the pseudocode for:

 

Loop Bounds

Not only must an updater exist within the body of the loop, but it must be updated such that eventually it will end the loop.

Infinite Loop Examples (what needs to be changed in each?)

In each case the updater fails to make progress toward the bound and the loop entry condition never becomes false.

Loop Indentation. Indenting properly is extremely important for readability and debugging purposes, even though the compiler doesn't recognize it. These examples all appear the same to the compiler, but the first most clearly illustrates to the reader what is intended.

 

Loop Variable Scope

If k is declared within the for statement, it cannot be used after the for statement:

If k is declared before the for statement, it can be used after the for statement:

 

Compound Loop Body

 

Nested Loops

 

Another Example: Printing a pattern

 

Another Example: Car Loan Table

Car Loan Interest Rate

Implementation: CarLoan Class

 

Conditional Loops: While .. , Do .. While

 

The While Structure

Effective Design: Loop structure.

 

 

 

Example while : Computing Average

 

Design Principles: Modularity and Localization

The Computing Average example above demonstrates design principles of Modularity and Localization, by using separate methods for the input and averaging tasks. We can then call getInput() when needed, rather than repeat the code in multiple places.

Computing Average: Main Program

 

The Do-While Structure

 

Example:

 

Data Validation: Example

 

Example: Modifications to CyberPet

 

Modified CyberPet Applet


Principles of Loop Design


Structured Programming

Structured Programming Constructs

 

Debugging Problem: Structured vs. Unstructured Code

Preconditions and Postconditions

 

Using Preconditions and Postconditions


Design/Programming Tips


From the Java Library: TextArea

 

Key terms : 

Suggested Exercises :  2, 3, 4, 7, 8, 9