Java if-else

If-Else Statement in Java

In Java programming, the if statement is used to test a condition. There are various types of if statements in Java:
    ● if statement
    ● if-else statement
    ● if-else-if ladder statement

If Statement

Basic Structure:

if (condition) {
    // code for execution
}

Scenario 1: Write a Java program that checks if a person is eligible to create a bank account based on their age. If the age is greater than or equal to 18, it should print “You can create your BankAccount” to the console.

public class IfExample {
    public static void main(String[] args) {
        int age = 20;

        if (age >= 18) {
            System.out.println(“You can create your BankAccount”);
        }
    }
}

Output:
You can create your BankAccount

Code Explanation

public class IfExample

It defines a new class named IfExample.

    public static void main(String[] args)

This is the entry point of the program.

        int age = 20;

It declares a variable named age of type int and assigns it the value 20.

        if (age >= 18)

If statement checks if the value of the age variable is greater than or equal to 18. If the condition is true, the code inside the if block will be executed; otherwise, it will not print anything to the console.

System.out.println(“You can create your BankAccount”);

The code inside the if block. If the condition (age >= 18) is true, this line will be executed. It prints the message "You can create your BankAccount" to the console.

Output:
You can create your BankAccount

If-Else Statement in Java

Basic Structure:

if (condition) {
    // code for execution
} else { // previous if condition not met then we will execute else block
    // code execution
}

Scenario 2: Write a Java program that checks if a person is eligible to create a bank account based on their age. The program should print a different message if the person is under 18.
    ● If the condition is true, print “You can create your BankAccount” to the console.
    ● If the condition is false, print “You can’t create your BankAccount, your age is less than 18” to the console.

public class IfElseExample {
    public static void main(String[] args) {
        int age = 11;
        if (age >= 18) {
            System.out.println(“You can create your BankAccount”);
        } else {
            System.out.println(“You can’t create your BankAccount, your age is less than 18”);
        }
    }
}
// Output: You can’t create your BankAccount, your age is less than 18

Code Explanation

public class IfElseExample

This line defines a new class named IfElseExample.

public static void main(String[] args)

This line is the entry point of the program.

int age = 11;

This line declares a variable named age of type int (integer) and initializes it with the value 11.

if (age >= 18)

The if statement checks if the value of the age variable is greater than or equal to 18. If the condition is true, the code inside the if block will be executed; otherwise, the code inside the else block will be executed.

System.out.println(“You can create your BankAccount”);

If the condition (age >= 18) evaluates to true, this line will be executed. It prints the message "You can create your BankAccount" to the console. However, since the age of 11 is less than 18, it will instead execute the else block. else This line introduces the default block of code to be executed if the previous condition is false.

System.out.println(“You can’t create your BankAccount, your age is less than 18”);

If the condition (age >= 18) evaluates to false, this line will be executed. It prints the message "You can't create your BankAccount, your age is less than 18" to the console.

Output:
You can’t create your BankAccount, your age is less than 18

If Else-If Ladder Statement

if (condition) {
    // code for execution
} else if (condition) { // previous if condition not met then we check else if condition
    // code for execution
} else { // if previous condition not met then we will execute else block
    // code execution
}

Scenario 3: Java Program to Check Age and Categorize
Write a Java program that checks a person’s age and categorizes them as a Senior Citizen, eligible to create a bank account, or too young to create a bank account using an if-else-if ladder.
    ● If the age is 60 or older, print “You are a Senior Citizen.”
    ● If the age is 18 or older but less than 60, print “You can create your BankAccount.”
    ● If the age is less than 18, print “You can’t create your BankAccount. Your age is less than 18.”

Java Code Example:

public class IfElseifLadderExample {
    public static void main(String[] args) {
        int age = 30;
        if (age >= 60) {
            System.out.println(“You are a Senior Citizen.”);
        } else if (age >= 18) {
            System.out.println(“You can create your BankAccount.”);
        } else {
            System.out.println(“You can’t create your BankAccount. Your age is less than 18.”);
        }
    }
}

