Writing for-loops in an untold language
Tags: angular, react, vue, svelte
Published: 2023-02-23 00:58:34

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:
```JS
    let a = start
    do {
       ...
       a++
    } while(a<=end)
```
or in a common abbreviation used in C or Javascript: 
```JS
    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:<br>
<table>
<tr><th>language</th><th>Loop</th></tr>
<tr><td>Algol 58</td>
<td>for i := 1(1)10</td></tr>
<tr><td>Algol 60</td>
<td>for i := 1 step 1 until 10 do<br>
for i := 1 step 1 until 5, 6, 7, index + 1 until 10 do</td></tr>
<tr><td>Fortran</td><td>do 20 i = 1, 10, 1</td></tr>
<tr><td>Python</td><td>for i in range(1, 10, 1)::</td></tr>
<tr><td>Basic</td><td>FOR i = 1 TO 10<br>
NEXT</td></tr>
<tr><td>Pascal, Oberon</td><td>for i:= 1 to 10 do</td></tr>
<tr><td>Modula</td><td>FOR i := 1 TO 10 BY 1 DO</td></tr>
<tr><td>ADA</td><td>for i in 1..10 loop<br>
end loop;</td></tr>
<tr><td>FORTH</td><td>10 1 DO  CR .i  LOOP ;</td></tr>
<tr><td>COBOL</td><td>PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10</td></tr>
<tr><td>PostScript</td><td>1 1 10 {STATEMENTS} for</td></tr>
<tr><td>MATLAB</td><td>for n = 1:10<br>
end</td></tr>
<tr><td>Bash</td><td>for (( i = 1; i <= 10; i++ ))do<br>
done</td></tr>
<tr><td>Haskell</td><td>forM_ [1..10] $ \indx -> do statements</td></tr>
<tr><td>PHP</td><td>for ($i = 1; $i <= 10; $i++) {}</td></tr>
<tr><td>Ruby</td><td>for counter in 1..5<br>
end</td></tr>
<tr><td>Go</td><td>for i := 1; i <= 10; i++ {}</td></tr>
<tr><td>Rust</td><td>for i in 1..10 {}</td></tr>
</table>
*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: 
<table>
<tr><td>Angular</td><td>`<pre>*ngFor="let item of [].constructor(10); let i = index"</pre>`</td>
</tr>
<tr><td>React</td><td>`<pre>let [counter, setCounter] = React.useState ();
if (counter <10) 
    {setCounter (prev => prev + 1);} 
else 
    {setCounter (0);}</pre>`
</td></tr>
<tr><td>VUE</td><td>`<pre>v-for="i in 10" :key="i">{{ i }}</pre>`</td></tr>
<tr><td>Svelte</td><td>`<pre>{#each Array(10) as _, index (index)}</pre>`</td></tr>
</table>
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.