CPP-Lecture-06 E...
CPP-Lecture-06 E...
(Using C++)
Huma Israr
Department Of Information Engineering Technology
National Skills University, Islamabad.
[email protected]
Fall 2023, BS-CS
2
Week 05
Lecture – 01
Lecture – 02
Operators in C++
Semester Calendar
3
Class Schedule
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int a; // Variable declaration int a; // Variable declaration
a = 5; // Actual initialization a = 5; // Actual initialization
a=a+1; a++;
cout<<“a = ”<<a; cout<<“a = ”<<a;
return 0; return 0;
} }
Arithmetic operators ( +, -, *, /, % )
Five arithmetical operations are supported by C++
Arithmetic operator (+,-,*,/,%) directs the computer to
perform certain mathematical manipulations .
+, -, *, and / can be used with integral and floating-point data
types.
The expression x = x+3; equivalent to x+=3;
Similarly
x = x-5;
x -=5;
Operators
+ Addition A+B
- Subtraction A-B
* Multiplication A*B
/ Division A/B
% Modulus Division A%B
+= Add and Assign A+=B A=A+B
-= Subtract and Assign A-=B A=A-B
*= Multiply and Assign A*=B A=A*B
/= Divided and Assign A/=B A=A/B
%= Mod and Assign A%=B A=A%B
Increment(++) & Decrement(– –) Operators :
C++ supports 2 useful operators namely
1. Increment ++ operators
2. Decrement – – operators
++ Increment operator: increment variable by 1
--Decrement operator: decrement variable by 1
Pre-increment: ++variable
Post-increment: variable++
Pre-decrement: --variable
Post-decrement: variable--
Increment Operators Decrement Operators
Prefix increment operator means the Prefix decrement operator means the
variable is incremented first and then variable is decremented first and then
the expression is evaluated using the the expression is evaluated using the
new value of the variable. new value of the variable.
#include<iostream> #include<iostream>
void main() void main()
{ {
int x,i; int x,i;
i=10; i=10;
x=++i; x=i++;
cout<<"x: "<<x; cout<<"x: "<<x;
cout<<"i: "<<i; cout<<"i: "<<i;
Return 0; Return 0;
} }
Relational Operators:
These are used for the comparison of the values of two
operands. For example, checking if one operand is equal to
the other operand or not, an operand is greater than the
other operand or not, etc. Some of the relational operators
are (>, < , ==, >= , <= , !=).
// checks if a is greater than b
a > b;
> and < Operators
Greater than operator: Represented as ‘>’, the greater than
operator checks whether the first operand is greater than the
second operand or not. If so, it returns true. Otherwise it
returns false. For example, 6>5 will return true.
A>b;
Less than operator: Represented as ‘<‘, the less than operator
checks whether the first operand is lesser than the second
operand. If so, it returns true. Otherwise it returns false. For
example, 6<5 will return false.
A<b;
>= and <= Operators
Greater than or equal to operator: Represented as ‘>=’, the
greater than or equal to operator checks whether the first
operand is greater than or equal to the second operand. If so, it
returns true else it returns false. For example, 5>=5 will return
true.
A >= B
Less than or equal to operator: Represented as ‘<=’, the less
than or equal to Operator checks whether the first operand is
less than or equal to the second operand. If so, it returns true
else false. For example, 5<=5 will also return true
A <= B
== and != Operators
Equal to operator: Represented as ‘==’, the equal to operator
checks whether the two given operands are equal or not. If so,
it returns true. Otherwise it returns false. For example, 5==5
will return true.
x == y
Not equal to operator: Represented as ‘!=’, the not equal to
operator checks whether the two given operands are equal or
not. If not, it returns true. Otherwise it returns false. It is the
exact boolean complement of the ‘==’ operator. For example,
5!=5 will return false.
X != Y
int x = 10; int x = 10;
int y = 15; int y = 15;
int z = 10; int z = 10;
x == y // false x != y // true
x == z // true x != z // false
Bitwise OR
a b a|b
0 0 0
0 1 1
1 0 1
1 1 1
Bitwise AND
#include <iostream>
using namespace std;
int main() {
// declare variables
int a = 12, b = 25;
return 0;
}
Any Question