DEV Community

Cover image for Writing for-loops in an untold language
Eckehard
Eckehard

Posted on

Writing for-loops in an untold language

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)
Enter fullscreen mode Exit fullscreen mode

or in a common abbreviation used in C or Javascript:

    for(let a=start; a<=end; a++) { ... }
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (6)

Collapse
 
ooosys profile image
oOosys • Edited

I suggest stopping the pursuit of an answer to the question, "Why are things the way they are?" Even if provided, such answers are often artificially invented and probably not true explanations. Their main function seems to be quieting the mind that posed the question in the first place, keeping it occupied with the search for an answer. In my opinion, the root of the by you observed issue traces back to the "Tower of Babel" and persists into modern times.

A more effective approach to dealing with such questions, in my view, is to explore whether anything can be done about the situation. If change is possible, then consider taking action. Admittedly, "stupidity rules the world," and I believe there's not much that can be done to alter this reality except attempting to distance oneself from the "insane madness"—at least to some extent.

Collapse
 
ooosys profile image
oOosys

There is a typo (missing "y") in: "Programmers ignored the "human readability" because they did not know better or the did not care at all."

Collapse
 
efpage profile image
Eckehard

Thank you, hope we get AI soon to check for typos :-)

Collapse
 
ooosys profile image
oOosys

Just use ChatGPT ... it does an excellent job and English is where it really excels.

Collapse
 
joshuaamaju profile image
Joshua Amaju • Edited

Your React example is completely and utterly incorrect

Collapse
 
efpage profile image
Eckehard

I´m not a React-specialist, so please correct me. As far as I remember, this was the source which Ih possibly got wrong. But that is exactly the topic!