Eckehard
Posted on February 23, 2023
Writing for-loops is pretty simple: Just repeat a part of the code with a sequence of numbers in a given range. This might be - in a most natural way - done by a program like this:
let a = start
do {
...
a++
} while(a<=end)
or in a common abbreviation used in C or Javascript:
for(let a=start; a<=end; a++) { ... }
Simple? Probably.
Of course, there are many variations like forEach, for..in, for..of etc., but let's focus on the basic stuff: just to count from 1 to 10.
An easy task...
...just - not if you change your "language". People in the history of computer science, had been very creative, so there are several mutations of this topic:
language | Loop |
---|---|
Algol 58 | for i := 1(1)10 |
Algol 60 | for i := 1 step 1 until 10 do for i := 1 step 1 until 5, 6, 7, index + 1 until 10 do |
Fortran | do 20 i = 1, 10, 1 |
Python | for i in range(1, 10, 1):: |
Basic | FOR i = 1 TO 10 NEXT |
Pascal, Oberon | for i:= 1 to 10 do |
Modula | FOR i := 1 TO 10 BY 1 DO |
ADA | for i in 1..10 loop end loop; |
FORTH | 10 1 DO CR .i LOOP ; |
COBOL | PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10 |
PostScript | 1 1 10 {STATEMENTS} for |
MATLAB | for n = 1:10 end |
Bash | for (( i = 1; i <= 10; i++ ))do done |
Haskell | forM_ [1..10] $ \indx -> do statements |
PHP | for ($i = 1; $i <= 10; $i++) {} |
Ruby | for counter in 1..5 end |
Go | for i := 1; i <= 10; i++ {} |
Rust | for i in 1..10 {} |
Please excuse any syntax errors - not so easy to do things right in a wrong language. But really amazing, how many variations people found on such a simple topic. Just, most of them are more made for computers than for humans.
The Computer (hopefully) does exactly the same on all this statements. So, we can ask: what might that be good for to invent all this different versions? Does this make any sense to use a different syntax, if you mean the same thing? Maybe we go with Albert Einstein, who said: Two things are infinite, the universe and human stupidity, but I'm not quite sure about the universe yet.
As we saw, the history of computer life was often a pain, because people did not to care for any standards. Programmers ignored the "human readability" because they did not know better or the did not care at all. Luckily, life has changed a lot...?
a bit...?
not at all?
Let assume, we just want to print the numbers from 1 to 10 and we go with one of popular frameworks today. What might happen? Let us just look into the tutorials:
Angular | `*ngFor="let item of [].constructor(10); let i = index"` |
React | `let [counter, setCounter] = React.useState (); if (counter <10) {setCounter (prev => prev + 1);} else {setCounter (0);}` |
VUE | `v-for="i in 10" :key="i">{{ i }}` |
Svelte | `{#each Array(10) as _, index (index)}` |
Maybe there are other ways to do the job, but referring to the numerous questions i found on this topic, nothing really simple and straightforward. At least nothing, that is easy to understand or to use.
There are tons of documentation just to explain the merits and options of ngFor, v-For or #each. Just: they are - well, a bit limited: "There's no step definition in v-for"...
You may argue: these tags have been included for list rendering only. Just, sometimes it might be handy to render every second item of a list or a list in reverse order. There are some stupid people thinking, that should be possible.
But my main question is: There have been loops as long as computers are used. In Javascript there are at least 10 different ways to iterate over a list in every way you can think of. Why in hell was it necessary to invent a new kind of loop, that is "a bit" limited? As Albert Einstein said: we are not sure about the universe.
Posted on February 23, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.