Loops in Python and JavaScript
I’m juggling between JavaScript and Python (C for only Arduino) and sometimes I cannot wrap my head around the syntaxes, so I decided that it will be a good idea to create a table to refer to when I’m using different languages.
As a refresher,
- do is an entry loop, which is the condition is evaluated and the loop is started if the condition is true.
- do-while is an exit loop; the loop is executed at least once, even if the condition never evaluates to true. Until the exit condition is met, the loop runs.
Note here that Python does not have an explicit do-while construct. Therefore we “trick” Python by starting while true, which always evaluates to true and then specify the exit condition.
| Loop | Python | JavaScript |
|---|---|---|
| for |
|
|
| for in array (elements only) |
|
|
| for in array (index and elements) |
|
|
| for in objects | same for any iterable |
|
| while |
|
|
| do while |
|
|