webpage icon indicating copy to clipboard operation
webpage copied to clipboard

Use one-line-if when possible in "Operators and Control Flow"

Open Beliavsky opened this issue 4 years ago • 0 comments

Fortran's one-line-if is convenient and concise, and I think it should be used when possible in the tutorial.

At https://fortran-lang.org/learn/quickstart/operators_control_flow , after showing

  if (angle < 90.0) then
    print *, 'Angle is acute'
  end if

I think it should be added that the following is equivalent:

if (angle < 90.0) print *, 'Angle is acute'

Then the one-line-if should be used in later examples:

  integer :: i
  do i=1, 100
    if (i > 10) then
      exit ! Stop printing numbers
    end if
    print *, i
  end do
  integer :: i
  do i=1,10
    if (mod(i,2) == 0) then
       cycle  ! Don't print even numbers
    end if
    print *, i
  end do
  integer :: i,j
  outer_loop: do i=1,10
    inner_loop: do j=1,10
      if ((j+i) > 10) then  ! Print only pairs of i and j that add up to 10
         cycle outer_loop  ! Go to the next iteration of the outer loop
      end if
      print *, 'I=', i, ' J=', j, ' Sum=', j+i
    end do inner_loop
  end do outer_loop

Beliavsky avatar May 24 '21 10:05 Beliavsky