Difference between revisions of "Blitz:Decision Making"

From Amiga Coding
Jump to: navigation, search
 
Line 3: Line 3:
 
==If/Then/Else==
 
==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:
 
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:
<code>If <expression> Then <command(s) to execute></code>
+
<pre>If <expression> Then <command(s) to execute></pre>
 
When <expression> results in anything other than 0/False, the <command(s) to execute> will be executed. If the result is 0/False, the program flow skips onto the next line. For example:
 
When <expression> results in anything other than 0/False, the <command(s) to execute> 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!"</code>
+
<pre>If x > 5 Then NPrint "Score is greater than 5!"</pre>
  
 
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 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:
<code>If x > 5
+
<pre>If x > 5
 
   NPrint "Score is greater than 5!"
 
   NPrint "Score is greater than 5!"
 
   x = 0
 
   x = 0
 
   Gosub gamewon
 
   Gosub gamewon
End If</code>
+
End If</pre>
 
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.
 
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===
 
Else can be added to an If statement in order to provide code to be executed if the expression is ''not'' true. For example:
 
Else can be added to an If statement in order to provide code to be executed if the expression is ''not'' true. For example:
<code>If x > 5
+
<pre>If x > 5
 
   NPrint "Score is greater than 5!"
 
   NPrint "Score is greater than 5!"
 
   x = 0
 
   x = 0
Line 23: Line 23:
 
Else
 
Else
 
   NPrint "Score isn't high enough!"
 
   NPrint "Score isn't high enough!"
End If</code>
+
End If</pre>
 
In this example, if x is greater than 5, the message "Score is greater than 5!" will be displayed, x will be set to 0 and the code will jump to the gamewon subroutine.
 
In this example, if x is greater than 5, the message "Score is greater than 5!" will be displayed, x will be set to 0 and the code will jump to the gamewon subroutine.
 
If x is ''not'' greater than 5 (i.e., any value less than or equal to 5), the message "Score isn't high enough!" will be printed instead. Only one of the two code segments will be executed, even if the value of the comparison is changed during the execution of the conditional code. This is because the comparison has already been calculated and is not re-checked when the Else statement is reached.
 
If x is ''not'' greater than 5 (i.e., any value less than or equal to 5), the message "Score isn't high enough!" will be printed instead. Only one of the two code segments will be executed, even if the value of the comparison is changed during the execution of the conditional code. This is because the comparison has already been calculated and is not re-checked when the Else statement is reached.
  
 
Else can also be used in the single line version of the If statement:
 
Else can also be used in the single line version of the If statement:
<code>If x > 5 Then NPrint "Score over 5!" Else NPrint "Score not over 5!"</code>
+
<pre>If x > 5 Then NPrint "Score over 5!" Else NPrint "Score not over 5!"</pre>
  
 
==Select/Case==
 
==Select/Case==
 
For situations where choosing between multiple code segments might be required instead of just two as in the If/Then/Else blocks, the Select and Case statements can be used. These statements take a variable and execute a particular code segment if the variable contains a particular value. Any number of different code segments are possible where each segment is executed only if the variable contains the value associated with that segment. The Select statement specifies the variable to compare, a Case statement starts each block with the value to be checked for, and End Select finishes the Select block and returns the program to normal flow. For example:
 
For situations where choosing between multiple code segments might be required instead of just two as in the If/Then/Else blocks, the Select and Case statements can be used. These statements take a variable and execute a particular code segment if the variable contains a particular value. Any number of different code segments are possible where each segment is executed only if the variable contains the value associated with that segment. The Select statement specifies the variable to compare, a Case statement starts each block with the value to be checked for, and End Select finishes the Select block and returns the program to normal flow. For example:
<code>Select level
+
<pre>Select level
 
   Case 0
 
   Case 0
 
     NPrint "Level 0"
 
     NPrint "Level 0"
Line 41: Line 41:
 
     NPrint "Final Level!"
 
     NPrint "Final Level!"
 
     Gosub lastlevel
 
     Gosub lastlevel
End Select</code>
+
End Select</pre>
 
In this example, the "Level 0" message is printed and the resetgame subroutine is executed if level is 0, the "Level 1" message is printed if level is 1, and the "Final Level!" message is printed and the lastlevel subroutine is executed if level is 5. Note that no other block is executed once one Case statement matches the variable's contents. Once the selected block is completed, program flow continues after the End Select statement.
 
In this example, the "Level 0" message is printed and the resetgame subroutine is executed if level is 0, the "Level 1" message is printed if level is 1, and the "Final Level!" message is printed and the lastlevel subroutine is executed if level is 5. Note that no other block is executed once one Case statement matches the variable's contents. Once the selected block is completed, program flow continues after the End Select statement.
  
Line 48: Line 48:
 
===Default===
 
===Default===
 
Note also that in the example above, if level doesn't contain 0, 1 or 5, none of the blocks are executed at all, and the program jumps directly to the statement following the End Select statement. An optional Default statement can be used at the end of the Select block to provide a special block of code to execute if none of the Case statements have been satisfied. For example:
 
Note also that in the example above, if level doesn't contain 0, 1 or 5, none of the blocks are executed at all, and the program jumps directly to the statement following the End Select statement. An optional Default statement can be used at the end of the Select block to provide a special block of code to execute if none of the Case statements have been satisfied. For example:
<code>Select level
+
<pre>Select level
 
   Case 0
 
   Case 0
 
     NPrint "Level 0"
 
     NPrint "Level 0"
Line 59: Line 59:
 
   Default
 
   Default
 
     NPrint "Random Other Level!"
 
     NPrint "Random Other Level!"
End Select</code>
+
End Select</pre>
 
This example works similarly to the first example above, except that the message "Random Other Level!" is printed whenever the level variable contains a value other than 0, 1 or 5.
 
This example works similarly to the first example above, except that the message "Random Other Level!" is printed whenever the level variable contains a value other than 0, 1 or 5.
  
 
[[Category:Blitz Basic / AmiBlitz|Blitz]]
 
[[Category:Blitz Basic / AmiBlitz|Blitz]]

Latest revision as of 13:56, 25 January 2018

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 <command(s) to execute>

When <expression> results in anything other than 0/False, the <command(s) to execute> will be executed. If the result is 0/False, the program flow skips onto the next line. For example:

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:

If x > 5
  NPrint "Score is greater than 5!"
  x = 0
  Gosub gamewon
Else
  NPrint "Score isn't high enough!"
End If

In this example, if x is greater than 5, the message "Score is greater than 5!" will be displayed, x will be set to 0 and the code will jump to the gamewon subroutine. If x is not greater than 5 (i.e., any value less than or equal to 5), the message "Score isn't high enough!" will be printed instead. Only one of the two code segments will be executed, even if the value of the comparison is changed during the execution of the conditional code. This is because the comparison has already been calculated and is not re-checked when the Else statement is reached.

Else can also be used in the single line version of the If statement:

If x > 5 Then NPrint "Score over 5!" Else NPrint "Score not over 5!"

Select/Case

For situations where choosing between multiple code segments might be required instead of just two as in the If/Then/Else blocks, the Select and Case statements can be used. These statements take a variable and execute a particular code segment if the variable contains a particular value. Any number of different code segments are possible where each segment is executed only if the variable contains the value associated with that segment. The Select statement specifies the variable to compare, a Case statement starts each block with the value to be checked for, and End Select finishes the Select block and returns the program to normal flow. For example:

Select level
  Case 0
    NPrint "Level 0"
    Gosub resetgame
  Case 1
    NPrint "Level 1"
  Case 5
    NPrint "Final Level!"
    Gosub lastlevel
End Select

In this example, the "Level 0" message is printed and the resetgame subroutine is executed if level is 0, the "Level 1" message is printed if level is 1, and the "Final Level!" message is printed and the lastlevel subroutine is executed if level is 5. Note that no other block is executed once one Case statement matches the variable's contents. Once the selected block is completed, program flow continues after the End Select statement.

Select / Case / End Select is very similar to the Switch statement in C, however no break; statement is needed at the end of each Case block, instead, the next Case, Default or End Select statement marks the end of the current block.

Default

Note also that in the example above, if level doesn't contain 0, 1 or 5, none of the blocks are executed at all, and the program jumps directly to the statement following the End Select statement. An optional Default statement can be used at the end of the Select block to provide a special block of code to execute if none of the Case statements have been satisfied. For example:

Select level
  Case 0
    NPrint "Level 0"
    Gosub resetgame
  Case 1
    NPrint "Level 1"
  Case 5
    NPrint "Final Level!"
    Gosub lastlevel
  Default
    NPrint "Random Other Level!"
End Select

This example works similarly to the first example above, except that the message "Random Other Level!" is printed whenever the level variable contains a value other than 0, 1 or 5.