0% found this document useful (0 votes)
3 views11 pages

Lecture 6

Uploaded by

h7ussin3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Lecture 6

Uploaded by

h7ussin3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

‫جامعة بغداد‪ /‬كلية التربية للعلوم الصرفة ابن الهيثم‪/‬قسم علوم الحاسبات‬

‫المرحلة االولى‪ /‬نظري‬


‫السنة الدراسية ‪2024-2023‬‬

‫‪Structured Programming‬‬
‫‪Lecture 6 – Relational , Logical and Bitwise‬‬
‫‪Operators‬‬
Relational and Equality Operators
• The relationships can be expressed in C++ by using the relational operators. These
operators are listed in the following table and assume variable A holds 10 and
variable B holds 20, then:
Here there are some examples:
• ( 7 == 5 ) // evaluates to false.
• ( 5 > 4 ) // evaluates to true.
• ( 3 != 2 ) // evaluates to true.
• ( 6 >= 6 ) // evaluates to true.
• ( 5 < 5) // evaluates to false.
Also, instead of using only numeric constants, we can use any valid
expression, including variables.
Suppose that a=2, b=3 and c=6,
• ( a == 5 ) // evaluates to false since a is not equal to 5.
• ( a * b >= c ) // evaluates to true since (2*3 >= 6) is true.
• ( b + 4 > a * c ) // evaluates to false since (3+4 > 2*6) is false.
• ( ( b = 2 ) == a ) // evaluates to true.
Logical Operators
• The logical expression is constructed from relational expressions by
the use of the logical operators not(!), and(&&), or(||)
Example 1: Assume: a = 4, b = 5, c = 6. find the following expression:

Example 2: Assume X = 0, Y = 1, Z = 1; find the following expression:


Example 3: Suppose you have the following declarations:
• bool found = true ; :
• int age = 20 ;
• double hours = 45.30 ;
• double overTime = 15.00 ;
• int count = 20 ;
• char ch = 'B' ;
Consider the following
Expressions :
Bitwise Operators
• The bitwise operators are listed in the following table as:
Example 4: Write a C++ program to read two numbers and compute
bitwise operators between them.
cout << "d=" << d << "\n" ;
#include<iostream.h> e=a^b;
#include<conio.h> cout << "e=" << e << "\n" ;
main ( ) f=~a;
{ cout << "f=" << f << "\n" ;
g = a << 3 ;
int a, b, c, d, e, f, g, h ; cout << "g=" << g << "\n" ;
cin >> a >> b ; h = a >> 3;
c=a&b; cout << "h=" << h << "\n" ;
cout << "c=" << c << "\n" ; getch ( ) ; }
d = a | b;
Exercises
Q1: Given the following declarations: int i1 = 2, i2 = 5, i3 = -3; double d1 =
2.0, d2 = 5.0, d3 = -0.5; Evaluate each of the following C++ expressions.

Q2 : Write C++ program to two numbers and compute bitwise operators


on them.
Exercises
Q3: Write C++ program to find the value of B (true or false) for the
following: i= 5; j = 9; B= ! (( i > 0 ) && ( i >= j ));

Q4: What is printed by the following code fragment?


int x1 = 2, y1, x2 = 2, y2;
y1 = ++ x1;
y2 = x2 ++ ;
cout << x1 << " " << x2 << "\n";
cout << y1 << " " << y2 << "\n";

You might also like