Conditionally executes a sequence of commands terminated by an END command. In its simplest form, the IF command looks like this:
IF logicalExpression 'commands to be executed 'if logicalExpression is true END
If the logical expression evaluates to true, then the command sequence will be executed. Otherwise, the command sequence will be skipped. See examples at right.
The logical expression may be a simple test, such as a <= 3. Or it may be a more complex expression such as a <= 3 AND b between 1 10. In general, a logical expression is a simple test or a number of simple tests combined using the logical operators NOT, AND, OR, or XOR. For a full explanation of logical expressions, see the section titled "Logical Expressions" in the latter part of Statistics101 Resampling Stats commands.
The IF command can also have an ELSE clause that is executed if the IF command's test fails. The structure of the IF command with an ELSE clause is:
IF logicalExpression 'commands to be executed 'if logicalExpression is true ELSE 'commands executed 'if logicalExpression is false END
The ELSE clause is optional. The ELSE command must be on its own line as must all commands. It has no arguments. The second example at the right illustrates the use of the ELSE command.
The most general version of IF includes one or more ELSEIF commands, each with its own logical expression, and an optional ELSE command. The first IF or ELSEIF whose logical expression evaluates to true will execute. If none of these expressions pass, then the commands controlled by the ELSE command will execute. The general structure of the IF command is:
IF logicalExpression1 'commands to execute if 'logicalExpression1 passes. ELSEIF logicalExpression2 'commands to execute if 'logicalExpression1 failed but 'logicalExpression2 passes. ELSEIF logicalExpression3 'commands to execute if logicalExpression1 'and logicalExpression2 failed but logicalExpression3 passes. ELSEIF logicalExpression4 . . . ELSE 'commands to execute if 'all the above logical expressions failed END
'Suppose you toss two coins. What is the 'probability that they will match? COPY (0 1) coin COPY 10000 numberOfTrials REPEAT numberOfTrials GENERATE 1 coin firstFlip GENERATE 1 coin secondFlip IF firstFlip = secondFlip score 1 bothEqual END END SUM bothEqual numberSuccessfulTrials DIVIDE numberSuccessfulTrials numberOfTrials probabilityOfMatch PRINT probabilityOfMatch
One run of the above program produced the following output:
Here's an example using the ELSE command:
'What is the probability of dealing a hand containing 'two clubs or three hearts but not both? ' NAME hearts clubs diamonds spades COPY 13#hearts 13#clubs 13#diamonds 13#spades deck NAME 100000 rptCount COPY 0 successCount REPEAT rptCount SAMPLE 5 deck hand COUNT hand = clubs clubCount COUNT hand = hearts heartCount IF clubCount = 2 'Equivalent to "two clubs OR IF heartCount <> 3 'three hearts BUT NOT BOTH" ADD 1 successCount successCount END ELSE IF heartCount = 3 ADD 1 successCount successCount END END END DIVIDE successCount rptCount probability PRINT probability
Here is the same program rewritten using a logical expression to simplify the logic and avoid the nested IF statements.
'What is the probability of dealing a hand containing 'two clubs or three hearts but not both? ' NAME hearts clubs diamonds spades COPY 13#hearts 13#clubs 13#diamonds 13#spades deck NAME 100000 rptCount COPY 0 successCount REPEAT rptCount SAMPLE 5 deck hand COUNT hand = clubs clubCount COUNT hand = hearts heartCount IF clubCount = 2 XOR heartCount = 3 ADD 1 successCount successCount END END DIVIDE successCount rptCount probability PRINT probability