As far as I know, for loops in Mel only run the first assignment ( $i=0;) once.
If i’m right, how does it know not to run it again and again as it goes thru the loop ?
proc cubix(int $num){
for ($i=0; $i<$num; $i++){ … }
As far as I know, for loops in Mel only run the first assignment ( $i=0;) once.
If i’m right, how does it know not to run it again and again as it goes thru the loop ?
proc cubix(int $num){
for ($i=0; $i<$num; $i++){ … }
That’s kind of like asking “How does it know to print something when I use the print
statement?”
The answer is: Because that’s how the language is designed. The MEL language has been designed to only run $i=0
only once at the beginning of the loop.
It sounds like you’re thinking there’s something like a “read head” that scans through the text top-to-bottom, left-to-right, reading the text as-is and executing exactly what it sees every time, jumping around when it hits flow controls. But that’s not the case.
From what I understand with interpreted languages like MEL, your statements will be broken down into some intermediate representation that then gets executed. There’s a lot of variation on exactly how this is accomplished. Each language does it differently (Python does this more explicitly with the .pyc files it creates), and I don’t know how MEL specifically works.
But, for the sake of teaching, It could take something like this for ($i=0; $i<$num; $i++){ ... }
and turn it into a more machine understandable version of this:
$i = 0;
while ($i < $num){
...
$i++;
}
A for
loop contains multiple parts that each do something different. But a while
loop is much simpler. And as long as you put the other statements in the correct places, it is 100% logically equivalent.
Now, this is a GROSS oversimplification. If you really want to dig into this stuff, google the terms “abstract syntax tree”, “parser”, and “lexer”