Each statement can be terminated either by a new line code or by a semicolon. To keep the working practice in line with general programming practice, it is recommended that you always use a semicolon at the end of a line, even though not strictly necessary.

One situation where a semicolon is required is in Inline Scripting.

The loop statements (for and while) and the if statement generally work with a block of statements (all the statements inside curly braces) but these statements will also work with a single following statement instead of a block.

e.g.   if (x < 3) x++;
      
 else x--;

 

Statement

Description

;

Defines the end of a statement.

{a; b....}

Defines a block of zero or more statements.

break

Statement that terminates the current while or for loop and transfers program control to the statement following the terminated loop.

continue

Statement that terminates execution of the block of statements in a while or for loop, and continues execution of the loop with the next iteration.

for (a;b;c) {x; y..}

Statement that creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a single statement or a block of statements executed in the loop.

The first expression is a 'initial' statement run once before the first loop is started.

The second expression is a stop condition. Tested at the start of each loop and If the condition is true then the loop is executed and if the condition is false then the statement is terminated.

The third expression is an 'end of each loop' statement run at the end of each loop.

for (a in b) {x; y..}

Statement that creates a loop that executes once for each property held by object b (this is called enumeration). Followed by a block of statements executed in the loop. At the start of each loop the variable a is set to the name of the property held by b. There is no guaranteed order in the selection of the properties held in b.

Tip: you can use the indirect reference operator shown in the table of Operators to access the objects held in b.

e.g.

 house.table = "dining";
 for (property in house) {
   write(property);
   write(house[property]);
 }

This example prints out the word 'table' and then the word 'dining'.

Note: This statement only returns the names of properties that can be enumerated. It is possible for an object to have properties that are not enumerated (e.g. most functions are properties that are not enumerated).

if (a) {x; y..}
 else if (b) {x; y..}
 else {x; y..}

Statement that executes a set of statements if a specified condition is true.

If the condition a is true then execute the single statement or block of statements following a. If b is true the execute the statement or block of statements following b. If both a and b are false then execute the single statement or block of statements in the else clause.

There can be any number of else if clauses. Only one block of statements is executed. If more than one else if clause has a condition that is true then the first one encountered is executed.

x = a?b:c

Statement that conditionally assigns a value. If the condition a is true then assign x to b, if a is false then assign x to c.

while (a) {x; y..}

Statement that creates a loop that evaluates an expression, and if it is true, executes a single statement or a block of statements.

The condition is tested at the start of each loop.

function() {x; y..}

Statement indicating that the following block of statements defines a function. The syntax is:

 function functionName (param1, ....., paramN) {......}

return

Statement that terminates execution of a function. This can be followed by an expression to return a value.

Comment on this topic

Topic ID: 150096