Skip to main content

Introduction

Iteration is the process of repeating the instructions of our program. Together with selection, iteration forms the core methods to control the logical flow of our programs. We often refer to iteration statements as loops. This section of the course notes will look at constructs that will do iteration

Learning Outcomes

After completing this chapter, students will be able to:

  1. Understand how iteration statements allow programs to repeat code blocks based on conditions or counts
  2. Distinguish between the three types of loops (while, for, and do-while) and select the appropriate loop type for different programming scenarios
  3. Construct and implement while loops for situations where the number of iterations is unknown, such as input validation and sentinel-controlled input
  4. Construct and implement for loops for counting operations and situations where the number of iterations is known in advance
  5. Apply the standard counting loop recipe starting from zero and incrementing by one to process a known number of items
  6. Convert between for and while loops by separating the initialization, condition, and update components of a for loop into a while loop structure
  7. Implement input validation loops that repeatedly prompt users until valid data is entered, with appropriate error messages
  8. Avoid common loop pitfalls such as infinite loops, modifying loop counters within the loop body, and using do-while loops inappropriately
  9. Design programs with iteration that solve real-world problems involving repeated operations, such as summing values, drawing patterns, and processing multiple data items