You are on page 1of 22

Lecture two

 Variables and data types


 Constant s
 Operators
 Input command
 Variables and data types
 Constant s
 Operators
 Input command
 What is Variable?
A portion of memory to store a value.
 What are variable
name constraints?
1. Only Alphabets, Digits and Underscores are
permitted.
2. Variable name cannot start with a digit.
3. Reserved words cannot be used as a name.
4. Upper case and lower case letters are distinct.
5. Special Characters are not allowed.
6. Global Identifier cannot be used as Variable.
 Variable name examples

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

Data type variable name ;

int a ;

Integer a,b Decimal c

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:

const type variable = value;


#include <iostream.h>

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

You might also like