Conditionals

[ LiB ]

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

Truth and Falsehood

Blitz Basic 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 than the rest, the entire expression is only evaluated as one or the other.

Blitz Basic (and computers in general) believes that 0 is false and any other value ( non-zero value) is true, although the true value is usually 1. This makes programming a much easier job.

To check and see if something is true or false, we use the relational and logical operators. These operators check one statement against another to see if 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 age = Input$("How old are you? ") If age >= 18 Then     Print "You are legally allowed to vote!" EndIf WaitKey 

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 in Figure 2.8, this program does nothing if the user is younger than 15. The program then waits for the user to press a key for the program to exit.

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 age = Input$("How old are you? ") If age >= 18 Then     Print "You are legally allowed to vote!" Else     Print "Sorry, you need to be a few years older." EndIf WaitKey 

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.


NOTE

CAUTION

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

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 WaitKey 

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

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.

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 read ing and understanding code much easier, and I highly recommend that you do the same.

By the way, the declaration for Select Case is

Select variable

Easy enough, huh?

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net