You are on page 1of 13

1

Lecture 2

 Operator: is a symbol that tells the compiler to perform specific mathematical


or logical manipulation.
‫عبارة عن رمز يخبر المترجم بإجراء عملية حسابية او منطقية‬
 Types:
 Arithmetic Operators (‫)العوامل الحسابية‬.
 Relational Operators (‫)العوامل العالقية‬.
 Logical Operators (‫)العوامل المنطقية‬.
 Bitwise Operators (bit‫) العوامل المختصة بال‬.
 Assignment Operators (‫)عوامل التخصيص‬.
 Misc Operators (‫)عوامل متنوعة‬.

 Arithmetic Operators: ( +, -, *, /, % )
Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example


+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
Modulus Operator and remainder of after an integer B % A will give 0
%
division
++ Increment operator, increases integer value by one A++ will give 11
-- Decrement operator, decreases integer value by one A-- will give 9

Dr/ A.A.A
2

:‫مثال على ذلك‬

#include <iostream>
using namespace std;

int main()
{
int a = 21;
int b = 10;
int c ;

c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;
c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;
c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;
c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0;
}

:‫وتكون النتيجة كما بالشكل‬

‫ العدد‬10 ‫ على‬21 ‫ يعنى ان باقى قسمة‬b ‫ على‬a ‫ هذا السطر عبارة عن باقى قسمة‬c = a % b; ‫فى السطر‬
1 ‫ أذا النتيجة لباقى القسمة هو‬1 ‫ والمتبقى‬10 ‫ فيقبل القسمة على‬20 ‫االقرب هو‬

Dr/ A.A.A
3

.‫ سنشرحهم بالتفصيل‬c = a- -; ‫ والسطر‬c = a++; ‫فى السطر‬


 Increment and Decrement Operators:‫الزيادة أو النقصان‬
. ‫ ويضيف واحد الى المعامل‬++ ‫ ياخد العالمة‬: Increment o
. ‫ ياخد العالمة – ويطرح واحد من المعامل‬: Decrement o
x ‫فمثال لو عندنا متغير اسمه‬
. x ‫ وتخزين الناتج الجديد فى‬x ‫ إلى قيمة‬1 ‫ وهذا يعنى اضافة‬x++; ‫ هذا مماثل ل‬x=x+1; ‫فان‬
. x ‫ وتخزين الناتج الجديد فى‬x ‫ من قيمة‬1 ‫ وهذا يعنى طرح‬x--; ‫ هذا مماثل ل‬x=x-1; ‫وأيضا‬
 Prefix and Postfix:
Prefix: meaning that the operator precedes the variable
Postfix: meaning that the operator follows the variable.
++x; // Prefix
x++; // Postfix
#include <iostream>
using namespace std;
int main()
{
int x = 10;
cout << "The Value Of X= " << x << endl; //displays 10
cout << "The Value Of X= " << ++x << endl; //displays 11 (prefix)
cout << "The Value Of X= " << x << endl; //displays 11
cout << "The Value Of X= " << x++ << endl; //displays 11 (postfix)
cout << "The Value Of X= " << x << endl; //displays 12
return 0;
}

11 ‫ فبالتالى اصبح الناتج‬10 ‫ وكانت قيمتها‬x ‫ الى‬1 ‫ نضيف قيمة‬: prefix ‫ فى حالة ال‬

Dr/ A.A.A
4

‫ كما هى ولذلك‬x ‫ ظلت قيمة ال‬: Postfix ‫ ثم فى حالة ال‬11 ‫ النهائية‬x ‫ثم السطر التالى اصبحت قيمة ال‬
‫ للسطر التالى له‬1 ‫ ولكن بيزود قيمة‬11 ‫طبع قيمة‬
12 = 1+ 11 ‫ النهائية‬x ‫لذلك اصبحت ال‬
 Library Functions: These functions perform file access, mathematical
computations, and data conversion, among other things.
sqrt() ‫والمثال التالى لحساب جذر تربيعى لعدد عن طريق الدالة‬
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double number, answer; //sqrt() requires type double
cout << "Enter a number: ";
cin >> number; //get the number
answer = sqrt(number); //find square root
cout << "Square root is " << answer << endl; //display it
return 0;
}

#include <cmath> ‫ البد من كتابة السطر‬sqrt ‫لكى يتم استدعاء دالة الجذر‬

 Relational Operators:
