Control Flow
Control flow is used to determine which blocks of code are executed and how many times. Conditional statements and expressions decide whether or not to execute some code and looping ones execute something more than once.
Truthiness
Logical Operators
Unlike most other operators in Ghost which are just a special syntax for method calls, the and
and or
operators are special. This is because they only conditionally evaluate right operand--they short-circuit.
An and
("logical and") expression evaluates the left-hand argument. If it's false, it returns that value. Otherwise it evaluates and returns the right-hand argument.
print(false and 1)
print(1 and 2)
An or
("logical or") expression is reversed. If the left-hand argument is true, it's returned, otherwise the right-hand argument is evaluated and returned:
print(false or 1)
print(1 or 2)
Conditional Statements
A conditional statement is a set of commands that executes if a specified condition is true.
If Statements
The simplest conditional statement, if
lets you conditionally skip a chunk of code. It looks like this:
if (condition) {
// Do something
}
This evaluates the parenthesized expression after if
. If it's true
, then the block after the condition is evaluated. Otherwise it is skipped. The condition can be any expression that evaluates to true
or false
.
You may also provide an else
branch. It will be executed if the condition is false
:
if (condition) {
// Do something
} else {
// Do something else
}
You can also chain if
statements together with else if
to check multiple conditions:
if (firstCondition) {
// Do something
} else if (secondCondition) {
// Do something else
} else if (thirdCondition) {
// Do something else
} else {
// Do something else
}
Switch Statements
The switch
statement evaluates an expression, matching the expression's value to a case
clause, and executes statements associated with that case
. They're similar to a series of if
statements but are more concise and easier to read when evaluating multiple possible conditions for a single variable.
switch (value) {
case 0 {
// Handle case where value is 0
}
case 1 {
// Handle case where value is 1
}
case 2 {
// Handle case where value is 2
}
}
// Is equivalent to:
if (value == 0) {
// Handle case where value is 0
} else if (value == 1) {
// Handle case where value is 1
} else if (value == 2) {
// Handle case where value is 2
}
It's important to note that cases are not fall-through. That is, the switch
statement will only execute the first case that matches. If you want to execute multiple cases, you can evaluate multiple cases in a single block:
switch (value) {
case 0, 1, 2 {
// Handle case where value is 0, 1, or 2
}
case 3, 4, 5 {
// Handle case where value is 3, 4, or 5
}
}
You can also provide a default
case which will be executed if no other case matches:
switch (value) {
case 0 {
// Handle case where value is 0
}
case 1 {
// Handle case where value is 1
}
case 2 {
// Handle case where value is 2
}
default {
// Handle all other cases
}
}
Regardless of whether or not a default
case is provided, execution will continue after the switch
statement per normal. If a default
case is not provided, and no other cases match, the switch
statement will do nothing.
Looping Statements
A looping statement offers a quick and easy way to do something repeatedly. In this section, we'll introduce the different looping statements available in Ghost.
While Statement
A while
statement executes its block as long as a specified condition evaluates to true
. A while
statement looks like the following:
while (condition) {
// Do something
}
The condition test occurs before the block is executed. If the condition returns true
, the block is executed and the condition is tested again. If the condition returns false
, execution stops, and control is passed to the statement following while
.
The following while
loop iterates as long as n
is less than 3
:
n = 0
x = 0
while (n < 3) {
n = n + 1
x += n
}
With each iteration, the loop increments n
and adds that value to x
. Therefore, x
and n
take on the following values:
- After the first pass:
n
=1
andx
=1
- After the second pass:
n
=2
andx
=3
- After the third pass:
n
=3
andx
=6
After completing the third pass, the condition n < 3
is no longer true
, so the loop terminates.
Generally you'll want to avoid infinite loops (there are very few cases where an infinite loops are utilized, such as in game development). These are loops where the condition never evaluates to false
. The following example will loop forever because the condition is always true
:
while (true) {
print("Hello, world!")
}
For Statement
A for
loop repeats until a specified condition evaluates to false
. The Ghost for
loop is similar to the C loop for those familiar.
A for
statement looks like the following:
for (initializer; condition; increment) {
// Do something
}
When a for
loop executes, the following occurs:
- The initializing expression is executed. This expression usually initializes one or more loop counters. This expression can declare variables.
- The condition expression is evaluated. If the value of condition is
true
, the loop block is executed. If the value of condition isfalse
, thefor
loop terminates. - The block executes.
- The increment expression is executed.
- Control returns to step 2 until the condition expression evaluates to
false
.
Break Statement
The break
statement terminates the current loop statement and transfers program control back to the state following the terminated statement.
A break
statement cannot be used at the top level of a script, module, function, or class.
As an example, the following function has a break
statement that terminates the while
loop when i
is 3
, and then returns the value 3 * x
from the function:
function testBreak(x) {
i = 0
while (i < 6) {
if (i == 3) {
break
}
i + i + 1
}
return i * x
}
Continue Statement
The continue
statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
Much like break
, a continue
statement cannot be used at the top level of a script, module, function, or class.
As an example, the following example shows a while
loop that has a continue
statement that executes when the value of i
is 3
. Thus, n
takes on the values 1
, 3
, 7
, and 12
.
i = 0
n = 0
while (i < 5) {
i = i + 1
if (i == 3) {
continue
}
n = n + 1
}