Professional Documents
Culture Documents
Lec 2
Lec 2
Lecture 2
Arithmetic Operators: ( +, -, *, /, % )
Assume variable A holds 10 and variable B holds 20, then:
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
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
Dr/ A.A.A
5
Logical Operators:
There are following logical operators supported by C++ language
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
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 0 0 1 (operand1 & operand2) –
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
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 0 (operand1 ^ operand2) - returned result
Assignment Operators:
There are following assignment operators supported by C++ language:
Dr/ A.A.A
8
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
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
Dr/ A.A.A
12
#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; // 20 + (150/5)
cout << "Value of a + (b * c) / d is :" << e << endl ;
return 0;
}
Dr/ A.A.A
13
Dr/ A.A.A