tutorial C++ part2 variables
In that lesson I will describe what are variables.
program:
program:
program:
char is one sinlge character, and if you want to set it value you need to set it's value between ' ' (not " ").
Numeric variable int
At the begin let speak about variable type int. That variable is a number. And that number can be changed during working of program. For example if we are creating computer game we can have variable called number_of_lifes. Let say that at begin that variable will be equal to 3, and during game it can be changed (Player can lost or can get more lifes thanks to geting high score).program:
int number_of_lifes= 3;
cout<<"number of your lifes is "<<number_of_lifes<<endl;
out:
number of your lifes is 3Now lets try to change that variable program:
int number_of_lifes= 3;
cout<<"number of your lifes is "<<number_of_lifes<<endl;
number_of_lifes= 5;
cout<<"number of your lifes is "<<number_of_lifes<<endl;
out:
umber of your lifes is 3 number of your lifes is 5OK, now lets try program with few variables, and try some numeric operations:
program:
int a=1;
int b=2;
int c=3;
cout<<"c is "<<c<<endl;
c= a+b;
cout<<"c is "<<c<<endl;
c= 2*b;
cout<<"c is "<<c<<endl;
out:
c is 3 c is 3 c is 4You can even use variable to callculate it's new value. In mathematica it have no sense, but in programing it have.
program:
int a=2;
cout<<"a is "<<a<<endl;
a= a+1;
cout<<"a is "<<a<<endl;
out:
a is 2 a is 3
Other types of variables
here is list of standard C++ variables:size | min | max | description | |
bool | 1B | 0 | 1 | true or false |
char | 1B | -128 | 127 | character, but can be also interpretated as number |
int | 4B | -2147483648 | 2147483647 | numeric variable |
float | 4B | - | - | Floating point number. |
double | 8B | - | - | Floating point number. |
char c= 'c';
Working example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
bool b= 0;
int i= 123;
float f=3.14;
double d= 3.14159265358979323846;
cout<<"b have logic value "<<b<<endl;
cout<<"i is "<<i<<endl;
cout<<"f approximating value is "<<f<<endl;
cout<<"d approximating value is "<<d<<endl;
return 0;
}
output:
b have logic value 0 i is 123 f approximating value is 3.14 d approximating value is 3.14159