Variables


At the heart of every program is its variables. In this respect, though, C++ has many differences when compared to Pascal. For instance, in Pascal, when you declare a variable, you say the name and then the type (ie, "Burrito : Integer"), but in C++, it's the other way around! Let's go over the names of the data types in Pascal versus C++:

Pascal    C++       Description

Boolean   bool      "TRUE" or "FALSE" only
Char      char      Single Character ('A', 'g', '1', etc)
Integer   int       Integer (10, -53, 5524, 0, etc)
Real      double    Real Number (0.3, 2.123, -121.1, etc)

Note that the names of the C++ equivalent are both shorter and all lowercase. Now, let's look at how you declare these variables in C++ versus Pascal:

Pascal:
a       : Boolean;
b       : Char;
c       : Integer;
d       : Real;
C++:
bool a;
char b;
int  c;
double d;

Note the way characters are represented: 'A', 'g', and so on. Anything you put between single quotes is considered a character. This is different from Pascal, where the single quotes were used for strings. In C++, the " (double quotes) character is used for strings, which are really just arrays of characters.

Note that the way C++ declares variables is backwards from the way Pascal does it, and no colon is involved. It may take a little getting used to, but it's really not so bad.


Table of Contents

The Big Picture | Comments | Variables | More on Variables
Variable Operations | Making Choices | More on if | Using else
Using switch | The while Loop | The do..while Loop | Constants
The for Loop | More on the for Loop | Exercises