0% found this document useful (0 votes)
22 views48 pages

OOP1 - Unit 2 (Amiraj) VisionPapers - in

Chapter 2 covers selections, mathematical functions, and loops in Java programming. It explains various control flow statements such as if, if-else, switch, and different types of loops including while, do-while, and for loops, along with their syntax and usage. Additionally, it details common mathematical functions used in Java, including trigonometric, exponent, and service methods.

Uploaded by

Arya Desai
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)
22 views48 pages

OOP1 - Unit 2 (Amiraj) VisionPapers - in

Chapter 2 covers selections, mathematical functions, and loops in Java programming. It explains various control flow statements such as if, if-else, switch, and different types of loops including while, do-while, and for loops, along with their syntax and usage. Additionally, it details common mathematical functions used in Java, including trigonometric, exponent, and service methods.

Uploaded by

Arya Desai
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/ 48

CHAPTER 2

SELECTION,MATHEMATICAL FUNCTIONS AND LOOPS

SUBJECT:OOP-I PREPARED BY:


CODE:3140705 ASST.PROF.NENSI KANSAGARA
(CSE DEPARTMENT,ACET)
SELECTIONS
IF STATEMENTS

The Java if statement tests the condition. It


executes the if block if condition is true.

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

sin(radians) This function returns sine of an angle. The angle should be in


radian.

cos(radians) This function returns cosine of an angle. The angle should be


in radian.

tan(radians) This function returns tan of an angle. The angle should be in


radian.

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.

atan(radians) This function returns inverse of tan angle. The angle


should be in radian.

toRadians(degree) This function converts the angle in degrees to angle in


radian

toDegree(radian) This function converts the angle in radian to angle in


degrees
EXPONENT METHOD
METHOD DESCRIPTION EXAMPLE

exp(x) It returns value of e^x Math.exp(1) returns


2.71828

log(x) It returns natural logarithm of x i.e Math.log(Math.E) returns


log(x) 1.0

log10(x) It returns logarithm of x i.e to the base Math.log10(10) returns 1.0


10

pow(x,y) It returns x^y Math.pow(2,3) returns 8

sqrt(x) It returns the square root of number x Math.sqrt(25) returns 5


SERVICE METHOD
METHOD DESCRIPTION EXAMPLE

ceil(x) Returns the smallest integer that is ceil(84.6)=85.0 or


greater than or equal to the argument ceil(0.45)=1.0

floor(x) Returns the smallest integer that is floor(84.6)=84.0 or


less than or equal to the argument floor(0.45)=0.0

round(x) Returns the closed int or long round(84.6)=85

Max(x,y) Returns the maximum two numbers max(2,3)=returns 3


x and y

min(x,y) Returns the minimum two numbers x min(2,3)=returns 2


and y

abs(x) Returns the absoulte value abs(-2) gives 2


LOOPS
WHILE LOOP

The Java while loop is used to iterate a part of the program


several times. If the number of iteration is not fixed, it is
recommended to use while loop.

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.

The Java do-while loop is executed at least once because condition is


checked after loop body.

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

the next statement following the loop.

It is almost always used with decision-making statements (Java if...else Statement).

Here is the syntax of the break statement in Java:

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.

The continue statement is almost always used in decision-making statements (if...else


Statement). It's syntax is:

continue;
HOW CONTINUE STATEMENT WORK?
NESTED CONTINUE & LABELED CONTINUE

You might also like