Comparisons - Making Decisions with IF Statements

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,
  
  Print "Hello"
  Print "Welcome to PlayBASIC"
  Sync
  WaitKey
  


      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.

Top





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

  
; Simple Examples ...  The conditions of the expressions here
;  will be all be TRUE, so the code after Then WILL BE executed.
  
  If  100 = 100 Then Print "Yes 100 does equal 100"
  
  If  50 > 10 Then Print " Yes 50 is bigger than 10"
  
  If  (100 * 5> 200 Then Print " Yes 100*5  is bigger than 200"
  
  Score= 100
  If Score > 50 Then Print "Score is bigger than 50"
  
  Lives =3
  If Lives >0 Then Print "Player Still Has some Lives Left"
  
  
; Simple Examples ...  The conditions of the expressions here
;  will be all be FALSE, so the code after Then is ignored.
  
  Score= 100
  If Score > 1000 Then Print "Score is bigger than 1000"
  Lives =3
  If Lives =0 Then Print "Player has no more Lives left"
  
  Sync
  WaitKey
  



Not sure what some of these symbols mean ?
Then check out the operators tutorial
.


Top






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

  
  lives=10
  If  Lives> 0
     Print "Play is not dead"
     Print "Keep on playing !"
  EndIf
  Sync
  WaitKey
  


Top






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

  
  Lives = 3
  If Lives > 0
     Print "Player Still has some lives left !"
  Else
     Print "Player is DEAD"
  EndIf
  Print "Done"
  Sync
  WaitKey
  


     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.

  
  Player Still has some lives left !
  Done
  


Top








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

  
  Weapon = 2
  If Weapon =1
     Print "Player is using the Small Gun"
  Else
     If Weapon =2
        Print "Player is using the Big Gun"
     Else
        If Weapon =3
           Print "Player is using the Massive Gun"
        EndIf
     EndIf
  EndIf
  Print "Done"
  Sync
  WaitKey
  


      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.

  
  Player is using the Big Gun
  Done
  


      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


  
  Weapon = 2
  If Weapon =1
     Print "Player is using the Small Gun"
  ElseIf Weapon =2
     Print "Player is using the Big Gun"
  ElseIf Weapon =3
     Print "Player is using the Massive Gun"
  EndIf
  Print "Done"
  
  Sync
  WaitKey
  


      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.


  
  Player is using the Big Gun
  Done
  


Top







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

  
  myInt = 1
; myInt = 2
; myInt = 3
  Select myInt
      Case 1
          Print "The first case"
      Case 2
          Print "The second case"
  EndSelect
  Sync
  WaitKey
  


      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:

  
; myInt = 1
; myInt = 2
  myInt = 3
  Select myInt
      Case 1
          Print "The first case"
      Case 2
          Print "The second case"
      Default
          Print "myInt is neither 1 nor 2"
  EndSelect
  Sync
  WaitKey
  


     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.
  
  myInt = 1
; myInt = 42
  Select myInt
      Case 1 To 2
          Print "myInt is either 1 or 2"
      Case 10 To 100
          Print "myInt is greater than 9"
          Print "myInt is smaller than 101"
      Default
          Print "The value myInt is neither 1 nor 2"
          Print "nor between 10 or 100"
  EndSelect
  Sync
  WaitKey
  


     If certain conditions apply to a number of unrelated values, you can list these values after the case keyword, separated by commas:
  
  myInt = 42
  Select myInt
      Case 144278
          Print "It's either 14 42 or 78"
      Case 1 To 10
          Print "It's between 1 and 10"
      Default
          Print "Neither of the above"
  EndSelect
  Sync
  WaitKey
  


     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:
  
; myInt = 14
  myInt = 42
; myInt = 178
  Select myInt
      Case 1442178
          Print "It's either 14, 42 or 178"
      ContCase
      Case 1 To 100
          Print "It's between 1 and 100"
  EndSelect
  Sync
  WaitKey
  




     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.

Top




 
Related Info: ArrayBasics | Else | ElseIf | EndIf | Functions&Psub | If | Loops | Operators | Select | Then | Variables :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com