Blitz:Decision Making

From Amiga Coding
Revision as of 18:01, 4 August 2015 by Daedalus (talk | contribs) (Created page with "For anything but the most simple programs, decisions will have to be made by your code. There are numerous ways of doing this, and all are similar to other BASICs and generall...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

For anything but the most simple programs, decisions will have to be made by your code. There are numerous ways of doing this, and all are similar to other BASICs and generally have equivalents across most languages. For all decisions, standard Blitz operators can be used for the comparisons, or direct numeric evaluation can be carried out without a comparison. In these cases, any numeric value other than 0 counts as true, while 0 counts as false.


If/Then/Else

The most straightforward decision-making command, If simply carries out a comparison and executes some code if the result of the comparison is true. The format is: If <expression> Then <Code to execute> When <expression> results in anything other than 0/False, the will be executed. If the result is 0/False, the program flow skips onto the next line. For example: <code>If x > 5 Then NPrint "Score is greater than 5!"

If more than a short piece of code is required to be executed by the If statement, it can be expanded by leaving out the Then keyword and instead finishing the block of code with End If. For example: If x > 5

 NPrint "Score is greater than 5!"
 x = 0
 Gosub gamewon

End If In this example, all three lines following the If statement are executed only if x is greater than 5. It is good practice in cases like this to indent your code as shown to make it easier to see what code is affected by the comparison.

Else

Else can be added to an If statement in order to provide code to be executed if the expression is not true. For example: