Home        Math Games        Word Games        Daily        Reference        Miscellaneous        Junior!        Problems


Beginner Programming Tips

Beginner Programming Tips and Tricks

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   

AddThis Social Bookmark Button     Link To This Page

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   

AddThis Social Bookmark Button     Link To This Page
"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.

 


Want To Try Something Completely Different?

The Treasure Hunt

Pirate's Map

Easter Eggs



Site Features

Word Games

Word Games

Math Games

Math Games

Daily Puzzles

Daily Puzzles

Brainfood

Brainfood

Math Problems

Problems

Miscellaneous

Miscellaneous


 
Search For More Resources

Search For More Educational Resources

Find more educational, problem solving, and puzzle resources using the Google safe-search below.

Google

Member Features
Login
 

Picture Word
Four-Scramble
Word Search
Blackberry Game
Telephone Game
Cheater Hangman
Word Grid
Secret Word
Scrambled Word
One of These
Hangman
 

Entrapment
Adders!
Side By Side
One To Ten
Sub Triangles
Magical Squares
Math Scramble
Secret Number
Secret Number 2
Fractional Hi Lo
Concentration
Monty Hall Game
 

Trio Match
Treasure Hunt
Pirate's Map
Fizziks Tilt
Zero Gravity
Easter Egg Hunt
Quad Puzzle
Tic Tac Toe
Rotating Block
 

Codes
Programming
Search It Out!
 
Secret Word
Scrambled Word
Secret Number
Word Grid
 
Brainfood
Math HS
Maine Page
Calculus Page
 
Contact
About
Related Sites
Link to TPS
 

Bookmarking and Linking
Bookmark/Link


Home        Math Games        Word Games        Daily        Reference        Miscellaneous        Junior!        Problems