CPP 2
CPP 2
Variable name
Name valid
name valid
name_1 valid
Int valid
_SUM valid
sum_of_the_numbers valid
num^2 Not valid
cin Valid // iostream not included
Variable name example
Variable name
Int valid
$sum Not valid
num^2 Not valid
num 1 Not valid
2num Not valid
INT valid
firstName valid
What are variable
data types ?
Boolean bool
Character char
Integer int
Long int long
Floating point float
Double floating point double
Variable types can be modified using one or more of these type
modifiers:
signed
unsigned
short
long
Declaration of variables
int a ;
int a,b;
float c;
Declare and initialize variable
int a=5;
Variables and data types
Constants
Operators
Input command
You can use const prefix to declare constants
with a specific type as follows:
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
Result
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE; 50
return 0;
}
Variables and data types
Constants
Operators
Input command
Arithmetic operators
operator description
+ addition
- subtraction
* multiplication
/ division
% modulo
X= 11%3 Result is 2
Expression Equivalent to...
y += x; y = y + x;
x -= 5; x = x - 5;
x /= y; x = x / y;
price *= units + 1; price = price * (units+1);
// compound assignment operators
#include <iostream.h>
int main ()
{
int a, b=3;
a = b;
a+=2; // equivalent to a=a+2
cout << a;
}
Result
5