OOP1 - Unit 2 (Amiraj) VisionPapers - in
OOP1 - Unit 2 (Amiraj) VisionPapers - in
Syntax:
if(condition){
//code to be executed
}
WORKING OF IF STATEMENT
TWO WAY IF-ELSE STATEMENT
The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is
executed.
Syntax:
if(condition)
{
//code if condition is true
}
Else
{
//code if condition is false
}
WORKING OF IF-ELSE STATEMENT
NESTED IF AND MULTI-WAY IF STATEMENTS
The nested if statement represents the if block within another if block. Here, the inner if block condition executes
only when outer if block condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
IF-ELSE STATEMENT
In Java, we have an if...else...if ladder, that can be used to execute one block of code among multiple other
blocks.
if (expression1) {
// codes
else if(expression2) {
// codes
}
else if (expression3) {
// codes
else {
// codes
}
SWITCH STATEMENT
❖ The Java switch statement executes one statement from multiple conditions. It is like if-else-if
ladder statement. The switch statement works with byte, short, int, long, enum types, String and
some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch
statement.
❖ In other words, the switch statement tests the equality of a variable against multiple values.
❖ Points to Remember
➢ There can be one or N number of case values for a switch expression.
➢ The case value must be of switch expression type only. The case value must be literal or
constant. It doesn't allow variables.
SWITCH STATEMENT
➢ The case values must be unique. In case of duplicate value, it renders compile-time error.
➢ The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums
and string.
➢ Each case statement can have a break statement which is optional. When control reaches to
the break statement, it jumps the control after the switch expression. If a break statement is
not found, it executes the next case.
➢ The case value can have a default label which is optional.
SYNTAX OF SWITCH
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
COMMON
MATHEMATICAL
FUNCTIONS
COMMON MATHEMATICAL FUNCTIONS
There are three types of mathematical functions that are commonly used
1.Trigonometric Function
2.Exponent Method
3.Service Method
TRIGONOMETRIC FUNCTION
METHOD PURPOSE
asin(radians) This function returns inverse of sine angle. The angle should
be in radian.
acos(radians) This function returns inverse of cosine angle. The angle
should be in radian.
Syntax:
while(condition){
//code to be executed
}
HOW WHILE LOOP WORKS?
❖ In the above syntax, the test expression inside parenthesis is a boolean expression. If the
test expression is evaluated to true,
➢ statements inside the while loop are executed.
➢ then, the test expression is evaluated again.This process goes on until the test
expression is evaluated to false. If the test expression is evaluated to false,
➢ the while loop is terminated.
DO-WHILE LOOP
The Java do-while loop is used to iterate a part of the program several
times. If the number of iteration is not fixed and you must have to execute
the loop at least once, it is recommended to use do-while loop.
Syntax:
do{
//code to be executed
}while(condition);
How do...while loop works?
❖ The body of do...while loop is executed once (before checking the test expression). Only
then, the test expression is checked.
❖ If the test expression is evaluated to true, codes inside the body of the loop are executed,
and the test expression is evaluated again. This process goes on until the test expression
is evaluated to false.
❖ When the test expression is false, the do..while loop terminates.
FOR LOOP
A simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It
consists of four parts:
1. Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the
variable, or we can use an already initialized variable. It is an optional condition.
2. Condition: It is the second condition which is executed each time to test the condition of the loop. It continues
execution until the condition is false. It must return boolean value either true or false. It is an optional condition.
3. Statement: The statement of the loop is executed each time until the second condition is false.
4. Increment/Decrement: It increments or decrements the variable value. It is an optional condition.
Syntax:
for(initialization;condition;incr/decr){
//statement or code to be executed
}
NESTED LOOP
If we have a for loop
inside the another loop,
it is known as nested for
loop. The inner loop
executes completely
whenever outer loop
executes.
THE BREAK AND CONTINUE
BREAK STATEMENT
The break statement in Java terminates the loop immediately, and the control of the program moves to
break;
HOW BREAK STATEMENT WORK?
NESTED BREAK & LABELED BREAK
CONTINUE STATEMENT
The continue statement in Java skips the current iteration of a loop (for, while, do...while, etc) and
the control of the program moves to the end of the loop. And, the test expression of a loop is
evaluated.
In the case of for loop, the update statement is executed before the test expression.
continue;
HOW CONTINUE STATEMENT WORK?
NESTED CONTINUE & LABELED CONTINUE