Chapter 20. Loops (Iterative Execution)

Table of Contents

20.. Motivation
20.. Design vs. Implementation
20.. Anatomy of a Loop
Practice
20.. While: The Universal Loop
Practice
20.. Fortran Fixed Do Loops
Practice
20.. The C for Loop
Practice
20.. Unstructured Loops
Fortran Unstructured Do Loops
The C break Statement
Practice
20.. The C do-while Loop
Practice
20.. Fortran cycle and C continue
20.. Infinite Loops
Practice
20.. Loops and Round-off Error
Practice
20.. Nested Loops
20.. Real Examples
Integer Powers
Newton's Method
Practice
20.. Code Quality
20.. Performance
20.. 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.