Home      All Games      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

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   

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
Slick Math!
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      All Games      Math Games      Word Games      Daily      Reference      Miscellaneous      Junior!      Problems