0% found this document useful (0 votes)
2 views

Lecture#05

Uploaded by

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

Lecture#05

Uploaded by

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

Programming Fundamentals

CS-111
(4 CrH = 3 Theory 1 Lab)
Lecture No. 05

Department of Computational Sciences


The University of Faisalabad (TUF)
In Last Lecture….
• Variable names and types
• Operators in C++
• Arithmetic Operators
• Dry test
In Today’s Lecture….
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Relational Operators
Assume variable A holds 10 and variable B holds
20, then
EXAMPLE
• #include<iostream>
• using namespace std;
• int main(){
• int x=3,y=5;
• cout<<(x>y)<<endl<<(x<y)<<endl;
return 0;
•}
Logical Operators
Assume variable A holds 1 and variable B holds
0, then
Bitwise Operators …

Assume if A = 60; and B = 13; now in binary


format they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------------------------------------------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Bitwise Operators …
Bitwise operator works on bits and perform bit-
by-bit operation. The truth table for &, |, and ^
are as follows
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
Assignment Operators
Assignment Operators

Assignment Operators …
Example
#include <iostream>
using namespace std; 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;
}
#include <iostream>
using namespace std; 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;
}

Assignment
Operators
Example
Miscellaneous
Operators
Miscellaneous
Operators …
Miscellaneous
Operators …
Operators Precedence in
C++
• Operator precedence determines the grouping of
terms in an expression. This affects how an
expression is evaluated. Certain operators have
higher precedence than others; for example, the
multiplication operator has higher precedence than
the addition operator
• For example x = 7 + 3 * 2; here, x is assigned 13,
not 20 because operator * has higher precedence
than +, so it first gets multiplied with 3*2 and then
adds into 7.
• Here, operators with the highest precedence
appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher
precedence operators will be evaluated first.
Operator
s
Preceden
ce in C++
Operators Precedence in C++
Example

You might also like