0% found this document useful (0 votes)
23 views

OOP1 Unit2

Uploaded by

makkakuldip
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

OOP1 Unit2

Uploaded by

makkakuldip
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Object Oriented Programming 1

Unit -2
Selections, Mathematical Functions
and Loops

OOP1 - 3140705
Conditional statements
Java has the following conditional statements:

• Use if to specify a block of code to be executed, if


a specified condition is true
• Use else to specify a block of code to be
executed, if the same condition is false
• Use else if to specify a new condition to test, if
the first condition is false
• Use switch to specify many alternative blocks of
code to be executed
• The Java if statement is used to test the condition. It
checks boolean condition: true or false. There are
various types of if statement in Java.

• if statement
• if-else statement
• if-else-if ladder
• nested if statement
The if Statement

if (condition)
{
// block of code to be executed if the condition is
true
}
class IfExample
{
public static void main(String[] args)
{
//defining an 'age' variable
int age=20;
//checking the age
if(age>18)
{
System.out.print("Age is greater than 18");
}
}
}
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
}
//A Java Program to demonstrate the use of if-else statement.
//It is a program of odd and even number.

class IfElseExample
{
public static void main(String[] args)
{
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0)
{
System.out.println("even number");
}
else
{
System.out.println("odd number");
}
}
}
if-else-if ladder Statement
The if-else-if ladder statement executes one condition from multiple statements.

Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
else
{
//code to be executed if all the conditions are false
}
Program to check POSITIVE, NEGATIVE or ZERO:

class PositiveNegativeExample
{
public static void main(String[] args)
{
int number=-13;
if(number>0)
{
System.out.println("POSITIVE");
}
else if(number<0)
{
System.out.println("NEGATIVE");
}
else
{
System.out.println("ZERO");
}
}
}
Nested if statement
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
}
}
//Java Program to demonstrate the use of Nested If Statement.

class JavaNestedIfExample
{
public static void main(String[] args)
{
//Creating two variables for age and weight
int age=20;
int weight=80;

if(age>=18)
{
if(weight>50)
{
System.out.println("You are eligible to donate blood");
}
}
}
}
Switch statement
• The Java switch statement executes one
statement from multiple conditions.
• It is like if-else-if ladder statement.
• In other words, the switch statement tests the
equality of a variable against multiple values.
Syntax:

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;
}
• The expression is evaluated once and compared
with the values of each case.

• If expression matches with value1, the code of


case value1 are executed. Similarly, the code of
case value2 is executed if expression matches
with value2.

• If there is no match, the code of the default case


is executed.
class Main
{
public static void main(String[] args)
{
int day = 4;
switch (day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println(“Wrong Choice");
break;

}
}
}
Loops
• Loops in Java come into use when we need to repeatedly execute
a block of statements.

• Loops can execute a block of code as long as a specified condition


is reached.

Java while Loop


• Java while loop is a control flow statement that allows code to be
executed repeatedly based on a given boolean condition.

• The while loop loops through a block of code as long as a specified


condition is true:
Syntax

while (condition)
{
// code block to be executed
}

class Main
{
public static void main(String[] args)
{
int i = 0;
while (i < 5)
{
System.out.println(i);
i++;
}
}
}
The do…while Loop

• The do/while loop is a variant of the while loop.


• This loop will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the
condition is true.

Syntax
do
{
// code block to be executed
}
while (condition);
• The example below uses a do/while loop.
• The loop will always be executed at least once, even if the condition is
false, because the code block is executed before the condition is tested:

class Main
{
public static void main(String[] args)
{
int i = 0;
do
{
System.out.println(i);
i++;
}
while (i < 5);
}
}
for loop
• When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:

Syntax

for(init;condition;incr/decr)
{
// code to be executed
}

init is executed (one time) before the execution of the code block.

condition defines the condition for executing the code block.

inc/dec is executed (every time) after the code block has been executed.
class Main
{
public static void main(String[] args)
{
for (int i = 0; i < 5; i++)
{
System.out.println(i);
}
}
}
class Main
{
public static void main(String[] args)
{
for (int i = 0; i <= 10; i = i + 2)
{
System.out.println(i);
}
}
}
break statement in java
• The break statement in Java programming language has the
following two usages −

• When the break statement is encountered inside a loop,


the loop is immediately terminated and the program
control resumes at the next statement following the loop.

• It can be used to terminate a case in the switch statement.

Syntax
The syntax of a break is a single statement inside any loop −

break;
class Main
{
public static void main(String[] args)
{
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
System.out.println(i);
}
}
}
class BreakExample2
{
public static void main(String[] args)
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
if(i==2&&j==2)
{
break;
}
System.out.println(i+" "+j);
}
}
}
}
Output:
11
12
13
21
31
32
33
continue statement in java
• The Java continue statement is used to continue the
loop. It continues the current flow of the program
and skips the remaining code at the specified
condition.

• The continue keyword can be used in any of the loop


control structures. It causes the loop to immediately
jump to the next iteration of the loop.

• In a for loop, the continue keyword causes control to


immediately jump to the update statement.

• In a while loop or do/while loop, control immediately


jumps to the Boolean expression.
Example
class Main
{
public static void main(String[] args)
{
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
System.out.println(i);
}
}
}
class Main
{
public static void main(String[] args)
{
int i = 0;
while (i < 10)
{
if (i == 4)
{
i++;
continue;
}
System.out.println(i);
i++;
}
}
}

You might also like