Chapter 4 — Teaching Notes

One-line purpose. Give students the verbs of computation — decide, dispatch, repeat, skip, break — in modern block-structured Fortran, and plant the two array/parallel constructs (where, do concurrent) that the rest of the book grows.

Key ideas to emphasize

  • The loop IS the computation. Students from web/scripting backgrounds treat loops as plumbing. In numerical code the loop is the product — a weather model is a loop. Say this out loud; it reframes the whole course.
  • Choosing the right loop. Counted (do i=1,n) when the count is known; do while when it isn't; infinite do+exit when the test can only run mid-body (the convergence pattern). Make students justify their choice, not just get output.
  • select case beats an if-ladder for single-value dispatch — disjoint labels checked by the compiler, ranges, and no fall-through (contrast C's missing-break bug). This is the chapter's cleanest "the-language-removes-a-footgun" story.
  • Two floating-point traps, one root cause. Never if (x == 0.1_dp); never a real do counter. Both are the same inexactness (Ch. 3 → Ch. 20). Tie them together explicitly.
  • where and do concurrent are a taste, not the meal. Introduce the idea (whole-array masking; asserting independence) and immediately point forward: arrays proper → Ch. 5, do concurrent performance → Ch. 29. Do NOT let students think do concurrent auto-parallelizes.

Misconceptions to preempt

  • "do concurrent runs in parallel." (No — it permits it; plain gfortran runs it serially. It is a promise, not a switch.)
  • "select case falls through like C." (No — exactly one block runs; no break.)
  • "do i = 1, n runs n-1 times." (No — inclusive both ends, n times. The Python-range off-by-one.)
  • "You can loop do t = 0.0, 1.0, 0.1." (Deleted from the standard; and inexact anyway. Count with an integer.)
  • "A bare exit leaves all nested loops." (No — only the innermost; name the outer loop.)
  • "Comparing reals with == is fine if the math is exact." (Only powers-of-two-style dyadic values are exact; default to a tolerance.)

A live demonstration (5–8 minutes)

Type the loop_tour program (code/example-03-do-loops.f90) live, but pause before running each print and have the class predict the number. Reveal by compiling. The halving-count (7) and the n^2 > 50 case (8) are the ones they will most often get wrong — great teachable misses. Then change the search block's bare exit to exit search vs plain exit and show how the answer changes: the difference between "leave the nest" and "leave one loop."

Class-time budget (~50 min)

  • 8 min: if construct + relational/logical operators; the real-equality pitfall (§4.1).
  • 8 min: select case, ranges, no fall-through (§4.2).
  • 14 min: the three do loops, with the live loop_tour prediction demo (§4.3).
  • 8 min: cycle, exit, named nested loops; loop-order habit (§4.4).
  • 7 min: where and do concurrent as a taste; forall is obsolescent (§4.5–4.6).
  • 5 min: the Python/C side-by-side table + launch the Project Checkpoint skeleton (§4.7).

Prerequisites to review

Chapter 3: the logical type, integer division (7/2 == 3), mod, and kind-suffixed literals (1.0_dp). Chapter 2: implicit none, the compile command, and -fcheck=all. A 3-minute warm-up recomputing 7/2, mod(7,2), and 7.0_dp/2.0_dp pays for itself the moment loop counters and conditions appear.

Connections

Back: Ch. 3 (types/division/mod), Ch. 2 (flags). Forward: Ch. 5 (arrays, where on real data, column-major), Ch. 20 (why real equality fails), Ch. 24 (the boundary if becomes the real solver), Ch. 29 (do concurrent for speed). The Project Checkpoint's boundary-vs-interior if is literally the Ch. 24 stencil gate — flag that so students see the payoff coming.