|
By: Kevin Picone I N D E X: About While learning to program, it's often common to think of our program code as just one big linear sequence of instructions. That's to say, we type in a our programs instructions in order, then the computer just follows these instructions from start to finish in the order in which they appear. Which woukld be from the top of the program to the bottom. For Example,
If we cut and paste this sample code into PlayBASIC and run it (Press F5) - PB will execute the four lines of code in order (top to bottom), so it will draw the two messages (see Print), display the screen (see Sync) and wait for a key to pressed (see WaitKey) before ending. Now thinking in a linear sequence might make learning easier initially, but what if we want our program to make decisions and selectively run some instructions and not others ? In BASIC we make decisions using the IF statement. It allows a program to execute a piece of code selectively. If is used in conjunction with either the Then statement, or the Else , ElseIf and EndIf statements. So when an IF statement block is executed in our program, PlayBASIC evaluates your comparison then chooses what code to run next. So we can select when a section of code is executed and when it's not. If / Then The pairing of the If/Then statements is the simplest form of single line decision possible. It allows us to check If a condition was true), then perform an action if it was. So when the If condition is True(1) the code following the Then statement is executed. However, when the condition is false(0), the code following the Then will be skipped over. Examples
Then check out the operators tutorial. If / EndIf The pairing of the If/EndIf statements is the multi line form of the If/Then code above. So this combination allows for many lines of code to be selectively executed when the If's condition expression is met. Apart from being able to be used across many lines, that's the only difference to the If/Then form above. Example
If / Else / EndIf Previously with the If / Then and If / EndIf combinations, we've only been able to selectively execute code when the If condition expression is TRUE. This is were Else allows us greater control. It allows us to choose the path our code takes, much like when driving a car at traffic intersection. So we can now choose between two TRUE/FALSE paths. So it's a little more complicated, but not too much! So logically, when the If condition is TRUE, the code between the If and Else statements will be executed. But when If the condition is FALSE, the code after the Else and between the Else-EndIf is executed. Example
This example would display (bellow), since the condition (lives > 0) is True. Notice how the code between the Else/EndIf is not executed. Only the code between the If/Else will be executed.
If / ElseIf / Else / EndIf Previously we've seen that the If / Else / EndIf combination, allows us to selectively execute code based upon whether the If condition is TRUE or FALSE. While If / Else / EndIf can be very powerful combination, sometimes we'll need even more control than it offer. For example when the IF expression is FALSE, we might need to perform another IF comparison to find the match we're after. Doing this with IF/Else/EndIF statements requires us to nest If / EndIF statements, inside other If statements. Which can get confusing to read. To demonstrate this, bellow i've nested three IF/Endif statements inside each other. The code is trying catch and execute the correct response depending upon what value is stored in the 'Weapon' Variable. Example
This example would output the following (bellow) when executed. Now since the Weapon variable equals 2, the first IF statement we hit will be FALSE as Weapon does no equal 1. Because it's False, execution will jump to the ELSE statement of the first IF. Here it will perform another IF comparison. This time the if statement will be TRUE as Weapon does indeed equal 2. Because the condition is met. Execution will run the code following that IF, then it'll jump out to that If statement to it's closing/matching EndIF statement. So the 3rd IF statement in this example, which has been nested inside the second If statement. Is Never even considered.
While that's a perfectly acceptable approach, It can be a tad confusing to follow. This is where the ElseIF statement can help make our code easier to read. ElseIf acts much like the Else statement, where if the opening If condition is FALSE, execution skips ahead to the ElseIF statement like we've expect. But ElseIf can also evaluate a conditional expression just like the If statement. This Allows us to test multiple the TRUE/ FALSE conditions, without having to nest If/EndIF statements. You can can have as many ElseIF statements in a row as you like. When your finished, you close the statement with a single EndIF Example
This example would output the following (bellow) when executed. Now since the 'Weapon' variable equals 2, when we hit the IF statement, the comparison will be FALSE since Weapon does not equal 1. Since it's False, execution will jump to the first ElseIF statement. Here it will perform another comparison, comparing Weapon with 2 this time. This statement will be TRUE, so the code following this ElseIF is executed. Once execution is complete, it will jump out of and over any following code to the closing EndIF statement. So just like the previous example the Second ElseIF statement Is Never even considered.
Select Case Select/Case statements are an alternative to If/Then statements when you have a large number of conditions to examine. The simplest form of a Select/Case block looks like this
If the variable myInt equals 1 this example will output The first case if myInt equals 2, it will output The second case. All other values of myInt will cause no output at all. In order to process all cases that are not explicitely stated with the Case keyword, you can use the keyword Default:
This example would output myInt is neither 1 nor 2 if the value of myInt is less than 1 or greater than 2. You can also check for value ranges with in the case statements by using the keyword To.
If certain conditions apply to a number of unrelated values, you can list these values after the case keyword, separated by commas:
By default PlayBASIC will exit a Select/EndSelect block as soon as a case match occurs. In order to force PlayBASIC to check further cases, use the ContCase statement:
Select/Case statements work with all standard types, ie. Integer, Float and String. Opposed to a lot of other Basic dialects, PlayBASIC also accepts expressions after the Case keyword. The expression followed by the Select keyword must be of the same type as the expressions or constants followed by the Case statements. Otherwise PlayBASIC will return an error. |
Related Info: | ArrayBasics | Else | ElseIf | EndIf | Functions&Psub | If | Loops | Operators | Select | Then | Variables : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |