Phantom Documentation

[Help Home] [Phantom Home]
if-else

Syntax:

if(Var1 OP Var2)
{
  Statement1;
  Statement2;
  ....
}
else
{
  Statement3;
  Statement4;
  ....
}

Where,

Var1 - The first variable to compare
OP - The conditional operator
Var2 - The second variable to compare

Description:
Processes the statements within the brackets if the conditional statement is true.  Does not process them if the statement is false.  The comparison is only done once and exits out of the if statement once the statements in the brackets have been processed.

An 'else' statement can optionally be used to execute statements if the conditional operation does not yield a true result. If-else statements can be nested, and an else can be followed directly by an if, while, or for statement. Like the basic else statement, these statements will only be evaluated if all previous if statements in the set fail the conditional check

Example Code

int i = 10;
bool b = false;

# Simple if statement
if(i == 10){
  disp("i is 10");
}

# An if statement followed by an else
if(i > 11){
  disp("Should not get here!");
}else{
  disp("i is not greater than 11");
}

# An if statement followed by an else if,
# and an else
if(i < 10){
  disp("Should not get here!");
}else if(i == 10){
  disp("i is 10");
}else{
  disp("Should not get here!");
}

# An if statement followed by a for
if(b){
  disp("Should not get here!");
}else for(i = 0; i < 3; i++){
  disp("i is " + string(i));
}

# An if statement followed by a while
if(b){
  disp("Should not get here!");
}else while(i > 0){
  disp("i is " + string(i));
  i = i - 1;
}



The output from the above example is as follows

Output
i is 10
i is not greater than 11
i is 10
i is 0
i is 1
i is 2
i is 3
i is 2
i is 1


See Also: while, Conditional Statements, for



Copyright 2000-2011 Phantom Automated Solutions