2.5. Loops and Iteration

You have already seen a couple of examples of iteration and looping in Javascript. So this section will just serve as a reference for the differences in Syntax.

2.5.1. Definite Loop

In Python the easiest way to write a definite loop is using the for loop in conjunction with the range function. For example:

for i in range(10):
   print(i)

In Javascript we would write this as:

for (let i = 0; i < 10; i++ ) {
    writeln(i);
}

Recall that the range function provides you with a wide variety of options for controlling the value of the loop variable.

range(stop)
range(start,stop)
range(start,stop,step)

The Javascript for loop is really analogous to the last option giving you explicit control over the starting, stopping, and stepping in the three clauses inside the parenthesis. You can think of it this way:

for (start clause; stop clause; step clause) {
    statement1
    statement2
    ...
}

If you want to start at 100, stop at 0 and count backward by 5 the Python loop would be written as:

for i in range(100,-1,-5):
    print(i)

In Javascript we would write this as:

for (let i = 100; i >= 0; i -= 5)
    writeln(i);

In Python the for loop can also iterate over any sequence such as a list, a string, or a tuple. Javascript also provides a variation of its for loop that provides the same functionality in its so called for each loop.

In Python we can iterate over a list as follows:

l = [1, 1, 2, 3, 5, 8, 13, 21]
for fib in l:
   print(fib)

In Javascript we can iterate over an Array of integers too:

The for of construct is new in Javascript, you will see lots more code examples written as a for in

To be clear in the example above, the loop variable i is an index variable that you use to index into the original array.

To iterate over the characters in a string in Javascript do the following:

String t = "Hello World";
for (let c of t) {
    writeln(c);
}

To iterate over the elements in an array in Javascript:

var data = [3, 7, 2, 9, 1, 11];
var sum = 0;
data.forEach(function(d){
    sum += d;
});
writeln(sum)

2.5.2. Indefinite Loops

Both Python and Javascript support the while loop. Recall that in Python the while loop is written as:

while  condition:
   statement1
   statement2
   ...

In Javascript we add parenthesis and curly braces to get:

while (condition) {
    statement1
    statement2
    ...
}

Javascript adds an additional, if seldom used variation of the while loop called the do loop. The do loop is very similar to while except that the condition is evaluated at the end of the loop rather than the beginning. This ensures that a loop will be executed at least one time. Some programmers prefer this loop in some situations because it avoids an additional assignment prior to the loop. For example:

do {
    statement1
    statement2
    ...
} while (condition);
Next Section - 2.6. Javascript Data Types