# Closing if-loops by "endif" vs. "end if"

**URL:** <https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651>\
**Category:** Homework\
**Created:** [August 8, 2021, 2:15pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651 "2021-08-08T14:15:12Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [August 8, 2021, 2:15pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/1 "2021-08-08T14:15:12Z")

</div>

When writing a conditional loop, one functional pattern is

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

```

However, so far, I did not notice a functional difference with

```auto
if (angle < 90.0) then
  print *, 'Angle is acute'
endif

```

with `endif`, without a space between `end` and `if`. (Similar to this, `do` iterations closed by either `end do`, or `enddo`.)

Is backward compatibility to FORTRAN a reason why Fortran permits both approaches to convey the (apparently) same instruction?

At present (excluding this post), posts in this forum prefer the separated form:

```auto
  | keyword | instances |
  |----------+-----------|
  | "endif" | 32 |
  | "end if" | 50+ |
  |----------+-----------|
  | "enddo" | 27 |
  | "end do" | 50+ |
  |----------+-----------|

```

This and `end if` (with space) being used in fortran-lang’s [quickstart tutorial](https://fortran-lang.org/learn/quickstart/operators_control_flow#conditional-construct-if) suggest it either is just _more popular_ (within this population). Or, is it today’s _recommended_ approach for writing anything new in Fortran?

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [August 8, 2021, 3:20pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/2 "2021-08-08T15:20:24Z")

</div>

There is no difference between `ENDIF` and `END IF`. Both are valid.

I looked into the FORTRAN 77 standard and it is written `END IF`. In the Fortran 2018 standard I found only three occurrences of `ENDIF`, which means the `END IF` version is preferred.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [August 8, 2021, 4:08pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/3 "2021-08-08T16:08:45Z")

</div>

Indeed, the modern way is to put space in it.

These are one of the things I am hoping the “opinionated modern” (pedantic/strict) mode in LFortran ([#450](https://gitlab.com/lfortran/lfortran/-/issues/450)) would simply warn against, so if you use `endif`, you would get a warning to use `end if`. That way it would help users to converge on the modern style.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [August 8, 2021, 5:58pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/4 "2021-08-08T17:58:22Z")

</div>

Can you add a style warning for REAL(4) and the like please?😃

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [August 8, 2021, 6:00pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/5 "2021-08-08T18:00:20Z")

</div>

The requirement to have `if/do` to appear after `end` seems rather redundant. I personally prefer to have `if/do` in closing a block/loop as it significantly improves readability. But I have also seen users and critiques counting this verbosity as a Fortran weakness. This argument is absolutely baseless, in my opinion. But at the same time, I do not understand why the presence of `if/do` following an `end` is mandatory in Fortran. Compilers should not have trouble decoding the meaning of an `end` as opposed to `end if` or `end do.`

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [August 8, 2021, 7:26pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/6 "2021-08-08T19:26:58Z")

</div>

> [@themos](#):
>
> Can you add a style warning for REAL(4) and the like please?😃

I put it on the [list](https://gitlab.com/lfortran/lfortran/-/issues/450). Thanks!

---

<div class="post-metadata">

**Author:** ![Euler-37](https://avatars.discourse-cdn.com/v4/letter/e/b19c9b/32.png) [@Euler-37](https://fortran-lang.discourse.group/u/Euler-37)\
**Post date:** [August 9, 2021, 2:37am UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/7 "2021-08-09T02:37:43Z")

</div>

> [@certik](#):
>
> These are one of the things I am hoping the “opinionated modern” (pedantic/strict) mode in LFortran ([#450](https://gitlab.com/lfortran/lfortran/-/issues/450)) would simply warn against, so if you use `endif` , you would get a warning to use `end if` . That way it would help users to converge on the modern style.

Fortran’s `function` have many forms

```fortran
   !> form 1
    function foo()
        integer::foo
    end function foo
  !> form 2
    integer function foo()
    end function foo
  !> form 3
    integer function foo()result(res)
    end function foo
   !> form 4
    function foo()result(res)
        integer::res
    end function foo

```

Is it possible to delete any forms and just keep one form(may in `strict mode`)?

I prefer `form 4`, because its return value is clearer than `form 1`,and if the return value is an array, `form 2` and `form 3` must write statement like `dimension(n,n)::res`, Thanks!

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 9, 2021, 3:16am UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/8 "2021-08-09T03:16:53Z")

</div>

I agree, and that is what the F subset supports – [see R1217](https://pages.swcp.com/~walt/F/F_bnf.html), and also ELF90, I think. A starting point for a modern Fortran subset could be F plus what was added in the Fortran 2003 and later standards.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [August 9, 2021, 4:08am UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/9 "2021-08-09T04:08:52Z")

</div>

> [@Euler-37](#):
>
> Is it possible to delete any forms and just keep one form(may in `strict mode` )?

It is possible and I added it to the list. I prefer the form 3 usually, but for arrays I use the form 4. I agree that it is quite confusing to newcomers the 4 different ways of writing a function, and reducing this number would help if we can all agree on one or two forms.

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 10, 2021, 1:38pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/10 "2021-10-10T13:38:43Z")

</div>

> [@shahmoradi](#):
>
> I do not understand why the presence of `if/do` following an `end` is mandatory in Fortran.

We need a way of differentiating between the different meanings of END.

In Fortran 77, “… an END statement is also an executable statement and must appear only as the last statement of a program unit”.

Modern Fortran does not require the keywords PROGRAM, SUBROUTINE, etc., after an END statement that terminates a program unit. How do you then differentiate between an END statement that means “return” and an END statement that ends an IF block, etc.?

---

<div class="post-metadata">

**Author:** ![fortran4r](https://avatars.discourse-cdn.com/v4/letter/f/9de0a6/32.png) [@fortran4r](https://fortran-lang.discourse.group/u/fortran4r)\
**Post date:** [October 10, 2021, 5:20pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/11 "2021-10-10T17:20:28Z")

</div>

Matlab has been around for more than 40 years, and you still think end if and end do are impossible to write as just end?

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 10, 2021, 6:34pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/12 "2021-10-10T18:34:00Z")

</div>

There are actually some ambiguities with regards to parsing of `end`, I don’t know if they are solvable or not in this case. Anything can be done with a backtracking parser or more than one look ahead. But it is also important to ensure parsing is fast. We would have to prototype it and see.

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [October 10, 2021, 6:55pm UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/13 "2021-10-10T18:55:32Z")

</div>

I personally write in the most verbose possible style in any programming language for clarity and readibility. I think it is possible to avoid the labels if needed/wanted. MATLAB is an example, as fortran4r mentioned. But I get your point (which appears in line with Certik’s concerns). The `end` word is a reserved keyword in MATLAB and cannot be used as a variable name. There is no reserved keyword in Fortran, which is great, but it _could_ create ambiguities, although I doubt if it really does. It certainly makes parsing more difficult for the compilers.

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 11, 2021, 2:12am UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/14 "2021-10-11T02:12:04Z")

</div>

Well, we could use braces (’{}’ ) instead of **begin** and **end** , but such changes to the language have to be proposed, discussed and accepted. If these changes cause large bodies of existing F77 and older codes to become invalid, there will be a need for compilers to have a legacy mode (or option) to enable these older codes to remain compilable.

Matlab as a programming language [has many irregularities](https://www.nayuki.io/page/matlab-language-pet-peeves), some of which may become major issues when ported to a compiled language. For example, the effective name of the first function in an m-file is taken from the file name and not the declaration of the function.

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [October 11, 2021, 2:27am UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/15 "2021-10-11T02:27:05Z")

</div>

> [@mecej4](#):
>
> there will be a need for compilers to have a legacy mode (or option) to enable these older codes to remain compilable.

`-std=legacy` of gfortran? Or one could even keep everything as before, but enforce the latest semantics when `-std=f202x` is specified, until all codebases catch up. Intel compiler used to default to Fortran \<2003 semantics until around 2015 (when I and perhaps many others like me began to criticize the Intel compiler’s tradition in the Intel forum).

I find the supreme oddity of MATLAB to be the fact that everything is a 2D matrix, by default.

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [October 11, 2021, 9:08am UTC](https://fortran-lang.discourse.group/t/closing-if-loops-by-endif-vs-end-if/1651/16 "2021-10-11T09:08:04Z")

</div>

I write utility programs in C sometimes. After reading the comments above regarding END IF versus END in Fortran, I remembered the many occasions where a missing or misplaced ‘}’ caused the C compiler to issue mystifying error messages.

Here is a part of the C source for [function nqueens from Rosettacode](http://www.rosettacode.org/wiki/N-queens_problem#C). Near the end of the function, we see a number of closing braces. I have added comments to indicate what construct it is that a particular ‘}’ terminates.

```auto
          } // end if
        } // end while
        posib = posibs[--d]; // backtrack ...
      } // end while
    } // end for
  } // end for
  return num*2;
} // end function

```

If one of those braces was missing, and the code was not properly indented, or there were additional statements between successive ‘}’ s, would the error messages from the C compiler tell the programmer where to place the missing ‘}’?
