A guard checks the risky condition before doing the work. The caller can inspect a status value and keep control flow explicit.

Program

Play the program to choose a divisor and watch the guard choose a result and status.

divisor
guarded_divide.f90
Replay: real traced execution (multi-file project)
program guarded_divide_demo
    implicit none
    integer :: numerator, divisor
    integer :: quotient, status

    numerator = 20
    divisor = 2
    if (divisor == 0) then
        quotient = 0
        status = 1
    else
        quotient = numerator / divisor
        status = 0
    end if
    print '(I0, 1X, I0)', quotient, status
end program guarded_divide_demo
program guarded_divide_demo
    implicit none
    integer :: numerator, divisor
    integer :: quotient, status

    numerator = 20
    divisor = 0
    if (divisor == 0) then
        quotient = 0
        status = 1
    else
        quotient = numerator / divisor
        status = 0
    end if
    print '(I0, 1X, I0)', quotient, status
end program guarded_divide_demo
program guarded_divide_demo
    implicit none
    integer :: numerator, divisor
    integer :: quotient, status

    numerator = 20
    divisor = 5
    if (divisor == 0) then
        quotient = 0
        status = 1
    else
        quotient = numerator / divisor
        status = 0
    end if
    print '(I0, 1X, I0)', quotient, status
end program guarded_divide_demo
  1. numerator ← 20

    6numerator = 207divisor = 2
    values this step20numerator
  2. divisor ← 2

    6numerator = 207divisor = 28if (divisor == 0) then
    values this step2divisor
  3. divisor == 0 ← .false.

    7divisor = 28if (divisor == 0) then9    quotient = 0
    values this step.false.divisor == 02divisor
  4. quotient ← 10

    11else12    quotient = numerator / divisor13    status = 0
    values this step10quotient20numerator2divisor
  5. status ← 0

    12    quotient = numerator / divisor13    status = 014end if
    values this step0status
  6. print '(I0, 1X, I0)', quotient, status

    14    end if15    print '(I0, 1X, I0)', quotient, status16end program guarded_divide_demo
    output10 0
    values this step10quotient0status
  1. numerator ← 20

    6numerator = 207divisor = 0
    values this step20numerator
  2. divisor ← 0

    6numerator = 207divisor = 08if (divisor == 0) then
    values this step0divisor
  3. divisor == 0 ← .true.

    7divisor = 08if (divisor == 0) then9    quotient = 0
    values this step.true.divisor == 00divisor
  4. quotient ← 0

    8if (divisor == 0) then9    quotient = 010    status = 1
    values this step0quotient
  5. status ← 1

    9    quotient = 010    status = 111else
    values this step1status
  6. print '(I0, 1X, I0)', quotient, status

    14    end if15    print '(I0, 1X, I0)', quotient, status16end program guarded_divide_demo
    output0 1
    values this step0quotient1status
  1. numerator ← 20

    6numerator = 207divisor = 5
    values this step20numerator
  2. divisor ← 5

    6numerator = 207divisor = 58if (divisor == 0) then
    values this step5divisor
  3. divisor == 0 ← .false.

    7divisor = 58if (divisor == 0) then9    quotient = 0
    values this step.false.divisor == 05divisor
  4. quotient ← 4

    11else12    quotient = numerator / divisor13    status = 0
    values this step4quotient20numerator5divisor
  5. status ← 0

    12    quotient = numerator / divisor13    status = 014end if
    values this step0status
  6. print '(I0, 1X, I0)', quotient, status

    14    end if15    print '(I0, 1X, I0)', quotient, status16end program guarded_divide_demo
    output4 0
    values this step4quotient0status
guard The `if` condition checks for the unsafe divisor before division.
status `status = 1` records the failure path without stopping the program.
normal path When the guard is false, the program computes the quotient normally.