Operators&FlowControls
Operators&FlowControls
JAVA
Operators & Flow Controls
by
M. S . B. KASYAPA
Assistant Professor
Operators
An operator is a symbol used to perform arithmetic
and logical operations. Java provides a rich set of
operators. Following are different operator types.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Conditional operators
Bitwise operators
Special operators
Operators
Arithmetic Operators
These operators are used to performing basic mathematical
operations like addition, subtraction, multiplication, etc.,
Operator Meaning Example
+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division 10 / 5 = 2
% Modulus - Remainder of the Division 5 % 2 = 1
++ Increment a++
-- Decrement a--
Operators
Arithmetic Operators
The addition operator can be used with numerical data types
and character or string data type.
Ex: Numerical data 2+3 = 5 (Mathematical Operation)
non numerical data ‘o’+’k’ = ‘ok’ (Concatination Operation)
The increment and decrement operators are used as pre-
increment or pre-decrement and post-increment or post-
decrement.
When they are used as pre, the value is get modified before it is
used in the actual expression and when it is used as post, the
value is get modified after the actual expression evaluation
Operators
Sample Arithmetic Operators Program
Operators
Arithmetic Operators Output
Operators
Relational Operators
The relational operators are the symbols that are used to
compare two values.
So the relational operators are used to check the relationship
between two values.
Every relational operator has two possible results either TRUE
or FALSE.
In simple words, the relational operators are used to define
conditions in a program.
These operators are <, >, <=, >=, ==, !=
Operators
Relational Operators
Operator Meaning Example
< Returns TRUE if the first value is smaller than second value 10 < 5 is FALSE
otherwise returns FALSE
> Returns TRUE if the first value is larger than second value otherwise 10 > 5 is TRUE
returns FALSE
<= Returns TRUE if the first value is smaller than or equal to second 10 <= 5 is FALSE
value otherwise returns FALSE
>= Returns TRUE if the first value is larger than or equal to second value 10 >= 5 is TRUE
otherwise returns FALSE
== Returns TRUE if both values are equal otherwise returns FALSE 10 == 5 is FALSE
!= Returns TRUE if both values are not equal otherwise returns FALSE 10 != 5 is TRUE
Operators
Sample Relational Operators Program
Operators
Relational Operators Program Output
Operators
Logical Operators
Logical operators are used to perform logical “AND”, “OR” and
“NOT” operations.
They are used to combine two or more conditions/constraints
assume x=5,y=2,a=2
= Assigns the value of the right operand to the left operand y=x ; x=5,y=5
+= Adds the values of the left and right operands and assigns the y+=x ; x=5,y=7
result to the left operand
-= Subtracts the value of the right operand from the left operand and Y-=x ; x=5,y=-3
assigns the result to the left operand
Operators
Conditional Operators
conditional operators check the condition and decides the
desired result on the basis of both conditions.
ternary operator (? :) is the example for this operator.
It is used to evaluate Boolean expressions.
variable = (condition) ? expression1 : expression2
If condition true expression 1 will be executed but if False
Expression 2 will execute.
Operators
Sample Program
Operators
Program Output
Operators
Bitwise Operators
Bitwise Operators are used to perform the manipulation of
individual bits of a number and with any of the integer types.
new used to create new instances of objects. When the new operator is Int arr=new int[5]
used, it allocates memory for the object and returns a reference to
that memory.
. The dot operator is used to access the members of a class, such as arr.length()
methods, variables, and nested classes.
Expressions
In any programming language, expression is a
collection of operators and operands that represents
a specific value.
Operator is a symbol that performs tasks like
arithmetic operations and Operands are the values
on which the operators perform the task.
Operand can be a direct value or variable or address
of memory location.
Expressions
In the java programming language, expressions are
divided into THREE types based on the operator
position in the expression. They are as follows.
Infix Expression
Postfix Expression
Prefix Expression
In Infix Expression operator is used between
operands
Expressions
In Postfix Expression operator is used after operands
If in if So Nested if
If – else-if So Ladder if
Java Flow Control
Program Output
Java Flow Control
Switch statement
Using the switch statement, one can select only one
option from more number of options very easily.
We provide a value that is to be compared with a
value associated with each option.
Whenever the given value matches the value
associated with an option, the execution starts from
that option.
In the switch statement, every option is defined as a
case.
Java Flow Control
switch Program Structure
switch(option)
{
case 1:
Statements to Execute;
break;
--------
case n:
Statements to Execute;
break;
}
Java Flow Control
switch Program
Java Flow Control
Iterative Statements
The iterative statements are also known as looping
statements or repetitive statements..
These are used to execute a statement or a block of
statements repeatedly as long as the given condition
is true.
Java provides the following iterative statements.
while statement
do-while statement
for statement
for-each statement
Java Flow Control
While Statement
It is used to execute a single statement or block of
statements repeatedly as long as the given condition
is TRUE.
The while statement is also known as Entry control
looping statement.
while(Condition)
{
Statements to Execute
}
Java Flow Control
while Program
Java Flow Control
Do-While Statement
It is also used to execute a single statement or block
of statements repeatedly as long as given the
condition is TRUE.
The do-while statement is also known as the Exit
control looping statement
do
{
Statements to Execute
} while(Condition);
Java Flow Control
Do-while Program
Java Flow Control
While vs Do-While Statement
Feature while Loop do-while Loop
Condition Before execution of the loop After execution of the
Check body loop body
Executes at least once,
Execution May not execute at all if the
even if the condition is
Guarantee condition is false initially
false initially
When we don’t want to run When we must execute
Use Case the loop if the condition is the loop body at least
false from the start once
while(condition) do { // loop body }
Syntax
{ // loop body } while(condition);
Java Flow Control
While vs Do-while Program
Java Flow Control
For Statement
It is works same as while, do-while.
In while or do-while only condition is mentioned
there is no Initialization and updating statement for
exiting the loop.
in other hand for-statement contains initialization
statement, condition, modification statement.
First it starts with initialization then checks the
condition if condition satisfies executes for block
statements then modification statement executes.
Java Flow Control
For Statement
Later it will go to condition and if it satisfies block
will be executed and then modification.
This process continues until condition satisfies.
Initialization executes at starting only remaining will
be executed until condition satisfies.
In most of the cases modification statements will be increment
or decrement.
for(initialization, Condition, Modification)
{
Statements to Execute
}
Java Flow Control
Foreach loop Program
Java Flow Control
For each Statement
The Java for-each statement was introduced since
Java 5.0 version.
It provides an approach to traverse through an
array or collection in Java.
It is also known as enhanced for statement.
It executes the block of statements for each element
of the given array or collection.
In for-each statement, we can not skip any element
of given array or collection.
Java Flow Control
For each Statement
Syntax:
for(datatype variable_name : array)
{
Statements to Execute
}
Example:
int arr={10, 20, 30, 40};
for(int a : arr)
{
System.out.println(“Sqare of elemnt is :”+(a*a));
}
Next Lecture on
Elements of Java
by
M. S . B. KASYAPA
Assistant Professor