Nested loops evaluate every pair from two parameter lists and keep the best combination.

Program

Play the program to include more width choices in the sweep.

nested_sweep_area.f90
program nested_sweep_area_demo
    implicit none
    integer :: widths(3)
    integer :: heights(2)
    integer :: width_count
    integer :: height_count
    integer :: wi, hi
    integer :: width, height, area
    integer :: best_width, best_height, best_area

    widths = [2, 4, 6]
    heights = [3, 5]
    width_count = 
    height_count = 2
    best_area = -1
    best_width = 0
    best_height = 0
    do wi = 1, width_count
        width = widths(wi)
        do hi = 1, height_count
            height = heights(hi)
            area = width * height
            if (area > best_area) then
                best_area = area
                best_width = width
                best_height = height
            end if
        end do
    end do
    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
program nested_sweep_area_demo
    implicit none
    integer :: widths(3)
    integer :: heights(2)
    integer :: width_count
    integer :: height_count
    integer :: wi, hi
    integer :: width, height, area
    integer :: best_width, best_height, best_area

    widths = [2, 4, 6]
    heights = [3, 5]
    width_count = 
    height_count = 2
    best_area = -1
    best_width = 0
    best_height = 0
    do wi = 1, width_count
        width = widths(wi)
        do hi = 1, height_count
            height = heights(hi)
            area = width * height
            if (area > best_area) then
                best_area = area
                best_width = width
                best_height = height
            end if
        end do
    end do
    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
program nested_sweep_area_demo
    implicit none
    integer :: widths(3)
    integer :: heights(2)
    integer :: width_count
    integer :: height_count
    integer :: wi, hi
    integer :: width, height, area
    integer :: best_width, best_height, best_area

    widths = [2, 4, 6]
    heights = [3, 5]
    width_count = 
    height_count = 2
    best_area = -1
    best_width = 0
    best_height = 0
    do wi = 1, width_count
        width = widths(wi)
        do hi = 1, height_count
            height = heights(hi)
            area = width * height
            if (area > best_area) then
                best_area = area
                best_width = width
                best_height = height
            end if
        end do
    end do
    print '(I0, 1X, I0, 1X, I0)', best_width, best_height, best_area
end program nested_sweep_area_demo
nested loop The outer loop picks one width and the inner loop tries every height.
combination Each `(width, height)` pair is one sweep case.
best combination The current best parameter pair is updated when its area is larger.