Computer Programming-II Object Oriented Programming & Data Structures
Computer Programming-II Object Oriented Programming & Data Structures
Peshawar, Pakistan
Computer Programming-II
Object Oriented Programming &
Data Structures
Lecture 2
Expression, Operands,
and Operators
By;
Dr. Muhammad Athar Javed Sethi
cout (See Out)
\\ - print backslash
\" - print double quote
\‟ – print single quote
C++ Techniques
Variables
Operators
Decisions
Loops
Variables
Basic Types:
Integers (signed and unsigned)
short
int
Floating point numbers
float
double
Characters (char)
Boolean (bool)
Basic Types: int and float
Integers (int)
0 1 1000 -1 -10 666
Floating point numbers (float)
1.0 .1 1.0e-1 1e1
Basic Types: char
Characters (char)
’a’ ’z’ ’A’ ’Z’ ’?’ ’@’ ’0’ ’9’
Naming variables
Rules For Variables;
First character of variable name must be an alphabetic
character.
Underscore can be used as first character of variable name.
Blank space are not allowed in a variable name.
A good name for your variables is important.
int a,b;
int start_time;
double d;
int no_students;
/* These are
double course_mark;
not good names
/* This is a bit better */
*/
Ideally, a comment with each variable name helps
people know what they do.
case sensitive: x2 & X2 are different
Data Type Size Data storage range
16 bits (2 byte) -32768 to 32767
C++ Variable Covered Range
int
short int 16 bits (2 bytes) -32768 to 32767
long int 32 bits (4 bytes) -2147483648 to
2147483647
unsigned int 16 bits (2 bytes) 0 to 65535
unsigned long int 32 bits (4 bytes)
0 to 4294967295
float 32 bits (4 bytes) 3.4x10(-38) to
3.4x10(+38)
long float 64 bits (8 bytes) 1.7x10(-308) to
1.7x10(+308)
double 64 bits (8 bytes) 1.7x10(-308) to
1.7x10(+308)
long double 80 bits (10 bytes) 3.4x10(-4932) to
1.1x10(+4932)
char 8 bits (1 byte) In case of string
storage capacity is 1
byte to 65535 bytes
Expressions
A constant or variable by itself is an
expression
Examples
7 /* Constant */
x /* Variable */
x + 7 /* with operator */
x = x + 7 /* with operators */
x = x + 7; /* Simple statement */
Operators
+ = add
- = subtract
* = multiply
/ = division
% = modulus - remainder
Logical Operators
== test equality x == y
= assignment x = y
!= not equal
>= greater than or equal
<= less than or equal
More operators
&& and
|| or
! not
The If Statement
Addition x = y + z;
Subtraction x = y – z;
Multiplication x = y * z;
Equal to if (x==10)
Not equal to if (x!=10)
Less than if (x<10)
Greater than if (x>10)
Less than / equal to if (x<=10)
Greater than / equal to if (x>=10)
C++ Operators
The increment Operator
x = 10; x = 10;
y = ++x; y = x++;
if(x > y)
{
x = y + 4;
z = 2 + y;
}
Conditional operator