ﺔﺜﻟﺎﺜﻟا ةﺮﺿﺎﺤﻤﻟا تاﺮﺛﺆﻤﻟا Operators
ﺔﺜﻟﺎﺜﻟا ةﺮﺿﺎﺤﻤﻟا تاﺮﺛﺆﻤﻟا Operators
Operators
C++
C++
Arithmetic Operators 1
C++
C++
C++
c=a+b
a+b
a+b
c=a-b
a-b
a-b
c=a/b
a/b
a
b
c=a*b
a*b
aXb
c=a%b
a%b
C++
C++
C++
m=(a+b+c+d)/4
m=a+b+c+d/4
abcd
4
m abc
m=a*b+w/x-y
m ab
d
4
w
y
x
m=a+(b+c)/8+5*d-(a/b) m a b c 5d a
8
C++
# include <iostream.h >
int main()
{
int a,b;
a=18;
b=5;
cout<<a+b<<"\n";
cout<<a-b<<"\n";
cout<<a*b<<"\n";
cout<<a/b<<"\n";
cout<<a%b<<"\n";
return 0;
}
# include <iostream.h >
main()
{
int a,b;
float x,y;
a=100;
b=5;
x=9-6;
y=0-3;
3
C++
cout<<x+y<<"\n";
cout<<a-b<<"\n";
cout<<a*b<<"\n";
cout<<x/y<<"\n";
return 0;
}
# include < iostream.h >
int main()
{
int p,q,r,x;
p=2;
q=6;
r=p + q / p;
x=( p + q ) / p;
cout<<"r = "<<r<<"\n x = "<<x<<"\n";
return 0;
}
r=5
x=4
1
4
C++
1)
X=7+3*6/2-1;
x =15
2)
y=2%2+2*2-2/2;
y =3
3)
Z=(3*9*(3+(9*3/(3))));
z =324
y=2*5/2+6-12/3
z=(5-6+8/2)+6*9/3-2
w=10-8%3*2+9
Relational Operators 2
C++
False True
C++
x == y
==
x != y
x > y
>
x < y
<
x >= y
>=
x <= y
<=
C++
>=, <=, !=
-35JI
I != J
TRUE
J*2==I*2
FALSE
IJ<I+J
FALSE
I3==J+5
TRUE
J<I>I+J
TRUE
logical operators 3
and
&&
or
not
||
NOT
AND
6
OR
/
C++
X
X && Y
X || Y
!X
True
True
True
True
False
True
False
False
True
False
False
True
False
True
True
False False
False
False
True
((5==5)&&(3>6)) 1
True && False
False
((5==5)||(3>6)) 2
True || False
True
F && F || F || F && F 3
F && F || F || F && F
F || F || F
F || F
F
conditional operator ? 4
(condition) ? result1 : result2
(condition)
7
C++
result1
result2
1
#include <iostream.h>
int main ( )
{
int a,b,c;
a=2;
b=7;
c=(a>b)?a:b;
cout<<"max = "<<c;
return o;
}
2
#include <iostream.h>
int main ( )
{
int a,b,c,d,e;
cin>>a>>b>>c;
d=(a>b)?a:b;
e=(c>d)?c:d;
cout<<"max = "<<e;
return o;
}
compound assignation operators 5
C
b+=cb=b+c
b- =cb=b-c
b*=cb=b*c
8
C++
b/=cb=b/c
b%=cb=b%c
vvr
vvr
vr
v r
vvr
vr
vrv
vr
vrv vr
1
value+=increase
a-=5
a/=b
price*=units+1
Value=value + increase
a=a-5
a=a/b
price=price*(units + 1)
2
//compound assignation
#include <iostream.h>
main ()
{
int a,b=3;
a=b;
a+=2;
//equivalent to a=a+2
cout<<a;
return 0;
}
3
#include <iostream.h>
main ()
9
C++
{
int p,q,r;
p=2;
q=3;
r=10;
r/=p+q;
cout<<"r="<<r;
return 0;
Increase and decrease operators 6
C++ 1
C
C+=1
C=C+1
i++i
ii++
ii++i++i
xx++x
xx++
yy--y
yy--
2
b a
10
C++
a = 6
a =+ +b
a =b ++
b
a = - -b
a = b- -
a=7
a=7
a=7
b=7
b=8
b=9
a=8
a=8
b=8
b=7
b = 6
2
a = 2, b = 4, c = 6
a, b,c
1. a + = 5 + a
2. a * = b+ + -c
3. a - = -b+ +*++c
a + =5+a = 5+2 =7
a = a +7= 2+7 = 9
a=9,b=4, c=6
a * = b+ + -c = 4-6 = -2
a = a*-2 = 9 *-2 = - 18
a =-18 , b = 5 , c = 6
a- = -b+ +*++c
a- = -5*7=-35
a = a-(-35) =-18 + 35= 17
11
C++
#include <iostream.h>
main ()
{
int i,j,k;
i=j=2;
k=++i;
cout<<"i="<<i<<"k="<<k<<"\n";
k=j++;
cout<<"j="<<j<<"k="<<k<<"\n";
return 0;
I=3 k=3
J=3 k=2
12