Conditional statements

Show Index

So now that we know how to print data to the screen and how to assign data to variables, let's look at how to handle conditional statements.

The most common conditional statement is the If command. It is usually followed by the Then statement or the EndIf statement depending on how it is used.

An example of its usage could be:

If answer = 5 Then Print "The answer is 5."

or an alternative form of the same code could be:

If answer = 5
  Print "The answer is 5."
EndIf

This command compares two 'arguments'.  An argument can be a variable (like 'answer' in the example above) or a constant value (like '5' in the example above) or an 'expression' (like 2+3  or "abc"+"def").  This is a very powerful command and is what allows your computer to 'think'.

A variation of the If command is the If-Else-EndIf sequence of commands.   Here is an example:

If answer = 5
  Print "The answer is 5."
Else
  Print "The answer is not 5."
EndIf

If you ran the code and the variable answer actually does contain the value of 5 then the output would be this:

The answer is 5.

If you ran the code and the variable answer did NOT contain the value of 5 (i.e. ANY number but 5) then the output would be this:

The answer is not 5.

 

If statements can also compare string variables as well. (If you don't remember what a 'string' variable is, then feel free to reread the variables lesson)

If name$ = "John"
  Print "Hello John."
  Print "Nice to meet you again."
Else
  Print "Hello " + name$
  Print "I haven't met you before."
EndIf

Did you notice in the example above that it is OK to use more than one statement following the If command or Else command?

Did you also notice that in each example above, I indented the lines that were part of the If-EndIf or If-Else-EndIf statements?  This is not necessary, but I like to write my code that way so that it is easier to read.  You can very quickly see which parts of the code are only executed if the If statement is true.  Indenting code is good programming practice, and I urge you to get into the habit of it as well.

 

Another form of conditional statement is the Select statement.  It's usage is like this:

Select answer
  Case 10
    Print "The answer is 10."
  Case 20
    Print "The answer is 20."
  Case 30
    Print "The answer is 30."
End Select

The Select statement compares its argument against each Case statements argument.  So for example, if the variable answer contained the value of 30, the computer would check it against the first Case statement (Case 10).  Since it wouldn't match, it would then check it against the second Case statement (Case 20).  Since it wouldn't match, it would then check it against the third and final Case statement (Case 30).  At the third Case statement, it would finally find a match (because answer = 30) and execute the Print statement immediately following the Case 30 statement (Print "The answer is 30.").

Note that once a Select statement executes one of its branches, it will jump forward to the End Select statement and continue execution from there.  So, to clarify, if in our previous example, the variable answer contained the value of 20, then the second Case statement (Case 20) would equate to true and the Print statement immediately following the Case 20 statement (Print "The answer is 20.") would be executed.  THEN, the program would jump forward to the End Select statement and continue execution with whatever line follows End Select.

 

Continue on to Our first game
Goto next lesson

 

If you've reached this page and there's no index on the left, Click here to show the Index Page