Table-Driven Calculations
Threshold Bracket Table
Last Matching Row
A bracket table can scan thresholds and keep the last matching row.
Program
Play the program to change an amount and see which bracket is selected.
threshold_bracket_table.f90
program threshold_bracket_table_demo
implicit none
integer :: minimums(3)
integer :: factors(3)
integer :: amount
integer :: i
integer :: selected_factor
integer :: adjusted
minimums = [0, 10, 20]
factors = [1, 2, 3]
amount =
selected_factor = factors(1)
do i = 1, 3
if (amount >= minimums(i)) selected_factor = factors(i)
end do
adjusted = amount * selected_factor
print '(I0, 1X, I0)', selected_factor, adjusted
end program threshold_bracket_table_demo
program threshold_bracket_table_demo
implicit none
integer :: minimums(3)
integer :: factors(3)
integer :: amount
integer :: i
integer :: selected_factor
integer :: adjusted
minimums = [0, 10, 20]
factors = [1, 2, 3]
amount =
selected_factor = factors(1)
do i = 1, 3
if (amount >= minimums(i)) selected_factor = factors(i)
end do
adjusted = amount * selected_factor
print '(I0, 1X, I0)', selected_factor, adjusted
end program threshold_bracket_table_demo
program threshold_bracket_table_demo
implicit none
integer :: minimums(3)
integer :: factors(3)
integer :: amount
integer :: i
integer :: selected_factor
integer :: adjusted
minimums = [0, 10, 20]
factors = [1, 2, 3]
amount =
selected_factor = factors(1)
do i = 1, 3
if (amount >= minimums(i)) selected_factor = factors(i)
end do
adjusted = amount * selected_factor
print '(I0, 1X, I0)', selected_factor, adjusted
end program threshold_bracket_table_demo
threshold table
Threshold rows describe which factor applies for each amount range.
last match
Scanning in order keeps the factor from the last satisfied threshold.
table-driven branch
Changing the table data can change the decision without changing the loop shape.