[B]int[/B] - An integer number [B]bool[/B] - Boolean, it can be only True or False [B]float[/B] - Floating point number [B]double[/B] - Double precision floating point number [B]char[/B] - A single character [B]string[/B] - String of textBefore using a variable, it needs to be initialized. The syntax is:
type identifier; or type identifier = value;In this lesson we will talk only about integer, these are the easiest to work with. Here is an example of initializing an integer without and with an initial value. I will call the variable myvariable:
int myvariable; //without initial value int myvariable = 5; //the initial value is 5As I said the value of the variables can be changed using operators. The basic operators in C++ are: + , - , * , / , % , = , += , -= , *= , /= , %= , and here is a long list of examples:
int myvariable; // intializing without initial value myvariable = 5 + 2; // 7 myvariable = myvariable - 4; //3 myvariable = 6 * myvariable; //18 myvariable = myvariable / 3; //6 myvariable = myvariable % 4; //2 (division remainder) myvariable = 10; //10 myvariable += 2; //12 myvariable -= 8; //4 myvariable *= 2; //8 myvariable /= 2; //4 myvariable %= 3; //1To write out the value of a variable just leave the double quotes from the cout line:
cout << myvariable;, but we will talk more about cout in the next lesson. Here is an example program that uses integer variables:
#include <iostream> using namespace std; int main(){ int myvariable; // intializing without initial value myvariable = 5 + 2; //7 myvariable = myvariable - 4; //3 myvariable = 6 * myvariable; //18 myvariable = myvariable / 3; //6 myvariable = myvariable % 4; //2 (division remainder) myvariable = 10; //10 myvariable += 2; //12 myvariable -= 8; //4 myvariable *= 2; //8 myvariable /= 2; //4 myvariable %= 3; //1 cout << myvariable << endl; system("pause"); }The above program should write out 1. Play with the code, remove and add new lines to familiarize with integers.
Increment and Decrement
Using the incrementing (++) or decrementing (--) operators means to add or to subtract 1 from the value of an integer variable. These operators can be used in two ways:
++number; //the value is increased before the value is used in the rest of the expression or number++ //the value is increased after the value is used in the rest of the expressionFor example try out the following code:
#include <iostream> using namespace std; int main(){ int numberone = 2; int numbertwo = 2; cout << ++numberone << endl << numbertwo++ << endl; system("pause"); }As you probably noticed the two written values are not the same. The first is 3, because the value is increased before it`s written and the second value is 2,because it`s increased only after it`s written.
Note: The increment and decrement operators are working only with integers!
float and double
Float and double are working exactly like integers, but these can be floating point numbers, too (2.75 or 9.24323). The difference between float and double is that a double can have more numbers after the decimal point then a float. Initializing a float or a double should look like in the bellow example:
float myvariable; or float myvariable = 4.23;Making the result of integer division a float
Try out to give the value of dividing two integers to a float. The result will don`t have floating point any numbers you use. To solve this look at my example:
int numone = 1; int numtwo = 2; float result = (float) numone / numtwo;Try out the above example, it should work.
double myvariable; or double myvariable = 5.24;Booleans
Booleans can have only two values: true(1) or false(0). Initializing a boolean should look like in the bellow example:
bool myvariable = true; or bool myvariable = false;Note: In C++ a and A are not equal. Be careful, don`t use uppercase letters to write true or false. This is true for the name of the variables, too. For example: myvariable and Myvariable are two different variables.