Conditions
Up to now, the code we have encountered had a strictly linear flow: every statement was executed after the previous one and before the following. But often we want to either skip one or more statements under certain conditions or to fork the process flow into two alternative ways. For this we need the concept of conditions. These are script commands that tell the trCAD system to execute a section of code only if a certain condition is fulfilled or to skip it or use an alternative code section, if the condition is not fulfilled.
This section explains the following types of conditions:
if and if-else Conditions
The if statement is used if a part of code should only be executed under a certain condition. Its basic sequence is if( condition ) statement. The condition can be any boolean value. If the condition has the value true, the statement is executed. If the condition has the value false, nothing happens and the code execution continues after the if command.
Example
bool b = true
if( b )
echo( "b must be true to see this text" )
Output
b must be true to see this text
The if statement can be transformed from its "do-or-do-not" behavior into a "do-either-this-or-that" type by using the if-else statement: if( condition ) statement1 else statement2. statement1 will be executed if the condition is found to be true and statement2 if it is false:
Example
bool b = true
if( b )
echo( "b must be true to see this text" )
else
echo( "b must be false to see this text" )
Output
b must be true to see this text
Since an if command is a statement by itself, it can as well be used recursively:
Example
bool b1 = true
bool b2 = false
if( b1 )
if( b2 )
echo( "b1 and b2 must be true to see this text" )
else
echo( "b1 must be true and b2 false to see this text" )
else
echo( "b1 must be false to see this text" )
Output
b1 must be true and b2 false to see this text
switch Conditions
A common task during programming is the selective execution of one of more code blocks. An expression, e.g. a variable is given whose value decides, which code block shall be executed. This task could be accomplished by nested if statements, but this may lead to code that is difficult to read. A more comfortable way is the use of a switch statement:
Example
int i = 0
switch( i )
{
case 0:
echo( "i must be 0 to see this text" );
case 1:
echo( "i must be 1 to see this text" );
case 2:
echo( "i must be 2 to see this text" );
}
Output
i must be 0 to see this text
In a switch block only the case block will be executed whose value matches the argument of the switch keyword. In the above example only the first of the three case blocks is executed. There can be arbitrarily many case sections with distinct values inside one switch statement. If no case value matches the expression, the switch statement quits without doing anything. This behavior can be modified by adding a default section:
Example
int i = 15
switch( i )
{
case 0:
echo( "i must be 0 to see this text" );
case 1:
echo( "i must be 1 to see this text" );
case 2:
echo( "i must be 2 to see this text" );
default:
echo( "i is neither 0, 1 or 2 to see this text");
}
Output
i is neither 0, 1 or 2 to see this text
The code in the default section is executed if none of the case values match the switch argument. Please note that there can be only one default section inside a switch statement.
The statements of the different case and default sections can be a single statement or a list of statements. Sometimes it is desirable to break off the code execution inside such list of statements. This can be achieved with the break command:
Example
int i = 1
switch( i )
{
case 1:
echo( "i is 1" );
if( i % 2 == 1 )
break;
echo( "i is an even number" );
}
Output
i is 1
In the example, the break command breaks off the execution of the statements in the case section before the claim that 1 be an even number.
Note
Unlike in other languages of the C family, the default behavior of the switch condition is to exit the condition after one case or the default section was executed. Therefore it is not required to add break keywords at the end of case or default statement blocks to prohibit the execution of the following sections.
However a fallthrough behavior can be achieved with the fallthrough keyword at the end of a case statement. This causes the execution of the following case section (or default section if fallthrough is placed at the final case):
Example
int i = 1
switch( i )
{
case 0:
echo( "i must be 0 to see this text" );
fallthrough
case 1:
echo( "i must be 0 or 1 to see this text" );
fallthrough
case 2:
echo( "i must be 0, 1 or 2 to see this text" );
fallthrough
default:
echo( "this text is always visible");
}
Output
i must be 0 or 1 to see this text
i must be 0, 1 or 2 to see this text
this text is always visible
This is often used to make a block execute in more than one cases:
Example
int i = 0
switch( i )
{
case 0: fallthrough
case 1:
echo( "i must be 0 or 1 to see this text" );
case 2:
echo( "i must be 2 to see this text" );
}
Output
i must be 0 or 1 to see this text
Note that the fallthrough keyword can only be placed after the last statement of a case section.