...
Tip |
---|
Tip To make conditional statements easy to debug, group the commands to be executed under the IF statement. |
IF statements
IF statements
The IF statement works as follows: if the test condition is true, the statement is executed.
...
Code Block |
---|
|
if ( 2 < 3 )
{
string $blah = "This worked";
print ($blah);
} |
...
ELSE statements
The ELSE statement always follows the IF statement. The ELSE statement is executed when the preceding IF statement is false.
...
Code Block |
---|
|
if (5 % 2 >1)
{
print "bah";
}
else
{
print "humbug";
} |
ELSE IF statements
ELSE IF statements
The ELSE IF statement is always paired with the IF conditional statement. Unlike the else command which executes a statement when the IF statement is false, the ELSE IF statement requires another test condition to be true, or it will not execute either statement.
...
Code Block |
---|
|
if (-1 >= 1)
{
print "Statement 1";
}
else if (-1 <= 1)
{
print "Statement 2";
} |
...
SWITCH statements
The switch statement is a conditional statement that executes a code block depending on which one of a number of given cases is matched to the switch expression.
...
Tip |
---|
Tip You can use Interruption statements to affect the flow control in looping statements. |
...
WHILE statements
A WHILE looping statement continuously executes designated statements as long as a specific condition remains true.
...
Code Block |
---|
|
int $PJ = 5;
while ($PJ > 0)
{
print
("There are " + string($PJ) + " P.J. sandwiches left.");
$PJ = $PJ - 1;
}
print ("Houston, we have a problem!"); |
DO WHILE statements
DO WHILE statements
A DO WHILE looping statement executes a specific statement then checks whether or not to repeat the loop. This ensures that the statement is executed at least once.
...
Code Block |
---|
|
int $peanuts = 1;
do {
print ("Elephants work for peanuts");
$peanuts += 1;
print ($peanuts);
}
while ( $peanuts < 10); |
FOR statements
FOR statements
A FOR looping statement provides loop control that has initialization, test, and an increment statement.
...