Self-Assessment Quiz: Control Flow

Twenty questions to confirm you can read and write Fortran control flow before moving on to arrays. Several are "what does this print?" — trace them by hand; that skill matters more than any single answer. Aim for 16 or more. Answers and a topic map are at the end.


Question 1

Which keywords close an if construct and a do loop, respectively? - A. endif and enddo only - B. end if and end do - C. fi and done - D. } and }

Question 2

Fortran spells logical AND, OR, and NOT as: - A. &&, ||, ! - B. and, or, not - C. .and., .or., .not. - D. AND, OR, NOT (uppercase required)

Question 3

The loop do i = 1, 6, 2 gives i the values: - A. 1, 2, 3, 4, 5, 6 - B. 1, 3, 5 - C. 2, 4, 6 - D. 1, 3, 5, 7

Question 4

The expression controlling a select case may be any of these types except: - A. integer - B. character - C. logical - D. real

Question 5

True or false (justify): the bounds of a counted do i = 1, n loop are inclusive on both ends, so the body runs n times.

Question 6

To break out of both loops in a nest at once, you should: - A. Use two exit statements in a row - B. Name the outer loop and write exit <name> from inside the inner loop - C. Use cycle - D. Use a goto

Question 7

What does this print?

integer :: k = 0
if (k > 0) then
  print '(a)', 'pos'
else if (k < 0) then
  print '(a)', 'neg'
else
  print '(a)', 'zero'
end if
  • A. pos
  • B. neg
  • C. zero
  • D. nothing

Question 8

Fortran's cycle corresponds to which Python keyword? - A. break - B. continue - C. pass - D. return

Question 9

The key safety difference between Fortran's select case and C's switch is that select case: - A. Is faster in every case - B. Allows real labels - C. Does not fall through, so there is no missing-break bug - D. Can only handle two cases

Question 10

True or false (justify): writing do concurrent instead of do guarantees the loop runs on multiple CPU cores.

Question 11

What does this print?

integer :: c = 0, m = 20
do while (m > 1)
  m = m / 2
  c = c + 1
end do
print '(i0)', c
  • A. 3
  • B. 4
  • C. 5
  • D. it never terminates

Question 12

Which array-assignment construct did Fortran 2018 declare obsolescent, so that new code should avoid it? - A. where - B. do concurrent - C. forall - D. select case

Question 13

Why is if (x == 0.1_dp) a poor test for a real(dp) variable x? - A. == is not a legal operator in Fortran - B. 0.1 cannot be represented exactly in binary, so the comparison may fail unexpectedly - C. Reals cannot be compared at all - D. It is legal but only inside a do loop

Question 14

In one sentence: what does where (a < 0.0_dp) a = 0.0_dp do to an array a?

Question 15

A real-valued do-loop counter (e.g. do x = 0.0, 1.0, 0.1) in modern standard Fortran is: - A. The recommended way to loop over time - B. Deleted from the standard; gfortran -std=f2018 rejects it - C. Faster than an integer counter - D. Required when the step is not 1

Question 16

What does this print?

integer :: v = 5
select case (v)
case (1:3)
  print '(a)', 'low'
case (4:6)
  print '(a)', 'mid'
case default
  print '(a)', 'high'
end select
  • A. low
  • B. mid
  • C. high
  • D. compile error

Question 17

True or false (justify): a select case whose labels are case (0:60) and case (60:100) is a compile-time error.

Question 18

The intrinsic mod(7, 2) evaluates to: - A. 0 - B. 1 - C. 3 - D. 3.5

Question 19

What does this print?

integer :: i, j, hits = 0
outer: do i = 1, 3
  do j = 1, 3
    if (i + j == 4) then
      hits = hits + 1
      cycle outer
    end if
  end do
end do outer
print '(i0)', hits
  • A. 1
  • B. 2
  • C. 3
  • D. 9

Question 20

You need to iterate "until the solution converges," and you cannot know the number of steps in advance. The most natural construct is: - A. A counted do i = 1, n - B. A do while (not_converged) (or an infinite do with exit) - C. A select case - D. A where


Answer Key

Q Ans Why
1 B Modern free-form spells them end if and end do (two words).
2 C The dots are part of the spelling: .and., .or., .not..
3 B Start 1, step 2, up to 6 inclusive: 1, 3, 5.
4 D Case labels must be discrete and exact; real is neither.
5 True do i = 1, n includes both 1 and n, so it runs exactly n times.
6 B Naming the outer loop and exit <name> is the only clean multi-level break.
7 C k is 0, so both >0 and <0 fail and the else runs: zero.
8 B cycle = continue (skip to next iteration); exit = break.
9 C No fall-through means exactly one block runs — no forgotten break.
10 False It permits reordering/parallelism; plain gfortran runs it serially.
11 B 20→10→5→2→1 by integer halving is 4 steps; then 1 > 1 is false.
12 C forall is obsolescent as of Fortran 2018.
13 B 0.1 is inexact in binary, so exact == is unreliable — use a tolerance.
14 It sets every negative element of a to 0 and leaves the rest unchanged (masked whole-array assignment).
15 B Real do counters were deleted in Fortran 95; -std=f2018 rejects them.
16 B 5 falls in case (4:6)mid.
17 True The ranges overlap at 60, so a value could match two cases — the compiler rejects it.
18 B 7 = 3·2 + 1, so the remainder mod(7,2) is 1.
19 C Each i finds one pair summing to 4 (i+j=4) then cycle outer: 3 hits.
20 B Unknown iteration count → do while or infinite do + exit.

Topics to review by question

  • Q1–2, 7, 13 → §4.1 (the if construct; relational/logical operators; the real-equality trap).
  • Q4, 9, 16, 17 → §4.2 (select case: types, ranges, no fall-through, disjoint labels).
  • Q3, 5, 11, 15, 18, 20 → §4.3 (the three do loops; integer counters; mod).
  • Q6, 8, 19 → §4.4 (cycle, exit, named nested loops).
  • Q14 → §4.5 (where).
  • Q10, 12 → §4.6 (do concurrent permits, not guarantees; forall is obsolescent).

Scored below 16? Re-read the flagged sections and re-trace the "what does this print?" questions by hand before starting Chapter 5 — arrays lean hard on the loop skills here.