Java Programming 18CS653-Module-2
Java Programming 18CS653-Module-2
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.
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.
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 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:
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
if
if-else
nested-if
if-else-if
switch-case
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:
.
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.
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: