Java Programming 18CS653-Module-2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 79

PROGRAMMING IN JAVA

18CS653
MODULE-2
OPERATORS & CONTROL
STATEMENTS
TOPICS
2.1 Operators
2.2 Arithmetic Operators
2.2.1 The Basic Arithmetic Operators
2.2.2 The Modulus Operator
2.2.3 Arithmetic Compound Assignment Operators
2.2.4 Increment and Decrement
2.3 The Bitwise Operators .
2.3.1 The Bitwise Logical Operators
2.3.2 The Left Shift
2.3.3The Right Shift
2.3.4 The Unsigned Right Shift
2.3.5 Bitwise Operator Compound Assignments
2.4 Relational Operators
2.5 Boolean Logical Operators
2.6 Short-Circuit Logical Operators
2.7 The Assignment Operator
2.8 The ? Operator
2.9 Operator Precedence
2.10 Using Parentheses
TOPICS
2.11 Control Statements
2.12 Java’s Selection Statements
2.12.1 if
2.12.2 switch
2.13 Iteration Statements
2.13.1 while .
2.13.2 do-while
2.13.3 for
2.13.4 The For-Each Version of the for Loop
2.13.5 Nested Loops
2.14 Jump Statements
2.14.1. Using break
2.14.2 Using continue
2.14.3 return
2.1 OPERATORS
Java operators are symbols that are used to perform operations on variables
and manipulate the values of the operands.
 Each operator performs specific operations.
Example consider an expression 5 + 1 = 6; here, 5 and 1 are operands, and
the symbol + (plus) is called the operator.
Java provides many types of operators which can be used according to the
need. They are classified based on the functionality they provide. Some of the
types are:
2.2 ARITHMETIC OPERATORS
Arithmetic operators are used in mathematical expressions.
The following table lists the arithmetic operators:
2.2.1 BASIC ARITHMETIC OPERATORS
The basic arithmetic operations—addition, subtraction, multiplication, and
division and Modulo.
The Addition operator performs addition between two entities on either side
of the operator.
The Multiplication operator performs multiplication between two entities
on either side of the operator.
The Subtraction operator performs subtraction between two entities on
either side of the operator.
The Division operator performs division and returns the quotient value of
the division.
The Modulo operator returns the remainder after dividing the two operands.
EXAMPLE OF BASIC ARITHMETIC OPERATORS
2.2.2 MODULUS OPERATOR
The modulus operator, %, returns the remainder of a division operation. It
can be applied to floating-point types as well as integer types.
Example of Modulus Operator:
2.2.3 ARITHMETIC COMPOUND ASSIGNMENT
OPERATORS
Arithmetic compound assignment operators are those operators where
arithmetic operators are combined with assignment operators and used.
Example: consider the statement
a= a+4; // increment by 4
We can rewrite the above statement as
a+= 4; // increment by 4
Both the statements perform the same operations. They increase the value of a
by 4.
Example:
a= a%2;
Can be rewritten as
a%=2;
Example
2.2.4 INCREMENT AND DECREMENT OPERATORS
Increment Operator: The increment operator ++ increments the value of
its operand by 1.
Example: the statement
x = x+1;
can be rewritten as
x++;
Decrement Operator: The decrement operator -- deccrements the value
of its operand by 1.
Example: the statement
x = x-1;
can be rewritten as
x--;
INCREMENT OPERATOR
Increment operators are classified as
 pre increment
 post increment
Pre increment: In Pre increment the value of the operand is first
incremented by 1 and then used
Syntax: ++operand;
Example: int a = 5;
++a;
Post increment: In Post increment the value of the operand is first used and
the incremented by 1 .
Syntax: operand++;
Example: int a = 5;
a++;
INCREMENT OPERATORS EXAMPLE
DECREMENT OPERATOR
Decrement operators are classified as
 pre decrement
 post decrement
Pre decrement: In Pre decrement the value of the operand is first
decremented by 1 and then used
Syntax: - -operand;
Example: int a = 5;
--a;
Post decrement: In Post decrement the value of the operand is first used and
the decremented by 1 .
Syntax: operand--;
Example: int a = 5;
a--;
DECREMENT OPERATORS EXAMPLE
2.3 THE BITWISE OPERATORS
Bitwise Operators can be applied to integer data types such as byte, short,in,
long and char.
Bitwise operators act upon the individual bits of the operands.
Below table shows the various bitwise operators.
2.3.1 THE BITWISE LOGICAL OPERATORS
The bitwise logical operators are &, |,^ and ~.
Bitwise operators are applied to individual bits of each operand.
The below table shows the outcome of each operation.

Bitwise Not Operator:


Bitwise not operator is also called as complement operator.
Bitwise not operator is an unary operator.
Bitwise not operator inverts all the bits of the operands.
Bitwise not operator is denoted by ~ ( tilde symbol).
Example of Bitwise NOT Operator.
The number 42 who's bit pattern is 00101010 becomes 11010101 when
bitwise not operator is applied.

Bitwise AND Operator:


Bitwise AND operator is a binary operator applied on two operands.
Bitwise AND operator is denoted by &.
Bitwise AND operator produce the result 1 if all the bits of the operands are 1.
results 0 in all other cases.

Example a= 00101010 //42


b = 00001111 // 15
-------------------------------------------------------
a & b =00001010 // 10
Bitwise OR Operator:
Bitwise OR operator is a binary operator applied on two operands.
Bitwise OR operator is denoted by |.
Bitwise OR operator produce the result 1 if either the bits of the operands are
1. results 0 in all other cases.

Example a= 00101010 // 42
b =00001111 // 15
-------------------------------------------------------
a | b = 00101111 // 47
Bitwise XOR Operator:
Bitwise XOR operator is a binary operator applied on two operands.
Bitwise XOR operator is denoted by ^.
Bitwise XOR operator produce the result 0 if the bits of the operands are
same. results 1 in all other cases.

Example a= 00101010 // 42
b =00001111 // 15
-------------------------------------------------------
a ^ b = 00100101 // 37
Example of the Bitwise Operators:
2.3.2 THE LEFT SHIFT
The left shift operator, <<, shifts all of the bits in a value to the left a
specified number of times.
It has this general form:
value << num
Here, num specifies the number of positions to left-shift the value in value.
That is, the << moves all of the bits in the specified value to the left by the
number of bit positions specified by num.

Note: The byte and short when shifted the result will be in int. so if you want
the result in same type then type casting should be done.
EXAMPLE OF LEFT SHIFT
2.3.3 THE RIGHT SHIFT
The right shift operator, >>, shifts all of the bits in a value to the right a
specified number of times.
It has this general form:
value >> num
Here, num specifies the number of positions to right-shift the value in value.
That is, the >> moves all of the bits in the specified value to the right by the
number of bit positions specified by num.

Example: int a = 32; // 00100000


a = a>> 2 ; // now a contains 8

Example : int a = 35; // 00100011


a = a>>2 ; // still now a contains 8
Example: int a = - 8; // 11111000
a = a>> 1 ; // now a contains -4

Example : int a = - 8; // 11111000


a = a>>2 ; // still now a contains 0
2.3.4 UNSIGNED RIGHT SHIFT
The unsigned right shift operator, >>>, shifts all of the bits in a value to the
right a specified number of times and shift higher order bits to zero no
matter what ever is the initial value.
It has this general form:
value >>> num
Here, num specifies the number of positions to right-shift the value in value.
That is, the >>> moves all of the bits in the specified value to the right by the
number of bit positions specified by num and substitute zeros at higher order.

Example : int a = 35; // 00100011


a = a>>2 ; // still now a contains 8
Example: int a = 8; // 00000000000000000000000000001000

a = a>>> 24 ; // now a contains 0

Example: int a = - 8; // 11111111111111111111111111111000


a = a>>>24 ; // now a contains 255
2.3.5 BITWISE OPERATOR COMPOUND ASSIGNMENTS

All of the binary bitwise operators have a compound form similar to that of
the algebraic operators.
Example, the following two statements, which shift the value in a right by
four bits, are equivalent:
a = a >> 4;
is same as
a >>= 4;
Example , the two statements, which perform the OR operations, are
equivalent.
a = a | b;
is same as
a | = b;
EXAMPLE OF BITWISE OPERATOR COMPOUND
ASSIGNMENTS
2.4 RELATIONAL OPERATORS

The relational operators determine the relationship that one operand has to
the other.
The outcome/ Result of these operations is a boolean value.
Relational Operators are most frequently used in expression to control if
and various loops.
The relational operators are shown here:
You can use Relational operators with the data types like char,
boolean and number data types like byte, short, int, long, float and
double.
Example 1:
Example 2:
Example 3:
2.5 BOOLEAN LOGICAL OPERATORS

The boolean logical operators operates on the boolean data type.


 Some logical operators work with a single Operand while others work with
two Operands.
The below table shows the various boolean logical Operators
The below table shows the effect of each boolean logical operator.

The logical Boolean operators, &, |, and ^, operate on boolean values in


the same way that they operate on the bits of an integer.
The logical ! operator inverts the Boolean state:
!true == false and !false == true.
Example 1 of Boolean Logical Operators:
Example 2 of Boolean Logical Operators:
2.6 SHORT CIRCUIT LOGICAL OPERATORS

The short circuit logical operators are AND (&&) and OR( ||).
These are the two interesting boolean operators that are not found in other
programming language.
These are secondary versions of the Boolean AND and OR operators, and
are known as short-circuit logical operators.
 OR operator results in true when A is true, no matter what B is.
 AND operator results in false when A is false, no matter what B is.
The operators && and || are used in the evaluation of expression .
Example 1 of Short Circuit Logical Operators:

Example 2 of Short Circuit Logical Operators:


2.7 ASSIGNMENT OPERATOR

Assignment operator is a single equal sign (=).


Assignment Operator is used to assign the value to the variable.
General Syntax:
variable = expression;
The type of variable must be compatible with the expression.
Example: int a;
a = 10;
In java we can use assignment operator for multiple assignments.
General Syntax:
var1 = var2 = varn =expression;
Example: int x,y,z;
x = y = z =100; // sets x,y and z to 100
2.8 The ? OPERATOR (Ternary Operator)

The ternary (three-way ) Operator replaces some if-then else statement.


General Syntax:
expression1 ? expression2 : expression3

Here, expression1 can be any expression that evaluates to a boolean value. If


expression1 is true, then expression2 is evaluated; otherwise, expression3 is
evaluated.
The result of the ? operation is that of the expression evaluated. Both
expression2 and expression3 are required to return the same type, which can’t
be void.
Example 1 Ternary Operator:

Example using if - else


Example 2 Ternary Operator:

Example 2 using if-else


2.9 OPERATOR PRECEDENCE
Operator precedence determines the order in which the operators in an
expression are evaluated.
Example: consider the expression
int myvalue = 12- 4 * 2 ;
Here, * is given the highest preference then - , so first 4*2 is evaluated to 8.
then 12-8 is evaluated to 4. therefore the myvalue is 4.

Example: consider the expression


int myvalue = 12- 4 * 9/2 ;
Here, * and / are given the highest preference then - , Since * and / have same
precedence the order of evaluation takes from left to right. So first 4*9 is
evaluated to 36 then 36/2 is evaluated to 18 and last 12 – 18 is evaluated to
-6.
OPERATOR PRECEDENCE TABLE

The below table shows the precedence of the operator from Highest to Lowest
2.10 USING PARENTHESES

Parentheses raise the precedence of the operations that are inside them.
Parentheses are often necessary to obtain the desired result.
For example, consider the following expression:
a >> b + 3
This expression first adds 3 to b and then shifts a right by that result. That is,
this expression can be rewritten using redundant parentheses like this:

a >> (b + 3)

However, if you want to first shift a right by b positions and then add 3 to
that result, you will need to parenthesize the expression like this:

(a >> b) + 3
2.11 CONTROL STATMENTS

Control statements to are used in Programming Language to control the


flow of execution of a program based on certain conditions. These are used to
cause the flow of execution to advance and branch based on changes to the
state of a program.
Java Provides different types of Control Statements they are:
 Selection Statements
 Iteration Statements
 Jump Statements
2.12 SELECTION STATMENTS

Java provides following different selection statements.

 if
 if-else
 nested-if
 if-else-if
 switch-case

If Statement: if statement is the most simple decision-making statement.


It is used to decide whether a certain statement or block of statements will
be executed or not i.e if a certain condition is true then a block of
statements is executed otherwise not.
Syntax for if Statement:

if(condition) {
statement1;
statement2;
}

Here the condition is evaluated. If the condition is evaluated to true then the
statement1 and statement2 enclosed in { and } brackets are executed. Suppose
if the { and } are not enclose then only the statement1 is executed if the
condition is true. Then followed by statement2 is executed.

if(condition)
statement1; // statement1 is part of if condition
statement2; // gets executed always as it is not part of if condition.
Example1 for if condition:

Example2 for if condition:

.
if-else Statement
We know that if tells us that statements are executed only if condition is true.
What if the condition is false so we use if-else statement.
Syntax:

