A sweep can score each candidate and keep the best result seen so far.

Program

Play the program to change the target and watch the best candidate shift.

target
sweep_score_table.f90
Replay: real traced execution (multi-file project)
program sweep_score_table_demo
    implicit none
    integer :: candidates(3)
    integer :: target
    integer :: i
    integer :: score
    integer :: best_value
    integer :: best_score

    candidates = [1, 2, 3]
    target = 2
    best_value = candidates(1)
    best_score = -999
    do i = 1, 3
        score = 10 - abs(candidates(i) - target)
        if (score > best_score) then
            best_score = score
            best_value = candidates(i)
        end if
    end do
    print '(I0, 1X, I0)', best_value, best_score
end program sweep_score_table_demo
program sweep_score_table_demo
    implicit none
    integer :: candidates(3)
    integer :: target
    integer :: i
    integer :: score
    integer :: best_value
    integer :: best_score

    candidates = [1, 2, 3]
    target = 1
    best_value = candidates(1)
    best_score = -999
    do i = 1, 3
        score = 10 - abs(candidates(i) - target)
        if (score > best_score) then
            best_score = score
            best_value = candidates(i)
        end if
    end do
    print '(I0, 1X, I0)', best_value, best_score
end program sweep_score_table_demo
program sweep_score_table_demo
    implicit none
    integer :: candidates(3)
    integer :: target
    integer :: i
    integer :: score
    integer :: best_value
    integer :: best_score

    candidates = [1, 2, 3]
    target = 3
    best_value = candidates(1)
    best_score = -999
    do i = 1, 3
        score = 10 - abs(candidates(i) - target)
        if (score > best_score) then
            best_score = score
            best_value = candidates(i)
        end if
    end do
    print '(I0, 1X, I0)', best_value, best_score
end program sweep_score_table_demo
  1. candidates ← [1, 2, 3]

    10candidates = [1, 2, 3]11target = 2
    values this step[1, 2, 3]candidates
  2. target ← 2

    10candidates = [1, 2, 3]11target = 212best_value = candidates(1)
    values this step2target
  3. best_value ← 1

    11target = 212best_value = candidates(1)13best_score = -999
    values this step1best_value
  4. best_score ← -999

    12best_value = candidates(1)13best_score = -99914do i = 1, 3
    values this step-999best_score
  5. i ← 1

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step1i
  6. score ← 9

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step9score1candidates(1)2target
  7. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.true.score > best_score
  8. best_score ← 9

    16if (score > best_score) then17    best_score = score18    best_value = candidates(i)
    values this step9best_score
  9. best_value ← 1

    17    best_score = score18    best_value = candidates(i)19end if
    values this step1best_value
  10. i ← 2

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step2i
  11. score ← 10

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step10score2candidates(2)2target
  12. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.true.score > best_score
  13. best_score ← 10

    16if (score > best_score) then17    best_score = score18    best_value = candidates(i)
    values this step10best_score
  14. best_value ← 2

    17    best_score = score18    best_value = candidates(i)19end if
    values this step2best_value
  15. i ← 3

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step3i
  16. score ← 9

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step9score3candidates(3)2target
  17. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.false.score > best_score
  18. print '(I0, 1X, I0)', best_value, best_score

    20    end do21    print '(I0, 1X, I0)', best_value, best_score22end program sweep_score_table_demo
    output2 10
    values this step2best_value10best_score
  1. candidates ← [1, 2, 3]

    10candidates = [1, 2, 3]11target = 1
    values this step[1, 2, 3]candidates
  2. target ← 1

    10candidates = [1, 2, 3]11target = 112best_value = candidates(1)
    values this step1target
  3. best_value ← 1

    11target = 112best_value = candidates(1)13best_score = -999
    values this step1best_value
  4. best_score ← -999

    12best_value = candidates(1)13best_score = -99914do i = 1, 3
    values this step-999best_score
  5. i ← 1

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step1i
  6. score ← 10

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step10score1candidates(1)1target
  7. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.true.score > best_score
  8. best_score ← 10

    16if (score > best_score) then17    best_score = score18    best_value = candidates(i)
    values this step10best_score
  9. best_value ← 1

    17    best_score = score18    best_value = candidates(i)19end if
    values this step1best_value
  10. i ← 2

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step2i
  11. score ← 9

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step9score2candidates(2)1target
  12. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.false.score > best_score
  13. i ← 3

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step3i
  14. score ← 8

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step8score3candidates(3)1target
  15. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.false.score > best_score
  16. print '(I0, 1X, I0)', best_value, best_score

    20    end do21    print '(I0, 1X, I0)', best_value, best_score22end program sweep_score_table_demo
    output1 10
    values this step1best_value10best_score
  1. candidates ← [1, 2, 3]

    10candidates = [1, 2, 3]11target = 3
    values this step[1, 2, 3]candidates
  2. target ← 3

    10candidates = [1, 2, 3]11target = 312best_value = candidates(1)
    values this step3target
  3. best_value ← 1

    11target = 312best_value = candidates(1)13best_score = -999
    values this step1best_value
  4. best_score ← -999

    12best_value = candidates(1)13best_score = -99914do i = 1, 3
    values this step-999best_score
  5. i ← 1

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step1i
  6. score ← 8

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step8score1candidates(1)3target
  7. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.true.score > best_score
  8. best_score ← 8

    16if (score > best_score) then17    best_score = score18    best_value = candidates(i)
    values this step8best_score
  9. best_value ← 1

    17    best_score = score18    best_value = candidates(i)19end if
    values this step1best_value
  10. i ← 2

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step2i
  11. score ← 9

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step9score2candidates(2)3target
  12. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.true.score > best_score
  13. best_score ← 9

    16if (score > best_score) then17    best_score = score18    best_value = candidates(i)
    values this step9best_score
  14. best_value ← 2

    17    best_score = score18    best_value = candidates(i)19end if
    values this step2best_value
  15. i ← 3

    13best_score = -99914do i = 1, 315    score = 10 - abs(candidates(i) - target)
    values this step3i
  16. score ← 10

    14do i = 1, 315    score = 10 - abs(candidates(i) - target)16    if (score > best_score) then
    values this step10score3candidates(3)3target
  17. if (score > best_score) then

    15score = 10 - abs(candidates(i) - target)16if (score > best_score) then17    best_score = score
    values this step.true.score > best_score
  18. best_score ← 10

    16if (score > best_score) then17    best_score = score18    best_value = candidates(i)
    values this step10best_score
  19. best_value ← 3

    17    best_score = score18    best_value = candidates(i)19end if
    values this step3best_value
  20. print '(I0, 1X, I0)', best_value, best_score

    20    end do21    print '(I0, 1X, I0)', best_value, best_score22end program sweep_score_table_demo
    output3 10
    values this step3best_value10best_score
candidate score A score formula converts each candidate into a comparable value.
running best `best_score` and `best_value` carry the best result across loop iterations.
tie shape This example keeps the first candidate when a later score is not greater.