Chapter 19. Loops (Iterative Execution)

Table of Contents

19.. Motivation
19.. Design vs. Implementation
19.. Anatomy of a Loop
Practice
19.. While: The Universal Loop
Practice
19.. Fortran Fixed Do Loops
Practice
19.. The C for Loop
Practice
19.. Unstructured Loops
Fortran Unstructured Do Loops
The C break Statement
Practice
19.. The C do-while Loop
Practice
19.. Fortran cycle and C continue
19.. Infinite Loops
Practice
19.. Loops and Round-off Error
Practice
19.. Nested Loops
19.. Real Examples
Integer Powers
Newton's Method
Practice
19.. Code Quality
19.. Performance
19.. Solutions to Practice Breaks

Motivation

Computers are particularly good at performing large numbers of repetitive calculations quickly and accurately. Suppose we need to perform the same calculations on a billion different inputs. One way we could achieve this is by writing a billion input statements, each followed by a statement which works on the latest input. Obviously, this wouldn't be very productive.

Loops, also known as iteration, provide a way to use the same statements repeatedly for different data.