if(condition)
{
// Execute this block;
// if the condition of if is true
}
else
{
// Execute this block;
// if the condition of if is false
}
Here the condition is evaluated. If the condition is evaluated to true then the
statements inside { and } brackets following if are executed else the
statements inside { and } brackets following else are executed.
Example1 for if –else condition:

.
Nested if Statement
Nested if statement is an if statement inside the if statement.
Syntax:

if(condition1)
{
// execute statements if condition1 is true

if( condition2)
{
// execute statements if condition1 is true
}
}
Here the condition1 is evaluated. If the condition1 is true then the statements
following condition1 are executed. Then condition2 is evaluated if condition2
is true then statement or set of statements following condition2 are executed.
Example for Nested if :

.
if-else- if Statement/ladder
In if-else-if ladder, a user can decide among multiple options.
The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement
associated with that ‘if’ is executed, and the rest of the ladder is bypassed.
If none of the conditions is true, then the final else statement will be
executed.
There can be as many as ‘else if’ blocks associated with one ‘if’ block but
only one ‘else’ block is allowed with one ‘if’ block.
Syntax if-else- if Statement/ladder
if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else if (conditionN)
statementN;
..
else
statement;
Example for if-else-if ladder :

.
2.12.2 Switch Case
Switch statement is a multiway branch statement. It provides an easy way to
dispatch execution to different parts of code based on the value of the expression.
Syntax: switch (expression)
{
case value1: statement1;
break;
case value2: statement2;
break;
..
case valueN: statementN;
break;
default: statementDefault;
}
value(s): can be integer or character
Example : Switch Case
Example : Switch Case
2.13 Iteration Statement
Java Iteration statements are for, while and do-while.
Iteration statements create loops.
A loop repeatedly executes the same set of instructions until a termination
condition is met.

2.13.1 While loop


The while loop is the most fundamental looping statement . It repeats the
statement or block while its controlling expression is true.
Here is its general form:

while(condition) {
// body of loop
}
The condition can be any Boolean expression. The body of the loop will be
executed as long as the conditional expression is true. When condition becomes
false, control passes to the next line of code immediately following the loop. The
curly braces are unnecessary if only a single statement is being repeated.
2.13.2 Do-While loop
The do-while loop always executes its body at least once, because its
conditional expression is at the bottom of the loop.
Its general form is

do {
// body of loop
} while (condition);

Each iteration of the do-while loop first executes the body of the loop and then
evaluates the conditional expression. If this expression is true, the loop will
repeat. Otherwise, the loop terminates. As with all of Java’s loops, condition
must be a Boolean expression.
Example of Do-While loop
2.13.3 for loop
for loop is used to execute the set of statement when we know the number of
iterations to be executed well in advance.
General Syntax:
for ( initialization ; condition ; iteration )
{
statements;
}
How for loop executes:
When the loop first starts, the initialization portion of the loop is executed.
Next, condition is evaluated. If it is false, the loop terminates.
Next, the iteration portion of the loop is executed. This is usually an expression
that increments or decrements the loop control variable.
Example of for loop
Example of for loop
2.13.4 Enhanced for loop
Beginning with JDK 5, a second form of for was defined that implements a
“for-each” style loop.
A for-each style loop is designed to cycle through a collection of objects, such
as an array, in strictly sequential fashion, from start to finish.
The advantage of this approach is that no new keyword is required, and
no
preexisting code is broken.
The for-each style of for is also referred to as the enhanced for loop.

The general form of the for-each version of the for is shown here:

for(type itr-var : collection)


statement-block.
Example of for-each loop
Example of for-each loop
2.13.5 Nested loop
Loop inside another another loop is called Nested Loop.
2.14 Jump Statements
Java supports following different types of jump statements.
break
continue
return.
These statements transfer the control to other part of the program.

2.14.1: break statement:


In java the break has three different uses:
First, it terminates a statement sequence in a switch statement.
Second, it can be used to exit a loop.
Third, it can be used as a “civilized” form of goto.
When used inside a set of nested loops, the break statement will only break
out of the innermost loop.
go-to form :
Java does not have a goto statement because it provides a way to branch in an
arbitrary and unstructured manner.
There are, however, a few places where the goto is a valuable and legitimate
construct for flow control.
For example, the goto can be useful when you are exiting from a deeply nested
set of loops.

The general form of the labeled break statement is shown here:


break label;
2.14.2 continue Statement
The continue statement breaks one iteration (in the loop), if a specified condition
occurs, and continues with the next iteration in the loop.
2.14.3 return Statement
The return statement is used to return some value from the method and transfer
the control back to the calling function.
THANK YOU

You might also like