Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ lesson about variables

c++ myvariable variable type

  • Please log in to reply
No replies to this topic

#1 Csabi

Csabi

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 60 posts

Posted 01 December 2010 - 06:33 AM

What is a variable ? A variable is a facility to store data. We can store values in variables and we can change it`s value. In C++ we can use multiple variable types, but the following ones are the most used:
[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 text
Before 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 5
As 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; //1
To 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 expression
For 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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: c++, myvariable, variable type

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download