Home Math Games Word Games Strategy Reference Junior Games Daily Puzzles Printables Problems Members

Home Reference Programming Tips

Each page contains a helpful programming tip and exercises which encourage beginners to use what they've learned in a different situation.
Index Previous    Next    Teachers    About   

Treating Equations As Values

A boolean variable is a variable which can hold one of two values. It can be True, or it can be False. Alternately, you could call the two values Yes and No.

Using a boolean variable gives us another way to Toggle A Value; we could use the following bit of code:

Dim MyToggle As Boolean

'Toggle A Boolean Variable
MyToggle = Not MyToggle

If the value was True, it becomes "Not True", or False. And if it was False, it becomes "Not False", or True.

So here's a question for you. Suppose you wanted to set MyToggle to True if X is equal to 7, but set it to False if X does not equal seven.

Here's one way to do it:

Dim MyBoolean As Boolean
Dim X As Integer

'Set the boolean value
If X = 7 Then
   MyBoolean = True
Else
   MyBoolean = False
End If

That'll do the trick, but it's actually not the easiest way to do it. Because every equation can be treated as a Boolean value! You see, X either equals seven, or it doesn't equal seven. If X is not equal to seven, then the equation X = 7 is False.

Right?

So take a look at this:

Dim MyBoolean As Boolean
Dim X As Integer

'Set the boolean value
MyBoolean = (X = 7)

To understand how this works, you should think of the second part of the equation (X = 7) as a question. The computer is asking: Is X equal to seven? And the answer to that question is either Yes (True) or No (False). So the entire line of code is really saying: Set MyBoolean to Yes if X equals seven, and No if X does not equal seven.

In other words, it's doing the same thing as the "If-Then-Else" statement shown above, but it's doing it with just one line of code instead of five!

You can get more complicated with this; take a look at the following code:

Dim MyFirstBoolean As Boolean
Dim MySecondBoolean As Boolean
Dim X As Integer
Dim Y as Integer

'Set the boolean values
MyFirstBoolean = (X = 7) Or (Y = 7)
MySecondBoolean = (X = 7) ANd (Y = 7)

This one sets the boolean value to True if either X equals seven or Y equals seven. The second one will only be True if both X and Y are equal to 7.

Other Scenarios

If Y equals 5, and Z equals 10, what will the value of X be after each of the following lines of code?
  1. X = (Y + Z = 15)
  2. X = (Y > Z) Or (Y = 20)
  3. X = (Y < Z) And (Y = 5)
  4. X = (Y - Z = 0) And (Z = 10)
Index Previous    Next    Teachers    About   

"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 



 
Search For More Resources Search For More Educational Resources

 

Top Games On This Site

 
Adders!
Scramble
Entrapment
Secret Word
Trio Match
 

Just a few of the educational game resources available on The Problem Site!

 
 






Home       All Games       Problems       Contact       Site History       Privacy Policy       Member Page