Controlling What Happens Next
There are several tools that you can use to indicate the next step in a script.
Topics:
if … else
You use the conditional when you want to perform a set of commands only if something is true. Here is an example:
MyVar = 1;
if(myVar > 0) {
aa.print(“Yes.”)
}
This script displays this output:
Yes.
The conditional begins with the word “if” followed by a pair of parentheses. You can place any expression between these parentheses. A pair of braces “{“ and “}” follow the parentheses. The braces contain one or more commands. If the expression between the parentheses evaluates to true then the commands between the braces executes. If the expression between the parentheses evaluates to false then the commands between the braces do not execute. The example script does print out the word “Yes” because it is true that myVar, which has the value one, is greater than zero. If you set myVar was to negative one, then nothing prints out.
You use the “else” clause to specify what happens when the condition is false. Here is an example:
MyVar = 1;
if(myVar > 2) {
aa.print(“Yes.”)
} else {
aa.print(“No”);
}
This script displays this output:
No.
In this example, we have changed the conditional to test if myVar is greater than two. Because the condition evaluates to false, the else block executes instead of the main block. We use the word “block” to refer to a group of commands between a matching pair of braces. The first block prints “Yes” if the condition is true. The second block prints “No” if the condition is false. Because the condition is false, this example prints out “No”.
You can also create a multi way branch with several blocks. Here is an example:
myVar = “Bagels”;
if(myVar == “Oranges”) {
aa.print(“Fruit.”)
} else if(myVar == “Bagels”) {
aa.print(“Cereals.”);
} else if(myVar == “Spinach”) {
aa.print(“Vegetables.”;
} else {
aa.print(“I don’t known what food group that is in.”;
}
This script displays this output:
Cereals.
This script contains several possible blocks that can execute, depending on the value of myVar. Because myVar has the value “Bagels”, the second block executes, and the word “Cereals” prints out. The final else clause is optional.
for
There are several kinds of loops in JavaScript. The for loop allows a script to repeat a set of commands repeatedly until some condition is false. You typically use the for loop when you know how many times you want to repeat the loop. Here is an example:
for(i = 1; i < 6; i = i + 1) {
aa.print(“The current value of the loop counter is: “ + i);
}
This script displays this output:
The current value of the loop counter is: 1
The current value of the loop counter is: 2
The current value of the loop counter is: 3
The current value of the loop counter is: 4
The current value of the loop counter is: 5
The for loop begins with the word “for” followed by a pair of parentheses that contain three expressions, each separated by semi-colons. After the parentheses are a pair of braces that contain the statements that repeat by the loop. The three expressions in between the parentheses determine how many times the loop repeats. Let us look at these three expressions:
i = 1; i < 6; i = i + 1
The first expression is i=1. This expression set the value of the variable i to one as you might expect. This first expression executes one time, before Civic Platform executes the body of the loop. The body of the loop is the block, surrounded by a pair of braces that comes right after the parenthesis. The second expression is i < 6. This is the condition of the loop, and you only execute the body of the loop if this condition is true. Civic Platform checks the condition just before the body of the loop executes, and the loop continues to repeat until it is false. The third expression, i = i + 1, tells the loop how to update the loop counter each time you reach the end of the body of the loop. When you reach the end of the body of the loop, Civic Platform executes this statement. In this case, the third expression adds one to the counter. From the output of the example, you can see that each time through the loop the counter value updates and the counter value prints out. The loop stops when the value of i reaches six because the second expression is no longer true.
while
The “while” loop is another loop that repeats until its condition is false. You typically use this loop when you do not know how many times the loop executes. Here is an example:
myArray = new Array();
myArray[0] = “Oranges”;
myArray[1] = “Bagels”;
myArray[2] = “Spinach”;
i=0;
while(i < myArray.length) {
aa.print(myArray[i]);
i = i + 1;
}
This script displays this output:
Oranges
Bagels
Spinach
In this example, you create an array with three elements and you set the loop counter variable i to zero. The loop begins at the word “while”. Next is a pair of parentheses that contain the condition for the loop. While the condition is true, the body of the loop, which comes after the parentheses and is enclosed by a pair of brackets, repeats. We can see two commands inside the body of the loop. The first prints out the value of the array at the position that you indicate by the loop counter. The second line adds one to the loop counter. We need to be careful to remember to always add a line to add to the loop counter to the end of our while loop bodies, because if we do not then the loop never stops, and Civic Platform terminates the script after a time-out period has elapsed.
do … while
This loop also repeats until its condition is false. Use the “do” loop when you want to make sure to execute the body of your loop at least one time even if the condition is false before the loop starts. Here is an example:
myArray = new Array();
myArray[0] = “Oranges”;
myArray[1] = “Bagels”;
myArray[2] = “Spinach”;
i=0;
do {
aa.print(myArray[i]);
i = i + 1;
} while(i < 0);
This script displays this output:
Oranges
In this example we can see that the body of the do loop executes one time even though the condition i < 0 is false before the loop begins. The example shows that a do loop begins with the word “do” followed by a block, surrounded by braces, for the body of the loop. After the block is the word “while” followed by a pair of parentheses that enclose the condition for the loop. Unlike the other two loops, this last line of the loop, after the parentheses, ends with a semi-colon. Remember to put a command to change the counter in the body of the loop if you are using a counter to control the loop.