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) {
[Link]("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) {
[Link]("x + y is greater than 20");
} else {
[Link]("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) {
[Link]("You are an Adult");
}
else {
[Link]("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) {
[Link]("Even Number");
}
else {
[Link]("Odd Number");
}
}
}
Output:
Odd Number
public class LeapYear {
public static void main(String[] args) {
int a = 2024;
if(a%4==0) {
[Link]("Leap Year");
}
else {
[Link]("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") {
[Link]("Block is Janjgir champa");
}else if (city == "Bastar") {
[Link]("Block is Kondagaon");
}else if(city == "Raipur") {
[Link]("Block is Abhanpur");
}else {
[Link](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([Link]("India")) {
if([Link]("Mumbai")) {
[Link]("Your city is Mumbai");
}else if([Link]("Raipur")) {
[Link]("Your city is Raipur");
}else {
[Link]([Link](",")[0]);
}
}else {
[Link]("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){
[Link]("You are eligible to donate blood");
} else{
[Link]("You are not eligible to donate blood");
}
} else{
[Link]("Age must be greater than 18");
}
} }