Conditionals

Conditionals are a very important part of any program. Conditionals allow your program to think. With conditionals, any program can make choices and decisions. Before you can fully understand conditionals, however, you must first learn about the Blitz3D idea of truth and falsehood.

Truth and Falsehood

Blitz3D has a different idea about what is true and what is false than we humans do. To a human, some things may be partly true, but to a computer, any expression is either true or false. Although parts of an expression can be different from the rest, the entire expression is only evaluated as one or the other.

Blitz3D (and computers in general) believes that zero is false and any other value (nonzero value) is true, although the true value is usually one. This makes programming a much easier job.

To determine whether something is true or false, you use the relational and logical operators. These operators check one statement against another to see whether the aspect of their relationship that is being checked is true or false. Table 2.4 lists all of the relational and logical operators.

Table 2.4. Relational and Logical Operators
Operator 
Relational Operators 
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to
=Equal to
<>Not equal to
Logical Operators 
And 
Or 
Not 

Using Table 2.4 as a guide, you can see that if, say, variable A is equal to 14 and variable B is equal to 12, A>B will return True, because 14 is a larger number than 12.

If...Then

The first conditional you will learn is the If statement. The If statement has a very basic declaration:

If

The idea of an If statement is that it allows your program to make choices. You pass an expression into the If statement by following the If command with the expression:

If expression is true Then
;Do something
Endif

As you can see, the If statement is followed by an expression. If the expression is true, the code between the If and EndIf commands is executed. If not, nothing happens.

;demo02-05.bb - Tests if you are old enough to vote

;Find out how old the user is
age = Input$("How old are you? ")
;if older than or equal to 18, print out confirmation that user is allowed to vote.
If age >= 18 Then
     Print "You are legally allowed to vote!"
EndIf
;Wait five seconds
Delay 5000

This program simply asks how old you are, tests it against the age 18, and then prints "You are legally allowed to vote!" if you are 18 years or older. But what if you want to tell the user something else, even if they aren’t over 18? As you can see from the code, this program does nothing if the user is younger than 18. The program then waits for the user to press a key for the program to exit. Figure 2.8 shows the program running.

Figure 2.8. The demo02-05.bb program.


You may not understand what the EndIf command does. The EndIf command signifies the end of the If...Then test. When the program reaches the EndIf, it resumes normal processing of commands instead of only executing the commands when the condition tested in the If statement is met.

If...Then...Else

Perhaps you want the program to test if the user is younger than 18. You could rewrite the program by adding another If statement to check if the user is younger than 18, but there is another easier (and better) way: Use the Else statement.

;demo02-06.bb - Tests if you are old enough to vote

;Ask how old the user is
age = Input$("How old are you? ")

;if older than or equal to 18 then let them vote
If age >= 18 Then
     Print "You are legally allowed to vote!"

;if younger than 18, do not let them vote

Else Print "Sorry, you need To be a few years older."
EndIf

;Wait five seconds
Delay 5000

Figure 2.9 shows the output.

Figure 2.9. The demo02-06.bb program.


This time, the program tests the user’s age, but if it is less than 18, it prints out the sentence under the Else statement.

There is also one other effective use of the If...Else conditional. You can combine the two to create Else If.

;demo02-07.bb Tests if you are old enough to vote
age = Input$("How old are you? ")
If age = 18 Then
       Print "You can now vote."
Else If age > 18
      Print "You've been able to vote for a while."
Else If age ≤ 18
     Print "Sorry, you will have to wait a few years to vote."
EndIf
WaitKey

Figure 2.10 shows the output.

Figure 2.10. The demo02-07.bb program.


Caution

This program will only work if the user enters an integer. If the user enters a string, the variable will always be zero. You can fix this problem using a loop, which will be explained soon.


Notice the new function WaitKey. Instead of delaying for five seconds, WaitKey just waits for the user to press any key before continuing on with the program.

This program tests all three user possibilities.

Sometimes, you might want to test a large number of possibilities, and using If...Then can be awkward. A conditional statement was made to fix this problem: Select...Case

Select...Case

Select...Case makes working with a large number of values much easier. The best way to demonstrate is with an example.

;demo02-08.bb - tests the keys pressed

x = Input$("Enter 1 to say hi, or 0 to quit. ")

Select x
     Case 1
          Print "Hi!"
     Case 0
          End
     Default
          Print "Huh?"
End Select

;Wait five seconds
Delay 5000

In this listing, the program asks the user to enter either one or zero. It then either writes "Hi!" or quits the program. The default case is a catch-all command; if the user enters neither one nor zero, the default code is displayed.

Note

If you haven’t observed it already, notice that I have been indenting my code in a very easy to understand and logical manner. This makes reading and understanding code much easier, and I highly recommend that you do the same.


In this case, Select...Case isn’t very necessary. Because there are only two cases, it is just as easy to use an If...Else. However, when the programs get more complex, Select...Case becomes a more useful tool.

By the way, the declaration for Select...Case is

Select variable

Easy enough, huh?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.107.100