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.

sweep_score_table.f90
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 = 
    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 = 
    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 = 
    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
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.