OOP1 Unit2
OOP1 Unit2
Unit -2
Selections, Mathematical Functions
and Loops
OOP1 - 3140705
Conditional statements
Java has the following conditional statements:
• 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
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.
}
}
}
Loops
• Loops in Java come into use when we need to repeatedly execute
a block of statements.
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
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.
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 −
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.