VBA->JS: Conditionals, Loops & Operators 3 (Syntax)

In the third part of working with conditionals we’re going to look at some looping operations. Again, the concept is the same as what’s used in VBA, just different syntax.

Loops repeat a series of actions until a condition is met. In this and the next article three different kinds are discussed.

While loops
In VBA there is a set of four variations of the “Do-Loop” type: Do While...Loop, Do Until...Loop, Do...Loop While and Do...Loop Until. The first two perform the check whether to continue at the beginning of the loop, meaning it’s possible that the loop is never executed. The last two check at the end of the loop, so you can be sure the loop will execute at least once. The variations with “While” mean the loop is executed as long as the condition is met; “Until” means the loop is executed until the condition is fulfilled.

JavaScript has only two variations, one which performs the test at the beginning of the loop, the other at the end. Both use the “While” logic. They’re referred to as “While” and “Do…While” loops.

VBA JS
Dim i as Long
Do While i < 10 i = i + 1 Loop Debug.Print i 'Result: 10
var i = 0;
while (i < 10) { i++; } i; //Result: 10
Dim i as Long
Do
i = i + 1
Loop While i < 10 Debug.Print i 'Result: 10
var i = 0;
do {
i++;
} while (i < 10) i; //Result: 10

Notice how the comparison in JavaScript is in parentheses with no following semicolon - just like "if" and "switch-case" comparisons.
Also note the curly braces, which are positioned the same as for "if" blocks.

Incremental operators
New in this series of posts is the way the counter, i, is incremented using a double plus: ++. You could write it out the same way as in VBA, but typing a double plus is much faster. You'll rarely, if ever, see JavaScript incrementation written as in VBA!

You may well ask whether there are other short-cuts of this kind and, indeed, the minus sign can be doubled up to perform a -1 decrement: --.

The incrementation operators can be before or after the variable to be incremented. Usually, you'll see them as here, after the variable. To learn more, see http://www.webdevelopersnotes.com/tutorials/javascript/javascript_increment_decrement_operators.php3

For loop
"For...Next" works in a similar way to the "While" loops: actions are performed until a condition is met. Which you use is often a matter of preference. The JavaScript syntax is a bit confusing, at first:

VBA JS
Dim i as Long
For i = 1 To 10
i = i + 1
Next
Debug.Print i 'Result: 11
var val = 0;
for (var i = 0; i <= 10; i++) { val = i; } val; //Result: 10 i; //Result: 11

A number of things stand out, looking at the JavaScript syntax:

  1. Here, as in the previous conditionals, what is to be compared is in parentheses and the opening curly brace is on the same line.
  2. An additional variable, val, is defined before the loop and the value of the counter assigned to it.
  3. Looking at the last two lines of code, the reason becomes apparent: the counter is incremented before the loop is executed. Even if the loop is not executed because the condition is met, in JavaScript the counter remains incremented.
  4. Instead of the condition having two parts, as in the VBA "For", the JavaScript version has three parts, separated by semicolons: the initialization, the comparison and the incrementation. Actually, VBA does have the third part, it just lets you leave it out when the counter is being incremented by steps of 1. Anything else and you need to include the incrementation step in VBA, as well. For example, in order to count backwards: For i = 10 To i = 0 Step -1
  5. You'll notice that the variable i is declared within the parentheses as part of the initialization. JavaScript will also let you declare it before the "for"-block (unlike C#), but this is not common.

That leaves the For Each...Next loop type, which I'll cover in the next post, along with the basics of arrays - because in JavaScript, unlike VBA, you can loop arrays using this type of loop, rather than needing to specify a counter.



2 Responses to “VBA->JS: Conditionals, Loops & Operators 3 (Syntax)”

  1.   Felipe Costa Gualberto Says:

    Maybe a correction. In the VBA example:
    Debug.Print i
    It returns 11, not 10.

    •   WordMeister Says:

      Hi Felipe

      Hmmm. Yes, I do find an error with the Result:, but in the first pair and in both VBA and JS. They return 10, not 9 (and I’ve made the correction). But nothing is returning 11?

      Got it, the “For” loop example. Corrected that, as well…

Leave a Reply