Statements
All functions are made up of a list of statements enclosed with left and right braces, which in and of itself is a statement called a compound statement, or block.
- Statement
Compound Statements
Note that the statement list is zero or more, so an empty compound
statement {}
is entirely valid.
- CompoundStatement
-
{
Statement*}
Expression Statements
An expression statement is the single most common type of statement in just about any programming language. In ZScript, exactly like C and C++, an expression statement is simply formed with any expression followed by a semicolon. Function calls and variable assignments are expressions, for instance, so it is quite clear why they are common.
- ExpressionStatement
Conditional Statements
A conditional statement will, conditionally, choose a statement (or none) to execute. They work the same as in C, C++, and ACS.
- ConditionalStatement
-
if
(
Expression)
Statement (else
Statement)?
Switch Statements
A switch statement takes an expression of integer or name type and selects a labeled statement to run. They work the same as in C, C++, and ACS.
- SwitchStatement
-
switch
(
Expression)
Statement
Loop Statements
ZScript has five loop statements: for
, while
, until
, do while
and do until
. for
, while
and do while
work the same as in C,
C++ and ACS, and until
and do until
do the inverse of while
and
do while
.
- LoopStatement
For Loop Statements
The for
loop takes a limited statement and two optional expressions:
The statement for when the loop begins (which is scoped to the loop,)
one expression for checking if the loop should break, and one which is
executed every time the loop iterates.
- ForLoopStatement
-
for
(
ForLoopInitializer?;
ForLoopUpdate?;
Expression?)
Statement - ForLoopInitializer
- ForLoopUpdate
-
Expression (
,
Expression)*
While Loop Statements
The while
loop simply takes one expression for checking if the loop
should break, equivalent to for(; x;)
.
The until
loop is equivalent to while(!x)
.
- WhileLoopStatement
-
while
(
Expression)
Statementuntil
(
Expression)
Statement
Do While Loop Statements
do while
and do until
will only check the expression after the
first iteration is complete. Unlike C, you don't need a semicolon
after one.
- DoWhileLoopStatement
-
do
Statementwhile
(
Expression)
do
Statementuntil
(
Expression)
Flow Statements
As in C, there are three control flow statements that manipulate where
the program will execute statements next, which are available
contextually. They are continue
, break
and return
.
- FlowStatement
Continue Flow Statements
continue
is available in loop statements and will continue to the
next iteration immediately.
- ContinueFlowStatement
-
continue
;
Break Flow Statements
break
is available in loop statements and switch statements, and
will break out of the containing statement early.
- BreakFlowStatement
-
break
;
Return Flow Statements
return
is available in functions. If the function does not return
any values, it may have no expressions, and will simply exit the
function early. If the function does return values, it takes a
comma-separated list for each expression returned.
- ReturnFlowStatement
-
return
(Expression (,
Expression)*)?;
Local Variable Statements
Local variable statements define one or more variables in the current scope.
- LocalVariableStatement
-
Type LocalVariableInitializer (
,
LocalVariableInitializer)*;
- LocalVariableInitializer
Multi-Assignment Statements
Expressions or functions that return multiple values can be assigned into multiple variables.
- MultiAssignmentStatement
-
[
Expression (,
Expression)*]
=
Expression;
Null Statements
A null statement does nothing. It is similar to an empty compound statement.
- NullStatement
-
;
Examples
Expression Statements
// Some basic expressions.
MyCoolFunction(5, 4);
m_MyCoolMember = 500;
// Does nothing, of course, but valid.
5 * 5;
Conditional Statements
// Simple conditional.
if(a)
B();
// Simple conditional, with else statement and a block.
if(a)
{
B();
c = d;
}
else
e = f;
Switch Statements
// A switch demonstrating fall-through and default cases.
switch(a)
{
case 500:
Console.PrintF("a is 500");
break;
case 501:
Console.PrintF("a is 501");
// Falls through to the next case.
case 502:
Console.PrintF("a is 501 or 502");
break;
default:
Console.PrintF("not sure what a is!");
// "break" is implied here.
}
Flow Statements
// Use of "continue."
for(int i = 0; i < 50; i++)
{
// Don't do anything when "i" is 25.
if(i == 25)
continue;
DoThing(i);
}
// Use of "break."
for(int i = 0; i < 50; i++)
{
// "break" when "i" is 25.
if(i == 25)
break;
DoThing(i);
}
// Use of `return` in various contexts.
void ReturnsNothing()
{
// Exit early if "m_Thing" isn't 50.
if(m_Thing != 50)
return;
DoThing(m_Thing);
}
int ReturnsInt()
{
// "m_Thing" is 50, so return 50.
if(m_Thing == 50)
return 50;
// Must have a return, eventually.
return 0;
}
int, int ReturnsTwoInts()
{
// Returns 1 and 2.
return 1, 2;
}
Multi-Assignment Statements
// Getting the actor out of "A_SpawnItemEx."
Actor mo; bool spawned; [spawned, mo] = A_SpawnItemEx("MyCoolActor");