# Go To Statement Considered Harmful: Fortran examples

**URL:** <https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744>\
**Category:** Humor\
**Created:** [November 7, 2023, 12:53am UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744 "2023-11-07T00:53:58Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [November 7, 2023, 10:44pm UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/21 "2023-11-07T22:44:21Z")

</div>

> [@Harper](#):
>
> […] unless you might go more than `huge(i)` times round the loop […]

I can remember a time when this was unthinkable. These days, with 4+ GHz processors and 32-bit default integers, it is not an unreasonable situation.

I personally like the unindexed DO…ENDDO structure. When I see things like DO WHILE (.true.)…ENDDO in other languages, I think it looks like an inelegant hack around a missing language feature.

I’m unsure about the idea of a BLOCK structure hiding as a loop. It seems to me if you have a loop, which is a fundamental programming concept, it should be unambiguous that it is a loop.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [November 8, 2023, 2:13am UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/22 "2023-11-08T02:13:36Z")

</div>

> [@RonShepard](#):
>
> I’m unsure about the idea of a BLOCK structure hiding as a loop. It seems to me if you have a loop, which is a fundamental programming concept, it should be unambiguous that it is a loop.

💯 If you want to go back to the beginning of a `block` of code, that’s a loop, which in Fortran is spelled `do`.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [November 8, 2023, 2:19am UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/23 "2023-11-08T02:19:42Z")

</div>

> [@everythingfunctional](#):
>
> […]which in Fortran is spelled `do`

I agree. It is spelled DO, not BLOCK. that is what I meant, that the looping structure should be obvious.

---

<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:** [November 8, 2023, 8:41am UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/24 "2023-11-08T08:41:03Z")

</div>

> [@ivanpribec](#):
>
> So what’s the purpose of this thread? **Use this thread to share the worst cases of GOTO 👹 _out in the wild_**.

Don’t forget to use the following hidden GOTOs on your highway to Hell, it could probably be useful for a better entanglement. This example comes from the Fortran 2018 standard draft (I have just modified the `err` value):

> Figure 10.3 Testing for an error or the end of the file.

```fortran
read (nunit, ’(3f10.3)’, iostat=ios, err=666, end=120) a,b,c 
! Successful read - continue execution. 
... 
666 call error (ios) ! Error condition - take appropriate action. 
return 
120 call last_file ! End of file - test for more files.

```

I am always surprised when I am compelled to use such error labels in modern Fortran. Like meeting Cthulhu…

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [November 11, 2023, 1:05pm UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/25 "2023-11-11T13:05:02Z")

</div>

> [@vmagnin](#):
>
> I am always surprised when I am compelled to use such error labels in modern Fortran. Like meeting Cthulhu…

It’s funny you mention Cthulhu 😆; the monster also appears on the cover of a **C** book: [Effective C | No Starch Press](https://nostarch.com/Effective_C)

![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/e/e06af6d6cdf187b07d6906881a5b2775f5928076.jpeg)

---

<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:** [November 11, 2023, 2:38pm UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/26 "2023-11-11T14:38:10Z")

</div>

> [@ivanpribec](#):
>
> It’s funny you mention Cthulhu 😆; the monster also appears on the cover of a **C** book: [Effective C | No Starch Press](https://nostarch.com/Effective_C)

Well, a monstrous thing of the past with tentacles 🐙, isn’t it GOTO and spaghetti code 🍝 ?

---

<div class="post-metadata">

**Author:** ![jeremie.vandenplas](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jeremie.vandenplas](https://fortran-lang.discourse.group/u/jeremie.vandenplas)\
**Post date:** [November 11, 2023, 7:15pm UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/27 "2023-11-11T19:15:15Z")

</div>

When implementing them in [`stdlib`](https://stdlib.fortran-lang.org/page/specs/stdlib_stats_distribution_normal.html), there were some discussions about the algorithms (see e.g. [this issue](https://github.com/fortran-lang/stdlib/issues/234)). some references can be also found at [the end of this page](https://stdlib.fortran-lang.org/page/specs/stdlib_stats_distribution_normal.html).

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [November 12, 2023, 2:15am UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/28 "2023-11-12T02:15:21Z")

</div>

Octopuses at least are pretty intelligent and nifty, just like goto. 😉

Anyways, to deepen the discussion on goto’s, it appears this is a quite big topic in decompilation and malware analysis. I’ve found several interesting academic works on the topic:

- Erosa, A. M., & Hendren, L. J. (1994, May). **Taming control flow: A structured approach to eliminating goto statements.** In _Proceedings of 1994 IEEE International Conference on Computer Languages (ICCL’94)_ (pp. 229-240). IEEE. [https://www.cs.tufts.edu/comp/150FP/archive/laurie-hendren/taming.pdf](https://www.cs.tufts.edu/comp/150FP/archive/laurie-hendren/taming.pdf)
- Pan, S., & Dromey, R. G. (1996). **A formal basis for removing goto statements.** _The Computer Journal_, _39_(3), 203-214. [Formal Basis For Removing Goto Statements | The Computer Journal | Oxford Academic](https://doi.org/10.1093/comjnl/39.3.203)
- Yakdan, K., Eschweiler, S., Gerhards-Padilla, E., & Smith, M. (2015, February). **No More Gotos: Decompilation Using Pattern-Independent Control-Flow Structuring and Semantic-Preserving Transformations.** In _NDSS_. [https://net.cs.uni-bonn.de/fileadmin/ag/martini/Staff/yakdan/dream\_ndss2015.pdf](https://net.cs.uni-bonn.de/fileadmin/ag/martini/Staff/yakdan/dream_ndss2015.pdf)

The [SPAG code restructuring](https://fortran.uk/fortran-analysis-and-refactoring-with-plusfort/spag-fortran-code-restructuring/) tool has some amazing looking examples of automatic goto transformation. This doesn’t mean you’ll understand the code purpose, but at least it’s much more readable and easy to track the flow. I wonder how complex the algorithms that SPAG uses are, and if they were developed independently.

I imagine it started similar to the xkcd comic:

![](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/7/71c49cc55852ea32a01a71c9212d4e2e5bee8b05.png)

---

<div class="post-metadata">

**Author:** ![MarDie](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mardie/32/54_2.png) [@MarDie](https://fortran-lang.discourse.group/u/MarDie)\
**Post date:** [November 20, 2023, 1:28pm UTC](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744/29 "2023-11-20T13:28:25Z")

</div>

If someone wants to follow up, [Simulating Normal Random Variables – Quant Girl](https://quantgirl.blog/box-muller-and-marsaglia-bray/) has nice explanations.

[Previous page](https://fortran-lang.discourse.group/t/go-to-statement-considered-harmful-fortran-examples/6744.md?page=1)
