Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

VARIABLES AND OPERATORS

VARIABLE
Variable a portion of memory to store a
determined value
a = 5;
b =2;
a = a+1;
result = a b;
Identifier = a sequence of one or more letters or
underscore characters.
A, b, result
Case-sensitive
RESERVED KEYWORD
asm, auto, bool, break, case, catch, char, class,
const, const_cast, continue, default, delete, do,
double, dynamic_cast, else, enum, explicit,
export, extern, false, float, for, friend, goto, if,
inline, int, long, mutable, namespace, new,
operator, private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof,
static, static_cast, struct, switch, template, this,
throw, true, try, typedef, typeid, typename,
union, unsigned, using, virtual, void, volatile,
wchar_t, while, and, and_eq, bitand, bitor, compl,
not, not_eq, or, or_eq, xor, xor_eq
FUNDAMENTAL DATA TYPES
DECLARATION OF VARIABLES
Syntax: <data type> <valid identifier>
Examples
int a;
float mynumber;
int a,b,c;
int a;
int b;
int c;
short year;
short int year
SCOPE OF VARIABLES
Global variables can be referred from anywhere in the
code, even inside functions, whenever it is after its
declaration.
Local variables - limited to the block enclosed in braces ({})
where they are declared

INITIALIZATION OF VARIABLES
Syntax:
<data type> <identifier> = <initial value>
<data type> <identifer> (inital value)
Example:
Int a = 0
Int b(2)
OPERATORS
Operators in C and C++ are mostly made of signs
that are not part of the alphabet but are
available in all keyboards
Assignment assignment operator assigns a
value to a variable
a = 5;
a = b;
a = 2 + (b = 5);
b = 5;
a = 2 + b;
a = b = c = 5;
ARITHMETIC OPERATORS
+ addition
- subtraction
* multiplication
/ division
% modulo
Modulo is the operation that gives the remainder of a division of
two values
a = 11 % 3; // the variable a will contain the value 2, since 2 is the
remainder from dividing 11 between 3
COMPOUND ASSIGNMENT
Expression Is equivalent to
value += increase; value = value + increase;
a -= 5; a = a 5;
a /= b; a = a / b;
price *= units + 1; price * (units + 1)
Compound assignments : += , -= , *=, /=, %= ,
>>=, <<=, &=, ^=, |=
INCREASE AND DECREASE
(++) increase operator
(--) decrease operator
c++;
c += 1;
c = c + 1;
b = 3; a = ++b; // b is increased before its value is copied
to a
b = 3; a = b++; // the value of b is copied to a and then b
is increased


RELATIONAL AND EQUALITY OPERATORS
Used to evaluate a comparison between two
expressions
The result of a relational operation is a boolean
value that can only be true or false, according to
its boolean result
==, !=, >, <, >=, <=

LOGICAL OPERATOR
!, &&, ||

DEMO
DEMO C++
DEMO C
SCANF FORMAT
LABORATORY EXERCISES
Write a program that calculates the squares and
cubes of the integers from 1 to 10 and use tabs to
print the following neatly formatted table of
values
Filename : <surname>_EX2.cpp
Filename : <surname>_EX2.c





6
/
2
0
/
2
0
1
4

18
EXERCISE
Make a program that will compute for the
perimeter, area and volume of the following:
Rectangle ( perimeter & area)
Circle (perimeter & area)
Triangle (perimeter & area)
You need to ask the user to enter the values
needed
Filename: <surname>_EX3.cpp
Filename: <surname>_EX3.c

6
/
2
0
/
2
0
1
4

19

You might also like