0% found this document useful (0 votes)
18 views20 pages

Lecture 03

This document summarizes a lecture on operators in programming fundamentals. It discusses the different types of operators including arithmetic, assignment, increment/decrement, relational, conditional, and ternary operators. Examples are provided for each operator type to demonstrate their usage, truth tables are shown for conditional operators, and short-circuit evaluation is explained for logical operators. The document concludes by mentioning bitwise operators and providing contact information for the lecturer.

Uploaded by

Mʌ ɭɩĸ
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)
18 views20 pages

Lecture 03

This document summarizes a lecture on operators in programming fundamentals. It discusses the different types of operators including arithmetic, assignment, increment/decrement, relational, conditional, and ternary operators. Examples are provided for each operator type to demonstrate their usage, truth tables are shown for conditional operators, and short-circuit evaluation is explained for logical operators. The document concludes by mentioning bitwise operators and providing contact information for the lecturer.

Uploaded by

Mʌ ɭɩĸ
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/ 20

Programming

Fundamentals
Fall 2022

Lecture 3
Dr. Farhan Aadil
[email protected]
DrAadil.com

Please turn OFF your Mobile Phones!


Operators
• What is operator?
• Type of operators
• Postfix
• Prefix
What are Operators?

• Operators are special symbols used for


– mathematical functions
– assignment statements
– logical comparisons

• Examples:
3 + 5 // uses + operator
The Operator Groups

• There are 5 different groups of operators:

– Arithmetic operators
– Assignment operator
– Increment/Decrement operators
– Relational operators
– Conditional operators
Arithmetic Operators

• Java has 6 basic arithmetic operators


+ add
- subtract
* multiply
/ divide
% modulo (remainder)
^ exponent (to the power of)
Assignment Operator

• The basic assignment operator (=) assigns the value of var to


expr
var = expr ;
• Java allows you to combine arithmetic and assignment
operators into a single operator.
• Examples:
x = x + 5; is equivalent to x += 5;
y = y * 7; is equivalent to y *= 7;
Increment/Decrement Operators

count = count + 1;
can be written as:
++count; or count ++;

++ is called the increment operator.

count = count - 1;
can be written as:
--count; or count--;

-- is called the decrement operator.


Prefix
The prefix form ++count, --count first adds 1 to the variable and then
continues to any other operator in the expression
public class Helloworld {

public static void main(String[] args) {


int ans,b=1;
ans =++b;

System.out.println(ans);
System.out.print("The value of b is "+b);
}
}
2
The value of b is 2
Relational (Comparison) Operators

• Relational operators compare two values

• Produces a Boolean value (true or false) depending on the relationship


operation is true when . . .

a > b a is greater than b


a >= b a is greater than or equal to b
a == b a is equal to b
a != b a is not equal to b
a <= b a is less than or equal to b
a < b a is less than b
Examples of Relational Operations

int x = 3;
int y = 5;
boolean result;

1) result = (x > y);


now result is assigned the value false because 3 is not greater than 5

2) result = (15 == x*y);


now result is assigned the value true because the product of 3 and 5 equals 15

3) result = (x != x*y);
now result is assigned the value true because the product of x and y (15) is not equal
to x (3)
Conditional Operators

Symbol Name

&& AND
|| OR
! NOT

• Conditional operators can be referred to as boolean operators,


because they are only used to combine expressions that have a value of
true or false.
Truth Table for Conditional Operators

x y x && y x || y !x

True True True True False

True False False True False

False True False True True

False False False False True


Using && and ||

public static void main(String[] args) {


int a=5;
int b=2;
if(a>2 || (b++ > 1))
{
System.out.print(b);
}
}
Using && and ||

public static void main(String[] args) {


int a=5;
int b=2;
if(a>2 && (b++ > 1)
{
System.out.print(b);
}
}
Short-Circuit Evaluations

Java performs short-circuit evaluation:


It evaluates && and || expressions from left to right and once it finds the result, it stops .

public static void main(String[] args) {


int a=5;
int b=2;
if(a>2 || (b++ > 1))
{
System.out.print(b);
}}
2
public static void main(String[] args) {
int a=5;
int b=2;
if(a>2 && (b++ > 1)){
System.out.print(b);
}
3
Ternary Operator
• Java ternary operator is the only conditional operator that
takes three operands.
• We can use the ternary operator in place of if-else
conditions or even switch conditions using nested ternary
operators.

• Although it follows the same algorithm as of if-else


statement, the conditional operator takes less space and
helps to write the if-else statements in the shortest way
possible.
Ternary Operator
Ternary Operator
// variable declaration
int n1 = 5, n2 = 10, max,res;

System.out.println("First num: " + n1);


System.out.println("Second num: " + n2);

// Largest among n1 and n2


max = (n1 > n2) ? n1 : n2;

res = (n1 > n2) ? (n1 + n2) : (n1 - n2);

// Print the largest number


System.out.println("Maximum is = " + max);
System.out.println("Result = " + res);

}
Assignment

• Bitwise Operator

19
Important links

• My email: [email protected]
• My web page: DrAadil.com

Thanks for listening!


Ready for Questions & Answers ☺

COMSATS University Islamabad, Attock Campus

You might also like