If Else

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Java Control Statements

Java provides three types of control flow statements.

1. Decision Making statements


if statements
switch statement
2. Loop statements
do while loop
while loop
for loop
for-each loop
3. Jump statements
break statement
continue statement
-------------------------------------------------------------
1) If Statement:
Simple if statement
if-else statement
if-else-if ladder
Nested if-statement

Simple if statement :-
if(condition) {
statement 1; //executes when condition is true
}

public class Student {


public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20");
}
}
}
Output:
x + y is greater than 20

-----------------------------------------------
if-else statement :-
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}

public class Student {


public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 20) {
System.out.println("x + y is greater than 20");
} else {
System.out.println("x + y is less than 20");
}
}
}
Output:
x + y is less than 20

public class Student {


public static void main(String[] args) {
int age = 25;
if (age >= 18) {
System.out.println("You are an Adult");
}
else {
System.out.println("You are Not an Adult");
}
}
}
Output:
You are an Adult

public class EvenOdd {


public static void main(String[] args) {
int a = 13;
if(a%2==0) {
System.out.println("Even Number");
}
else {
System.out.println("Odd Number");
}
}
}
Output:
Odd Number

public class LeapYear {


public static void main(String[] args) {
int a = 2024;
if(a%4==0) {
System.out.println("Leap Year");
}
else {
System.out.println("Not Leap Year");
}
}
}
Output:
Leap Year

---------------------------------------------------------
if-else-if ladder:-
if(condition 1) {
statement 1; //executes when condition 1 is true
}
else if(condition 2) {
statement 2; //executes when condition 2 is true
}
else {
statement 2; //executes when all the conditions are false
}
public class Student {
public static void main(String[] args) {
String city = "Raipur";
if(city == "Bilaspur") {
System.out.println("Block is Janjgir champa");
}else if (city == "Bastar") {
System.out.println("Block is Kondagaon");
}else if(city == "Raipur") {
System.out.println("Block is Abhanpur");
}else {
System.out.println(city);
}
}
}
Output:
Block is Abhanpur
------------------------------------------------

Nested if-statement :-
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
}
else{
statement 2; //executes when condition 2 is false
}
}

public class Student {


public static void main(String[] args) {
String address = "Raipur, India";

if(address.endsWith("India")) {
if(address.contains("Mumbai")) {
System.out.println("Your city is Mumbai");
}else if(address.contains("Raipur")) {
System.out.println("Your city is Raipur");
}else {
System.out.println(address.split(",")[0]);
}
}else {
System.out.println("You are not living in India");
}
}
}
Output:
Your city is Raipur

//Java Program to demonstrate the use of Nested If Statement.


public class JavaNestedIfExample2 {
public static void main(String[] args) {
//Creating two variables for age and weight
int age=25;
int weight=48;
//applying condition on age and weight
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
} else{
System.out.println("You are not eligible to donate blood");
}
} else{
System.out.println("Age must be greater than 18");
}
} }

You might also like