There are following relational operators supported by C++ language

Assume variable A holds 10 and variable B holds 20, then:

Dr/ A.A.A
5

Operator Description Example


Checks if the values of two operands are equal or not, if (A == B) is not
==
yes then condition becomes true. true.
Checks if the values of two operands are equal or not, if
!= (A != B) is true.
values are not equal then condition becomes true.
Checks if the value of left operand is greater than the
(A > B) is not
> value of right operand, if yes then condition becomes
true.
true.
Checks if the value of left operand is less than the value
< (A < B) is true.
of right operand, if yes then condition becomes true.
Checks if the value of left operand is greater than or
(A >= B) is not
>= equal to the value of right operand, if yes then
true.
condition becomes true.
Checks if the value of left operand is less than or equal
<= to the value of right operand, if yes then condition (A <= B) is true.
becomes true.
. ‫اقرى الجدول وهنعرفها أكتر فى الباب التالى‬

 Logical Operators:
There are following logical operators supported by C++ language

Assume variable A holds 1 and variable B holds 0, then:

Operator Description Example


Called Logical AND operator. If both the
&& operands are non-zero, then condition becomes (A && B) is false.
true.
Called Logical OR Operator. If any of the two
|| operands is non-zero, then condition becomes (A || B) is true.
true.
Called Logical NOT Operator. Use to reverses the
! logical state of its operand. If a condition is true, !(A && B) is true.
then Logical NOT operator will make false.
.‫اقرى الجدول وهنعرفها أكتر فى الباب التالى‬

 Bitwise Operators:
The Bitwise operators supported by C++ language are listed in the following
table. Assume variable A holds 60 and variable B holds 13, then:

Dr/ A.A.A
6

Operator Description Example


Binary AND Operator copies a bit to the result if it (A & B) will give 12 which
&
exists in both operands. is 0000 1100
Binary OR Operator copies a bit if it exists in (A | B) will give 61 which
|
either operand. is 0011 1101
Binary XOR Operator copies the bit if it is set in (A ^ B) will give 49 which
^
one operand but not both. is 0011 0001
(~A ) will give -61 which
Binary Ones Complement Operator is unary and is 1100 0011 in 2's
~
has the effect of 'flipping' bits. complement form due to
a signed binary number.
Binary Left Shift Operator. The left operands
A << 2 will give 240
<< value is moved left by the number of bits
which is 1111 0000
specified by the right operand.
Binary Right Shift Operator. The left operands
A >> 2 will give 15 which
>> value is moved right by the number of bits
is 0000 1111
specified by the right operand.

o Bitwise AND (&):


