fortran-lang / webpage

New Fortran webpage
https://fortran-lang.org/en
MIT License
46 stars 39 forks source link

Describe infinite loop in Operators and Control Flow section #83

Open Beliavsky opened 3 years ago

Beliavsky commented 3 years ago

The Operators and Control Flow section presents the do while loop with the example

  integer :: i
  i = 1
  do while (i < 11)
    print *, i
    i = i + 1
  end do
  ! Here i = 11

but the infinite do loop can emulate this behavior and is more general, since you can test the condition anywhere in the loop. In my codes I use do loops with indices and infinite do loops with exits, but not do while (although there is nothing wrong with it). So I suggest that the infinite do loop be presented in addition to or instead of do while. The equivalent to the code above would be

  integer :: i
  i = 1
  do
    if (i > 10) exit
    print *, i
    i = i + 1
  end do
  ! Here i = 11
  print*,i
  end

The code uses the one-line-if, which I suggested mentioning in another issue.

The sentence "The do loop has an integer counter variable which is used to track which iteration of the loop is currently executing." should be qualified by putting "usually" or "typically" before "has", since infinite do loops do not have integer counters.

LKedward commented 3 years ago

Yes we should present the option of an 'infinite' do loop with a conditional exit in addition to the existing loop examples. Rather than replacing the existing do while example, I suggest we simply adapt the first excerpt in the Loop control statements section to be an 'infinite' loop and add text to explain the lack of counter variable as a valid construct.

As you allude to, the choice of an 'infinite' do loop versus a do while loop is merely a personal choice and so we should objectively present both options. (I personally prefer do while loops when there is only one terminating condition since it makes the exit condition clear upfront without needing to inspect the body of the loop.)

arjenmarkus commented 3 years ago

Would it be an idea to gather typical constructions (the "Fortran idiom")? Mind you, I have not looked in detail at the tutorial, just something I realised might be useful.

Op di 25 mei 2021 om 14:39 schreef Laurence Kedward < @.***>:

Yes we should present the option of an 'infinite' do loop with a conditional exit in addition to the existing loop examples. Rather than replacing the existing do while example, I suggest we simply adapt the first excerpt in the Loop control statements https://github.com/fortran-lang/fortran-lang.org/blob/master/learn/quickstart/operators_control_flow.md#loop-control-statements-exit-and-cycle section to be an 'infinite' loop and add text to explain the lack of counter variable as a valid construct.

As you allude to, the choice of an 'infinite' do loop versus a do while loop is merely a personal choice and so we should objectively present both options. (I personally prefer do while loops when there is only one terminating condition since it makes the exit condition clear upfront without needing to inspect the body of the loop.)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/fortran-lang/webpage/issues/83, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN6YR4WWEMLO2ZBTTMRYJLTPOK6JANCNFSM45PHTEFA .