Boolean Values and Taking Turns

Many games involve two players who alternate taking turns. In those situations, a game program must keep track of whose turn it is to play. Boolean values are useful in those circumstances.

A boolean value is one that can take on any one of only two possible values. This is sort of like a power switch for a light bulb or a cell phone: it is either on or off: there are no other possibilities.

In Basic, the values of a boolean variable are true and false. The NOT operator can be applied to a boolean value. The NOT operator flips boolean values. NOT true is false and NOT false is true.

Here are examples of boolean variables and values.

 dim isTall as Boolean = true        'Create a boolean variable isTall and set it to true
 dim IamHungry as boolean            'Create a boolean variable IamHungry
 IamHungry = false                   'Says I am not hungry
 dim x as boolean                    'Create a boolean variable x                                    
 x = NOT IamHungry                   'Assign to x the opposite value of IamHungry, so x is now true

Let us write a program that uses a single button to switch colors between green and blue (or yellow and pink if you are tired of Green and Blue. The form should look as you see here.




Pressing the button once changes the background color of the form to Green:






Pressing the button again changes the background color of the form to Blue. The form keeps alternating colors between green and blue with each click of the button.





Previous Next