If...Then...Else...Endif decisions

As mentioned in the introduction, If commands can be added to your code to make your program execute different code based on different values of expressions. It allows you to either execute one sequence of commands on occaision, or to select one of two possible sequences of commands. There are two basic forms that the command can take. The first is:
If Expression Then Command [ Else Command ]

In this case, if the value of the Expression evaluates to true, the command following the Then will be performed. However, if the expression evaluates to false, the command after the Else will be performed (if it is included - this part is optional).

Code age.w = 21
If age>40 Then NPrint "It's all downhill frm here ;)" Else NPrint "You have lots to look forward to - life begins at 40"
It is even possible to use more If statements in place of the commands which come after the Then or Else keywords.

The second style of using the If statement is as follows:

If Expression
AltCommandN ]

Notice there is no Then keyword in this mode. It is also possible to use "End If" (with a space). Similar to the first mode, if the value of the expression is true then the first set of commands will be performed. If the value of the expression is false, then the alternate set of commands are carried out. The Else keyword and alternate commands are optional again. Here is an example of using the If structure in the second way:

Code name$="John"
If name$="Bob"
NPrint "Hey Bob, bit of a doofus name, how's about we change it?"
NPrint "There, that's better."
NPrint "That's a fine name."
NPrint "(Well, maybe)"
NPrint "And your fixed name is ",name$

Previous: Introduction to program flow Programming contents Next: Select...End Select decisions

News | Introduction | Search | List/Forum/IRC Webring | Contact
Installation | Troubleshooting | Programming | Archives | Links
Return to programming main
Page last modified: 17th March 2002