Home   My Friends   Things of interest Information   Other things
Information
  Front Page
  Programming
  Role Playing Games
  Steve Jackson Games

Chapter 4 - Variables

Getting the computer to type things on the screen is all good and well... but any text editor could do that. Lets make the thing to to what it's best at: math.

For a computer to use a number we need it to remember it... this is called allocating memory. Different data types allocated different sizes of numbers. For inststance the data type "integer" stores an (surprise, surprise) integer between -16384 to +16384. Let us define an integer variable:

#include <iostream.h>

main ()
{
   int VariableA;
   VariableA = 7;
   cout << VariabelA;
}

The first command allocates an integer (int) and ends with ";" to indicate that next there will be an entierly new command (note that a new line won't do). We call this "VariableA". Variable names can include any letters A-Z, a-z, 1-9 plus "_" but cannot start with a number. No spaces.

Second we sets the VariableA to 7. Please not that "=" always sends its data to the left from the right. This means that if you say that "A = B" then A will be set the the value B contains.

The third line is familliar. It will send the data in VariableA as a stream to cout that is defined in iostream.h.

< Previous Chapter | Next Chapter >

 

Johan Thorstensson, 2003©