0% found this document useful (0 votes)
15 views26 pages

Chapter 5

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)
15 views26 pages

Chapter 5

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/ 26

Faculty of Computer Science

Introduction to Programming

Lecturer: Lutfullah “Haqnesar”


Introduction to Programming

Chapter 5

Operators
Introduction to Programming

Learning outcomes:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Increment / decrement Operators
 Ternary Operators
Introduction to Programming

Operator
• An operator is a symbol that is used to perform operations.
• Different types of operators perform different types of operations in C+
+ language.
Introduction to Programming

Types of Operator
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operator
6. Increment / decrement Operators
7. Ternary or Conditional Operator
Introduction to Programming
Introduction to Programming
1. Arithmetic Operators
• Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example


+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x*y
/ Division Divides one value by another x/y
% Modulus Returns the division remainder x%y
Example
public class Example {
public static void main(String args[]) {
int a=10;
int b=20;
System.out.println (a+b);
System.out.println (a-b);
System.out.println (a*b);
System.out.println (a/b);
System.out.println (a%b);
}}
Introduction to Programming

2. Relational Operator
A relational operator is used to check the relationship between two operands.
if the values of two operands are equal, then condition becomes true else its value become
false.
Operator Meaning Example

== Is Equal To 3 == 5 gives us false

!= Not Equal To 3 != 5 gives us true

> Greater Than 3 > 5 gives us false

< Less Than 3 < 5 gives us true

>= Greater Than or Equal To 3 >= 5 give us false

<= Less Than or Equal To 3 <= 5 gives us true


Introduction to Programming
Example
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x == y) <<endl;
cout << (x != y) <<endl;
cout << (x > y) <<endl;
cout << (x >= y) <<endl;
cout << (x < y) <<endl;
cout << (x <= y) <<endl;
// returns 0 for alse and 1 for true
return 0;
Introduction to Programming

3. Logical Operator
• Logical operators are used to determine the logic between variables or
values.
• Logical operators are used to check whether an expression is true or false.
• If the expression is true, it returns 1 whereas if the expression is false, it
returns 0.
Introduction to Programming

3. Logical Operator

Operator Name Description Example


&& Logical and Returns true if both statements are 4 < 5 && 6 < 10
true
|| Logical or Returns true if one of the 4 < 5 || 7 < 4
statements is true
! Logical not Reverse the result, returns false if !(3 < 5 && 3 < 10)
the result is true
Introduction to Programming

3. Logical Operator
The logical operators && and || are used when evaluating two expressions to
obtain a single relational result.

&& OPERATOR (and) || OPERATOR (or)


a b a && b a b a || b
true true true true true true
true false false true false true
false true false false true true
false false false false false false
Introduction to Programming
Example
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 3;
cout << (x > 3 && x < 10) <<endl;
cout << (x > 3 && x < 10) <<endl;
cout << !(x > 3 && x < 10);
}
Introduction to Programming

4. Bitwise Operator
In C++, bitwise operators are used to perform operations on individual bits.
Operator Description

& Binary AND

| Binary OR

^ Binary XOR

~ Not (Binary One's Complement)

<< Binary Shift Left

>> Binary Shift Right


Introduction to Programming

4. Bitwise Operator
p q p&q p|q ~p p^q
0 0 0 0 1 0
0 1 0 1 1 1
1 0 0 1 0 1
1 1 1 1 0 0
Introduction to Programming

4. Bitwise Operator
There are two shift operators in C++ programming:
1. Right shift operator >>
2. Left shift operator <<
Introduction to Programming
Example
#include <iostream>
using namespace std;
int main() {
int a = 12, b = 25;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a & b = " << (a & b) << endl;
return 0;
Introduction to Programming

5. Assignment Operator
In C++, assignment operators are used to assign values to variables.

Example:
int x = 10;
Introduction to Programming

5. Assignment Operator
Operator Example Same As
= x=5 x=5
+= x+=3 x=x+3
-= x-=3 x=x-3
*= x*=3 x=x*3
/= x/=3 x=x/3
%= x%=3 x=x%3
Introduction to Programming

Example:
#include <iostream>
using namespace std;
int main() {
int x = 5;
x += 3;
cout << x;
return 0;
}
Introduction to Programming

6. Increment / decrement Operators


Increment and decrement operators are used to increases or deceases the of
variable by one.

Operator Name Description Example


++ Increment Increases the value of a variable by 1 x++

-- Decrement Decreases the value of a variable by 1 x--


Introduction to Programming
Example
#include <iostream>
using namespace std;
int main() {
int x = 10;
int y = 15;
x ++;
y --;
cout << x << endl ;
cout << y ;
}
Introduction to Programming

7. Conditional or Ternary Operator ( ? : )


The conditional operator evaluates an expression, returning one value if
that expression evaluates to true, and a different one if the expression
evaluates as false.
Its syntax is:
condition ? result1 : result2

 If condition is true, the entire expression evaluates to result1, and


otherwise to result2.
Introduction to Programming

Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy