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   

Counting Truth Values

Suppose you have four variables, X, Y, Z and W, and you want to know how many of them are equal to 10. You could do it like this:

Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Dim Z As Integer
Dim C As Integer

'Find out how many are equal to 10
If X = 10 Then
   C = C + 1
End If
If Y = 10 Then
   C = C + 1
End If
If Z = 10 Then
   C = C + 1
End If
If W = 10 Then
   C = C + 1
End If

In this code, the variable C keeps track of the number of variables that are equal to 10. It works, but it takes twelve lines of code, and the same thing can be done with a single line of code. Really.

Remember that an equation has a value. It is either True, or it is False. What I haven't mentioned yet, but am telling you now, is that Although we think of Equations as being True or False, the computer has a very numerical way of describing that. If an equation is False, the computer says it has a value of Zero. But if it's True, the computer says it has a value of Negative One.

Does that seem strange to you? There are reasons why the computer uses zero and negative one for False and True, but we don't need to get into that right now. But keeping in mind how a computer thinks of True and False, take a look at the following line of code:

Dim X As Integer
Dim Y As Integer
Dim Z As Integer
Dim Z As Integer
Dim C As Integer

'Find out how many are equal to 10
C = -(X = 10) -(Y = 10) -(Z = 10) -(W = 10)

Suppose that X = 10 and Z = 10. Then the Equation evaluates to:

C = - True - False - True - False
C = - (-1) - 0     - (-1) - 0
C = 2
Pretty slick, eh?

Other Scenarios

If X = 0, Y = 10, and Z = 15, what is the numerical value of the following?
  1. - (X = 0) - (Y = 5)
  2. - (X + Y = Z) - (X = 5)
  3. - (X = 0) - (Y = 10) - (Z = 15)
  4. - (X * Y = X * Z)
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!

 
 




ask doug

Home       All Games       Problems       Contact       Site History       Privacy Policy       Member Page