If condition [Then]
Parameters
|
condition - any expression; zero counts as false, anything else as true Then (optional) - required only for the single-line form |
Description
|
Runs a block of code only when a condition is true. If is the decision-maker of the language, and it comes in two shapes. The block form runs everything up to its EndIf: If hull >= 75 Print "All systems go" EndIf The single-line form puts the statement on the same line after Then, and needs no EndIf at all: If shield = 0 Then Print "Shields down!". It is perfect for one-liners and clutters the code far less than a three-line block. Add Else for the other case, and ElseIf to test further conditions before falling through. Only the first branch whose condition is true runs; the rest are skipped. Conditions are just numbers underneath. Zero is false, anything else is true, so If lives works as a shorthand for "lives is not zero" and If Not Null-checking works the same way. Join tests with And and Or, and invert with Not. One thing to remember: And and Or do not short-circuit, so both sides of a joined condition always run. And once you find yourself four ElseIf levels deep, a Select block is usually easier to read. See also: Then, Else, ElseIf, EndIf, Select, True. |
Example
; If Example ; ---------- ; The pilot's status after the last battle hull=65 shields=0 ; If runs the block below only when its condition is true If hull>=75 Then Print "Hull at "+hull+"% - all systems go!" ElseIf hull>=40 Then Print "Hull at "+hull+"% - fly carefully out there." Else Print "Hull at "+hull+"% - return to base immediately!" End If ; If also has a single-line form that needs no End If If shields=0 Then Print "Warning: shields are down!" Print "" Print "Press any key to close the example" WaitKey End
Index