‫ غير ذلك تكون النتيجة صفر‬1 ‫ تكون النتيجة‬1 ‫ و‬1 (bits ‫لو المدخالت (ال‬

0 0 1 1 operand1
0 1 0 1 operand2
----------
0 0 0 1 (operand1 & operand2) –

:‫مثال على ذلك‬

int a = 92; // in binary: 0000000001011100


int b = 101; // in binary: 0000000001100101
int c = a & b; // result: 0000000001000100, or 68 in decimal.

o Bitwise OR (|):
‫ غير ذلك تكون النتيجة صفر أو بمعنى لو المدخالت صفر‬1 ‫ تكون النتيجة‬1 (bits ‫لو أحد المدخالت (ال‬
.‫وصفر تكون النتيجة صفر‬

Dr/ A.A.A
7

0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 1 (operand1 | operand2) - returned result

:‫مثال على ذلك‬

int a = 92; // in binary: 0000000001011100


int b = 101; // in binary: 0000000001100101
int c = a | b; // result: 0000000001111101, or 125 in decimal.

o Bitwise XOR (^):


‫ غير ذلك تكون‬1 ‫ او صفر وصفر تكون النتيجة‬1 ‫ تكون النتيجة‬1 ‫ و‬1 (bits ‫ لو المدخالت (ال‬o
‫النتيجة صفر‬

0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 0 (operand1 ^ operand2) - returned result

:‫مثال على ذلك‬

int x = 12; // binary: 1100


int y = 10; // binary: 1010
int z = x ^ y; // binary: 0110, or decimal 6

Assignment Operators:
There are following assignment operators supported by C++ language:

Operator Description Example


Simple assignment operator, Assigns
C = A + B will assign value of A + B
= values from right side operands to left
into C
side operand
Add AND assignment operator, It adds
+= right operand to the left operand and C += A is equivalent to C = C + A
assign the result to left operand

Dr/ A.A.A
8

Subtract AND assignment operator, It


subtracts right operand from the left
-= C -= A is equivalent to C = C - A
operand and assign the result to left
operand
Multiply AND assignment operator, It
multiplies right operand with the left
*= C *= A is equivalent to C = C * A
operand and assign the result to left
operand
Divide AND assignment operator, It
divides left operand with the right
/= C /= A is equivalent to C = C / A
operand and assign the result to left
operand
Modulus AND assignment operator, It
%= takes modulus using two operands and C %= A is equivalent to C = C % A
assign the result to left operand
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
bitwise exclusive OR and assignment
^= C ^= 2 is same as C = C ^ 2
operator
bitwise inclusive OR and assignment
|= C |= 2 is same as C = C | 2
operator

Dr/ A.A.A
9

‫مثال على ذلك‬

#include <iostream>
using namespace std;

int main()
{
int a = 21;
int c ;

c = a;
cout << "Line 1 - = Operator, Value of c = : " <<c<< endl ;

c += a;
cout << "Line 2 - += Operator, Value of c = : " <<c<< endl ;

c -= a;
cout << "Line 3 - -= Operator, Value of c = : " <<c<< endl ;

c *= a;
cout << "Line 4 - *= Operator, Value of c = : " <<c<< endl ;

c /= a;
cout << "Line 5 - /= Operator, Value of c = : " <<c<< endl ;

c = 200;
c %= a;
cout << "Line 6 - %= Operator, Value of c = : " <<c<< endl ;

c <<= 2;
cout << "Line 7 - <<= Operator, Value of c = : " <<c<< endl ;

c >>= 2;
cout << "Line 8 - >>= Operator, Value of c = : " <<c<< endl ;

c &= 2;
cout << "Line 9 - &= Operator, Value of c = : " <<c<< endl ;

c ^= 2;
cout << "Line 10 - ^= Operator, Value of c = : " <<c<< endl ;

c |= 2;
cout << "Line 11 - |= Operator, Value of c = : " <<c<< endl ;

return 0;
}

Dr/ A.A.A
10

 Misc Operators
There are few other operators supported by C++ Language.

Operator Description
sizeof operator returns the size of a variable. For example,
sizeof
sizeof(a), where a is integer, will return 4.
Conditional operator. If Condition is true ? then it returns value X :
Condition ? X : Y
otherwise value Y
Comma operator causes a sequence of operations to be
, performed. The value of the entire comma expression is the value
of the last expression of the comma-separated list.
. (dot) and -> Member operators are used to reference individual members of
(arrow) classes, structures, and unions.
Casting operators convert one data type to another. For example,
Cast
int(2.2000) would return 2.
Pointer operator & returns the address of an variable. For example
&
&a; will give actual address of the variable.
Pointer operator * is pointer to a variable. For example *var; will
*
pointer to a variable var.

Dr/ A.A.A
11

.‫ اخدناها قبل كدا‬sizeof

 Conditional operator:
The conditional operator evaluates an expression, returning one value if that
expression evaluates to true, and a different one if the expression evaluates
as false. Its syntax is:
condition ? result1 : result2

 Operators Precedence in C++:


‫االسبقية فى التنفيذ‬

‫يتم اسبقية التنفيذ تبعا للجدول اآلتى‬

Dr/ A.A.A
12

Category Operator Associativity


Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

#include <iostream>
using namespace std;

int main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;

e = (a + b) * c / d; // ( 30 * 15 ) / 5
cout << "Value of (a + b) * c / d is :" << e << endl ;

e = ((a + b) * c) / d; // (30 * 15 ) / 5
cout << "Value of ((a + b) * c) / d is :" << e << endl ;

e = (a + b) * (c / d); // (30) * (15/5)


cout << "Value of (a + b) * (c / d) is :" << e << endl ;

e = a + (b * c) / d; // 20 + (150/5)
cout << "Value of a + (b * c) / d is :" << e << endl ;

return 0;
}

Dr/ A.A.A
‫‪13‬‬

‫نالحظ انا االقواس تنفذ أوال ثم العمليات الحسابية من الشمال لليمين‪.‬‬

‫‪Dr/ A.A.A‬‬

You might also like