Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"do / else" proposal #342

Open
certik opened this issue Oct 31, 2024 · 7 comments
Open

"do / else" proposal #342

certik opened this issue Oct 31, 2024 · 7 comments

Comments

@certik
Copy link
Member

certik commented Oct 31, 2024

https://fortran-lang.discourse.group/t/for-else-or-do-else-in-fortran/8696

Example:

condition = .false.
do i = 1, 10
    .......
    if (condition) exit
else
    ! This block only executes if the do loop doesn't exit.
    error stop "Condition not met"
enddo

which would be equivalent to:

condition = .false.
do i = 1, 10
    ......
    if (condition) exit
enddo
if (.not. condition) error stop "Condition not met"

Similar to Python's "for else": https://docs.python.org/3/tutorial/controlflow.html#else-clauses-on-loops

@everythingfunctional
Copy link
Member

Just to be clear, the above is equivalent to the below?

condition = .false.
do i = 1, 10
    ...
    if (condition) exit
end do
if (i > 10) then
    ! This block only executes if the do loop doesn't exit.
    error stop "Condition not met"
end if

@certik
Copy link
Member Author

certik commented Oct 31, 2024

Yes. I updated the above description with an example. (I don't like using loop variables after the loop, but that's a separate issue: is it even specified by the standard?)

@everythingfunctional
Copy link
Member

is it even specified by the standard?

Yes, although it's not immediately obvious. From 11.1.7.4.3 The execution cycle

The DO variable, if any, is incremented by the value of the incrementation parameter m3

and from 11.1.7.4.5 Loop termination

When a DO construct becomes inactive, the DO variable, if any, of the DO construct retains its last defined value.

So if a do loop completes all its iteration, the standard says it has a value > the ending value.

@certik
Copy link
Member Author

certik commented Nov 1, 2024

@everythingfunctional thanks for the clarification!

@klausler
Copy link

klausler commented Nov 1, 2024

Looks like syntactic vinegar for the existing and less mysterious

search: block
  do j = 1, 10
    ...
    if (condition) exit search
  end do
  error stop 'nope'
end block

@chuckyvt
Copy link

chuckyvt commented Dec 3, 2024

Try fully sketch this out, I think the following example would also be included.

condition = .false.
i = 1
do while (i < 11 )
    .......
    if (condition) exit
    i = i + 1
else
    ! This block only executes if the do loop doesn't exit.
    error stop "Condition not met"
enddo

Not exactly sure how the below case should be handled. Python explicitly ties it to a break statement, so perhaps it doen't execute.

condition = .false.
do 
    .......
    if (condition) go to 10
10 else
    ! This doesn't execute since there is no exit statement?  
    error stop "Condition not met"
enddo

@chuckyvt
Copy link

chuckyvt commented Dec 3, 2024

I think current approaches to catch failures can be lumped into two general groups, along with what I believe are limitations of those approaches. I believe these limitations would mostly be addressed with a do / else approach.

GO TO / Exit Block: Use either a GO TO or exit block statement to jump forward and skip the error condition code. This is likely the most common approach. The downsides is jump statements can become difficult to follow and breaks up the flow of the code.

Check loop variable after loop completes: It's not obvious at first glance that the if statement is conditionally executed based on the loop exit state. And since the if block isn't explicitly tied to the do loop, the do conditional statement can get out of sync with the if conditional statement if the programmer isn't careful.

For an interesting discussion of this feature in Python, see below link .
https://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants