Log in using your OpenID or your Sol Union log in account.
[Cancel]     

C++ Language Tutorial Page 3
Variables and Expressions
Ned Bingham
Previous Index Next
Variables
     If you have ever taken algebra, you know the basics of variables. For example, if x = 5, what is x*5 + 2? Well, if x is 5, then the expression x*5 + 2 is the same as 5*5 + 2, which evaluates to 27. In c++, variable usage is identical to this. However, before you can use a variable, you have to tell the compiler that it exists. This is called declaring the variable. The syntax of declaring a variable is this:

type name;

where name can be anything you want that doesn't have spaces and that doesn't start with a number, and type can be one of the following:

bool - This contains a boolean value: true or false, where true corresponds to the integer value of 1, and false corresponds to the integer value of 0. A variable of this type is contained within 1 byte or 8 bits.

char - This contains a character value: any letter or symbol contained within single quotes. 'a', '1', and '%' are good examples of this. Each character corresponds to an integer value between 0 and 255 inclusive. A variable of this type is contained within 1 byte or 8 bits.

int - This contains an integer value: any non-decimal number such as 10 or -4. A variable of this type is contained within 4 bytes or 32 bits and can store numbers between 2^32 and -2^32 (-4294967296 to 4294967296).

float - This contains a floating point value: any decimal number such as 10, 10.256, -3.5, or -2. A variable of this type is contained within 4 bytes or 32 bits.

double - This contains a decimal value: any decimal number just like float, however this can store numbers in scientific notation, so 3.42E21 or 2.4E-10.

Program: Variables
#include <iostream.h>
#include <stdlib.h>
#include <math.h>

int main()
{
	int integer;
	bool boolean;
	double decimal;
	float floating_point;
	char character;
	
	integer = 5;
	cout << integer << "\n";
	character = 'c';
	cout << character << "\n";
	decimal = 2.5E10;
	cout << decimal << "\n";
	boolean = false;
	cout << boolean << "\n";
	floating_point = 2.53;
	cout << floating_point << "\n";
	return 0;
}
     Next, expressions. So you have this variable, say int x; and you want to set it to the value of 25*5 + 2 - 600. That is called an expression. To do this, the syntax is very similar to the syntax of math: x = 25*5 + 2 - 600; Or, if you want to declare and set the variable all in one line:
int x = 25*5 + 2 - 600;

     You can even increment variables, or use them in an expression. So if x is 5 to begin with, then x = x*5; will set x to 25, and x = x+1; will set x to 6. This is called incrementing. There are several other ways to do this, instead of having to say x = x+1 every time, you can put x+=1 or even x++. However, if you want to say x = x+2, you can't use x++. If you want to write x = x-1, you can write x-=1 or x--. Then, if you want to divide x by 2, you can put x/=2, but there is no x//. Same with multiplication, x*=2, but no x**.

     Boolean expressions are similar to that of numbers, only it deals specifically with ones and zeros. There are also boolean operators. Using the 'equal to' operator, 32 == 4 would return false or 0, and 32 == 32 would return true or 1. With the 'not equal to' operator, 32 != 4 would return true or 1, while 32 != 32 would return false or 0.
Here are the most common boolean operators:
	== 'equal to'
	!= 'not equal to'
	<  'less than'
	>  'greater than'
	<= 'less than or equal to'
	>= 'greater than or equal to'
	&& 'and'
	|| 'or'
	!  'not'
	

Program: Expressions
#include <stdlib.h>
#include <iostream.h>
#include <math.h>

int main()
{
	int i = 2*5 + 3;
	int i2;
	i2 = 5;
	int i3 = i2*i1;
	cout << i << " " << i2 << " " << i3 << "\n";
	
	bool b = false;
	bool b2 = !b;
	bool b3;
	b3 = b || b2;
	cout << b << " " << b2 << " " << b3 << "\n";
	return 0; 
}
     Now you probably want to make your program interactive. To get text input from the console while the program is running, you use cin. Cin is just like cout, only it pauses the program, waits for the user to type in something into the console and press enter, and then takes whatever they entered and stores it in a variable.
int var;
cin>> var;
     Cin works with any variable type, but does not work with objects, which will be discussed later.