Output:
You can create your BankAccount.

Code Explanation

public class IfElseifLadderExample:

This line defines a new class named IfElseifLadderExample.

public static void main(String[] args)

This line is the entry point of the program.

int age = 30;

This line declares a variable named age of type integer and initializes it with the value 30.

if (age >= 60)

This line introduces the if statement. It checks if the value of the age variable is greater than or equal to 60. If the condition is true, the code inside the if block will be executed.

System.out.println(“You are a Senior Citizen.”);

If the condition (age >= 60) evaluates to true, this line will be executed. If not, the next condition will be checked. Since age is 30, the line "You are a Senior Citizen" will not be printed.

else if (age >= 18)

This line introduces an else-if statement, which allows for the evaluation of multiple conditions if the previous conditions are not met. It checks if the value of age is greater than or equal to 18. If yes, it will execute the code under the else if block.

System.out.println(“You can create your BankAccount.”);

If the condition (age >= 18) evaluates to true, then this line will be executed. Since age is 30, this condition is true, and the line "You can create your BankAccount" will be printed.

else

This line introduces the else block, the default block of code to be executed if all previous conditions are not met.

System.out.println(“You can’t create your BankAccount. Your age is less than 18.”);

Since the age is 30, neither the condition (age >= 60) nor (age >= 18) evaluates to false, so this line will not be executed.

Output:
You can create your BankAccount.

Tasks:
1. Do you want Health Insurance ?
a.If no then print Thank you
Output:

b.if yes then continue below 2nd question
Output:

c.if input is not equals to yes or no then show error.
Output:

2. a.If age is less then 21 then insurance cost is 1500
Output:

b.If age is between 21 and 50 then insurance cost is 2000
Output:

c. If age is more then 50 then insurance cost is 3000
Output:

3. In which Standard you are ?
a. if you are from 5th standard then you have to go at 1st floor for exam.
Output:

b. if you are from 6th standard then you have to go at 2nd floor
Output:

c. if you are from 7th standard then you have to go at 3rd floor.
Output:

d. other wise goto library for exam.
Output:

4. Class and If-Else (Java):
Create a class named Person with properties name, age, and gender.
In the main method, create an object of Person and set its properties.
Use an if-else statement to print “Adult” if the age is 18 or older, otherwise print “Minor”.

5. Constructor and If-Else (Java):
Create a class named Student with a constructor that initializes name and grade.
In the main method, create an object of Student.
Use an if-else statement to print “Pass” if the grade is 50 or above, otherwise print “Fail”.

6. Static Members and If-Else (Java):
Create a class named Utility with a static method isEven(int number) that returns true if the number is even.
In the main method, use the isEven method to check if a number (e.g., 10) is even.
Print “Even” if true, otherwise print “Odd”.

7. Namespaces and If-Else (Java):
Create a package company.employees with a class Employee having properties name and salary.
In the main method, create an object of Employee.
Use an if-else statement to print “High Salary” if the salary is greater than 100,000, otherwise print “Low Salary”.

8. DateTime and If-Else (Java):
Get the current date using LocalDate.now().
Use an if-else statement to print “Weekend” if today is Saturday or Sunday, otherwise print “Weekday”.

9. Class and Constructor with If-Else (Java):
Create a class named Book with properties title and price, and a constructor to initialize them.
In the main method, create an object of Book.
Use an if-else statement to print “Expensive” if the price is above 500, otherwise print “Affordable”.

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

This free course covers Java Date and Time concepts for beginners, including practical examples.

It is designed for beginners looking to learn Java programming concepts.

Yes, the training is offered for free on the Iqra Technology Academy website.

Yes, it includes hands-on exercises to practice if-else statements.

Yes, the training includes other basic Java topics beyond if-else statements.

Yes, the training is available online and can be accessed anytime.

Conditional statements allow decision-making in programs based on conditions.

It executes a code block if the condition evaluates to true.

It executes code when the if condition is false.

Yes, else if handles additional conditions after if.