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 statement block will be executed whose value matches the argument of the switch keyword. In the example only the first of the three case statement 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 matches 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 desireable to break off the execution of code inside such list of statements. This can be achieved by the break command, like in the following code example:

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

The break command breaks off the execution of the case statement list before the program can claim that 1 is an even number.

Note

Unlike in other languages of the C family, it is not required to add a break keyword at the end of each case or default statement block. Only the statement list of the first matching case section is executed.