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"
MouseWait
End
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
Command1
Command2
...
CommandN
[Else
AltCommand1
AltCommand2
...
AltCommandN ]
EndIf

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?"
name$="Frank"
NPrint "There, that's better."
Else
NPrint "That's a fine name."
NPrint "(Well, maybe)"
EndIf
NPrint "And your fixed name is ",name$
MouseWait
End


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