A comparison over an array produces a logical array, and count tells how many entries are true.

Program

Play the program to change the passing cutoff and count matching scores.

cutoff
count_mask.f90
Replay: real traced execution (multi-file project)
program count_mask_demo
    implicit none
    integer :: scores(5)
    integer :: cutoff
    logical :: passing(5)
    integer :: total

    scores = [70, 85, 90, 60, 75]
    cutoff = 75
    passing = scores >= cutoff
    total = count(passing)
    print '(I0)', total
end program count_mask_demo
program count_mask_demo
    implicit none
    integer :: scores(5)
    integer :: cutoff
    logical :: passing(5)
    integer :: total

    scores = [70, 85, 90, 60, 75]
    cutoff = 65
    passing = scores >= cutoff
    total = count(passing)
    print '(I0)', total
end program count_mask_demo
program count_mask_demo
    implicit none
    integer :: scores(5)
    integer :: cutoff
    logical :: passing(5)
    integer :: total

    scores = [70, 85, 90, 60, 75]
    cutoff = 80
    passing = scores >= cutoff
    total = count(passing)
    print '(I0)', total
end program count_mask_demo
  1. scores ← [70, 85, 90, 60, 75]

    8scores = [70, 85, 90, 60, 75]9cutoff = 75
    values this step[70, 85, 90, 60, 75]scores
  2. cutoff ← 75

    8scores = [70, 85, 90, 60, 75]9cutoff = 7510passing = scores >= cutoff
    values this step75cutoff
  3. passing ← [F, T, T, F, T]

    9cutoff = 7510passing = scores >= cutoff11total = count(passing)
    values this step[F, T, T, F, T]passing75cutoff
  4. total ← 3

    10passing = scores >= cutoff11total = count(passing)12print '(I0)', total
    values this step3total[F, T, T, F, T]passing
  5. print '(I0)', total

    11    total = count(passing)12    print '(I0)', total13end program count_mask_demo
    output3
    values this step3total
  1. scores ← [70, 85, 90, 60, 75]

    8scores = [70, 85, 90, 60, 75]9cutoff = 65
    values this step[70, 85, 90, 60, 75]scores
  2. cutoff ← 65

    8scores = [70, 85, 90, 60, 75]9cutoff = 6510passing = scores >= cutoff
    values this step65cutoff
  3. passing ← [T, T, T, F, T]

    9cutoff = 6510passing = scores >= cutoff11total = count(passing)
    values this step[T, T, T, F, T]passing65cutoff
  4. total ← 4

    10passing = scores >= cutoff11total = count(passing)12print '(I0)', total
    values this step4total[T, T, T, F, T]passing
  5. print '(I0)', total

    11    total = count(passing)12    print '(I0)', total13end program count_mask_demo
    output4
    values this step4total
  1. scores ← [70, 85, 90, 60, 75]

    8scores = [70, 85, 90, 60, 75]9cutoff = 80
    values this step[70, 85, 90, 60, 75]scores
  2. cutoff ← 80

    8scores = [70, 85, 90, 60, 75]9cutoff = 8010passing = scores >= cutoff
    values this step80cutoff
  3. passing ← [F, T, T, F, F]

    9cutoff = 8010passing = scores >= cutoff11total = count(passing)
    values this step[F, T, T, F, F]passing80cutoff
  4. total ← 2

    10passing = scores >= cutoff11total = count(passing)12print '(I0)', total
    values this step2total[F, T, T, F, F]passing
  5. print '(I0)', total

    11    total = count(passing)12    print '(I0)', total13end program count_mask_demo
    output2
    values this step2total
logical array `scores >= cutoff` compares each array element.
count `count(passing)` returns how many logical values are true.
threshold Changing the cutoff changes the mask before the count runs.