1. Java if Statement
The if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statements is executed otherwise not.
*Syntax: *
if(condition) {
// Statements to execute if
// condition is true
}
EXAMPLE1
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("Inside If block");
System.out.println("10 is less than 15");
System.out.println("I am Not in if");
}
}
Output:
Inside If block
10 is less than 15
I am Not in if
EXAMPLE2
package Loop;
public class ifDemo {
public static void main(String[] args) {
int num = 25;
if (num % 5 == 0) {
System.out.println("Number is divisible by 5");
}
}
}
Output:
Number is divisible by 5
EXAMPLE3:
package Loop;
public class ifDemo2 {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("Eligible to vote");
}
}
}
Output:
Eligible to vote
2.Java if-else Statement
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false? Here, comes the “else” statement. We can use the else statement with the if statement to execute a block of code when the condition is false.
*Syntax: *
if(condition){
// Executes this block if
// condition is true
}else{
// Executes this block if
// condition is false
}
EXAMPLE4:
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Ouput:
i is smaller than 15
EXAMPLE5
package Loop;
public class ifDemo3 {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}
Output:
Odd number
EXAMPLE6:
package Loop;
public class ifDemo4 {
public static void main(String[] args) {
int num = -10;
if (num >= 0) {
System.out.println("Positive number");
} else {
System.out.println("Negative number");
}
}
}
Output:
Negative number
3.Java if-else-if ladder
Here, a user can decide among multiple options.The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that ‘if’ is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. There can be as many as ‘else if’ blocks associated with one ‘if’ block but only one ‘else’ block is allowed with one ‘if’ block.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}
EXAMPLE7:
class Geeks {
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
Output:
i is 20
EXAMPLE8:
package Loop;
public class ifDemo6 {
public static void main(String[] args) {
int temperature = 35;
if (temperature <= 0) {
System.out.println("Freezing weather");
} else if (temperature <= 15) {
System.out.println("Cold weather");
} else if (temperature <= 25) {
System.out.println("Pleasant weather");
} else if (temperature <= 35) {
System.out.println("Warm weather");
} else {
System.out.println("Hot weather");
}
}
}
Output:
Warm weather
EXAMPLE9:
package Loop;
public class ifDemo7 {
public static void main(String[] args) {
int day = 3;
if (day == 1) {
System.out.println("Monday");
} else if (day == 2) {
System.out.println("Tuesday");
} else if (day == 3) {
System.out.println("Wednesday");
} else if (day == 4) {
System.out.println("Thursday");
} else if (day == 5) {
System.out.println("Friday");
} else if (day == 6) {
System.out.println("Saturday");
} else if (day == 7) {
System.out.println("Sunday");
} else {
System.out.println("Invalid day number");
}
}
}
Output:
Wednesday
4. Java nested-if Statement
A nested if is an if statement that is the target of another if or else. Nested if statements mean an if statement inside an if statement. Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.
Syntax:
if (condition1) {
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
EXAMPLE10:
class Geeks {
public static void main(String args[])
{
int i = 10;
if (i == 10 || i < 15) {
// First if statement
if (i < 15)
System.out.println("i is smaller than 15");
// Nested - if statement
// Will only be executed if statement above
// it is true
if (i < 12)
System.out.println(
"i is smaller than 12 too");
}
else {
System.out.println("i is greater than 15");
}
}
}
Output:
i is smaller than 15
i is smaller than 12 too
EXAMPLE11:
package Loop;
public class ifDemo8 {
public static void main(String[] args) {
int age = 25;
boolean hasDegree = false;
if (age >= 18) {
if (hasDegree) {
System.out.println("You are eligible to apply for the job.");
} else {
System.out.println("You must have a degree to apply for the job.");
}
} else {
System.out.println("You must be at least 18 years old to apply.");
}
}
}
Output:
You must have a degree to apply for the job
EXAMPLE12:
package Loop;
public class ifDemo9 {
public static void main(String[] args) {
int balance = 1000;
int withdrawalAmount = 500;
if (withdrawalAmount <= balance) {
if (withdrawalAmount % 100 == 0) {
System.out.println("Withdrawal successful.");
} else {
System.out.println("Amount must be in multiples of 100.");
}
} else {
System.out.println("Insufficient balance.");
}
}
}
Output:
Withdrawal successful
Example13:
package Loop;
public class ifDemo10 {
public static void main(String[] args) {
String username = "admin";
String password = "1234";
if (username.equals("admi")) {
if (password.equals("1234")) {
System.out.println("Login successful!");
} else {
System.out.println("Invalid password.");
}
} else {
System.out.println("Invalid username.");
}
}
}
Output:
Invalid username.
Reference link:
https://www.geeksforgeeks.org/decision-making-javaif-else-switch-break-continue-jump/