Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

AlBalqa Applied University

Programming in C++ LAB

LAB#3: OPERATORS
OBJECTIVES
After reading this lab, we should be able to understand the various operators of
C++ language that include: arithmetic operators, relational operators, logical,
assignment operators, increment, decrement, conditional operators
C++ operators are classified into a number of categories. They include:

1. ARITHMETIC OPERATORS
+ (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulo division)
Note: C++ does not have any operator for exponentiation.
Task1: Write then execute the following code using the numbers:
x=5 and y=2 (notice the value of division /)
x= -5 and y=2, then try x= -5 and y= -2, then try x= 5 and y= -2 (notice the
value of modulus %)
#include <iostream>
using namespace std;
void main() {
int x, y, sum, diff, prod, div, mod;
cout << "Enter first integer: ";
cout << "Enter second integer: ";

sum
diff
prod
div
mod

=
=
=
=
=

cout
cout
cout
cout
cout

<<
<<
<<
<<
<<

x
x
x
x
x

+
*
/
%

cin >> x;
cin >> y;

y;
y;
y;
y;
y;

"\nThe sum is: " << sum << endl;


"The difference is: " << diff << endl;
"The product is: " << prod << endl;
"The division is: " << div << endl;
"The modulus is: " << mod << endl;

2. ASSIGNMENT OPERATORS
a=a+1
a=a1
a = a * (n+1)
a = a / (n+1)
a=a%b

a + =1
a-=1
a *= n + 1
a /= n + 1
a %= b

Task2: Write a C++ code using the previous assignment operators?

3. INCREMENT and DECREMENT OPERATORS


The operator ++ adds 1 to the operands while subtracts 1.It takes the following
form: ++m; or m++ , --m; or m.
Task3: Write then execute the following code:
#include <iostream>
using namespace std;
void main() {
int x=2, y=5, z;
cout<<"x
z=++x;
cout<<"x
y--;
cout<<"x
cout<<"x
cout<<"x

= "<<x<<"\ty = "<<y<<endl;
= "<<x<<"\ty =
x++; --z;
= "<<x<<"\ty =
= "<<++x<<"\ty
= "<<x<<"\ty =

"<<y<<"\tz = "<<z<<endl;
"<<y<<"\tz = "<<z<<endl;
= "<<y++<<"\tz = "<<z--<<endl;
"<<y<<"\tz = "<<z<<endl;

4. RELATIONAL OPERATORS
The value of a relational expression is either one (True) or zero (False).
< (is less than)
<= (is less than or equal to)
> (is greater than)
>= (is greater than or equal to) == (is equal to)
!= (is not equal to)
Task4: Write then execute the following code:
#include <iostream>
using namespace std;
void main() {
int x, y;
cout << "Enter first integer: ";
cout << "Enter second integer: ";
cout << x<<"
cout << x<<"

cin >> x;
cin >> y;

> "<<y<<" = " << (x>y) << endl;


>= "<<y<<" = " << (x>=y) << endl;

cout
cout
cout
cout

<<
<<
<<
<<

x<<"
x<<"
x<<"
x<<"

<
<=
==
!=

"<<y<<"
"<<y<<"
"<<y<<"
"<<y<<"

=
=
=
=

"
"
"
"

<<
<<
<<
<<

(x<y) << endl;


(x<=y) << endl;
(x==y) << endl;
(x!=y) << endl;

5. LOGICAL OPERATORS
&& (logical AND)
|| (logical OR)
! (logical NOT)
short-circuit evaluation :
&& : if the left-hand side expression is false, the combined result is false (the righthand side expression is never evaluated).
|| : if the left-hand side expression is true, the combined result is true (the righthand side expression is never evaluated).
Task5: Write then execute the following code:
#include <iostream>
using namespace std;
void main() {
int x=3, y=7;
bool z;
z= x >= y || x != ++y;
cout<<"x = "<<x<<" y = "<<y<<"
z= x <= y || ++x != y;
cout<<"x = "<<x<<" y = "<<y<<"
z= x != y && x++ == ++y;
cout<<"x = "<<x<<" y = "<<y<<"
z= x > y && x-- == y++;
cout<<"x = "<<x<<" y = "<<y<<"
}

z = "<<z<<endl;
z = "<<z<<endl;
z = "<<z<<endl;
z = "<<z<<endl;

6. CONDITIONAL OPERATORS
A ternary operator : exp1 ? exp2 : exp3;
Task6: Write then execute the following code:
#include <iostream>
using namespace std;
void main() {
int x;
cout << "Enter a number: ";
cin >> x;
(x%2==1)? cout<<x<<" is odd number\n" : cout<<x<<" is even number\n";
}

Try to write only (x%2) in the condition, what do you notice?


Write (x%2==0) in the condition, then change the sentences to output a correct
answer?

7. sizeof OPERATOR
sizeof(operand) : returns the number of bytes the operand occupies.
Task7: Write a C++ code that displays the size (in bytes) of the primary C++
Types?

Operators Precedence
Category
Postfix
Unary
Multiplicative
Additive
Relational
Equality
Logical AND
Logical OR
Conditional
Assignment

Operator
() ++ - + - ! ++ - - (type) sizeof
*/%
+< <= > >=
== !=
&&
||
?:
= += -= *= /= %=

Associativity
Left to right
Right to left
Left to right
Left to right
Left to right
Left to right
Left to right
Left to right
Right to left
Right to left

Task8: Write then execute the following code:


#include <iostream>
using namespace std;
void main() {
int x=7, y=7;
bool z;
z =++x>y;
cout<<"x = "<<x<<" y = "<<y<<" z = "<<z<<endl;
}
Now change the value of z to be: z=x++>y; execute the

Task9: Write then execute the following code:


#include <iostream>
using namespace std;
void main() {
int x=3, y=7;
bool z;
z = x+2*2 == y*(x-2) && 2+x*y >= x/y+10;
cout<<"x = "<<x<<", y = "<<y<<", z = "<<z<<endl;
z = ++x+2*2 == y--*(x-2) || 2+x*y <= x/y+10;
cout<<"x = "<<x<<", y = "<<y<<", z = "<<z<<endl;
}

Explain the output:


X= 3, y= 7, z= 1
X= 4, y= 6, z= 0

code, what do you notice?

You might also like