Skip to content

Statements

Nathan Burgess edited this page Oct 17, 2017 · 1 revision

if, elif, and else

You can use the if statement to conditionally execute part of your program based on the truth value of a given expression. In the case that more than one condition needs to be evaluated, the elif statement is used.

if expression
    if-statement
elif
    elif-statement
else
    else-statement

while

The while statement is a loop statement with an exit test at the beginning of the loop. If the test evaluates true then the statement is executed. The statement continues to be executed as long as the test evaluates to true.

for

A for loop is used to iterate over the iterable values of an Array or Object, or over a range of values. The loop will continue until every element in the given object has been used or the loop encounters a break or continue.

// Loop over each element of the Array
Array a = [1, 2, 3, 4]
for value in a

Where value is a temporary variable that is assigned the first value that exists in object. When the loop repeats, value will hold the second value in object, and so on.

// Loop over a range, where i will hold the value at each step
for int i in 2 to 10

break

When a loop needs to end prematurely, the break keyword is used. If used within nested loops, a break will only operate on the innermost loop in which it was called.

continue

When a loop needs to skip a particular iteration, the continue keyword is used. When continue is called, the loop resets and any code after the continue will not be executed on that iteration. If used within nested loops, a continue will only operate on the innermost loop in which it was called.

Comments

Single-line comments in begin with // and end at the end of the line. Multi-line comments which start with /* and end with the */.

Clone this wiki locally