Lab#3: Operators Objectives: Albalqa Applied University Programming in C++ Lab
Lab#3: Operators Objectives: Albalqa Applied University Programming in C++ Lab
Lab#3: Operators Objectives: 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;
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
= "<<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;
cout
cout
cout
cout
<<
<<
<<
<<
x<<"
x<<"
x<<"
x<<"
<
<=
==
!=
"<<y<<"
"<<y<<"
"<<y<<"
"<<y<<"
=
=
=
=
"
"
"
"
<<
<<
<<
<<
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";
}
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