if statement

In Java, the if statement is used for conditional branching.

Simple if

It allows you to execute a block of code only if a specified condition is true.

Syntax

if (condition) {
    // Code to be executed if the condition is true
}Code language: JavaScript (javascript)
public class StudentInterest {
    public static void main(String[] args) {
        String studentInterest = "Java";
        if (studentInterest.equals("Java")) {
            System.out.println("Great choice! Java is a versatile and widely used programming language.");
            System.out.println("It's used in various applications, from web development to Android app development.");
            System.out.println("You can create powerful and efficient programs with Java. Keep exploring!");
        }
        System.out.println("Program finished.");
    }
}

Output

D:\>javac StudentInterest.java
D:\>java StudentInterest
Great choice! Java is a versatile and widely used programming language.
It's used in various applications, from web development to Android app development.
You can create powerful and efficient programs with Java. Keep exploring!

if-else statement

The if-else statement adds an alternative code block to be executed if the condition is false.

if (condition) {
    // Code to be executed if condition1 is true
} 
else {
    // Code to be executed if above condition is false
}Code language: JavaScript (javascript)

Here’s an example that demonstrates if-else statement

//StudentInterest.java
import java.util.Scanner;

public class StudentInterest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("What are you interested in learning? ");
        String studentInterest = scanner.nextLine();
        if (studentInterest.equalsIgnoreCase("Java")) {
            System.out.println("Great choice! Java is a versatile and widely used programming language.");
            System.out.println("It's used in various applications, from web development to Android app development.");
            System.out.println("You can create powerful and efficient programs with Java. Keep exploring!");
        } else {
            System.out.println("That's interesting! Learning Java can open up many opportunities for you.");
            System.out.println("Java is known for its portability, strong community support, and numerous job prospects.");
            System.out.println("Consider giving Java a try and see where it takes you!");
        }
        System.out.println("Program finished.");
    }
}

Nested if Statement

A nested if statement in Java refers to the practice of placing one if statement inside another. This allows you to create more complex decision-making structures by checking multiple conditions in a hierarchical manner. In other words, you have an outer if statement that contains one or more inner if statements (or if-else statements) within its code block.

if (outerCondition) {
    // Outer if block
    if (innerCondition1) {
        // Inner if block 1
        // Code to be executed if both outerCondition and innerCondition1 are true
    } else if (innerCondition2) {
        // Inner else-if block 2
        // Code to be executed if outerCondition is true and innerCondition1 is false,
        // but innerCondition2 is true
    } else {
        // Inner else block
        // Code to be executed if both outerCondition and innerCondition1 are false
    }
} else {
    // Outer else block
    // Code to be executed if outerCondition is false
}Code language: JavaScript (javascript)

Here’s an example that demonstrates the usage of Nested if Statement.

//OnlinePlatformSelection.java
import java.util.Scanner;
public class OnlinePlatformSelection {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Is the student interested in Indian traditional arts? (yes/no): ");
        String interestedInArts = scanner.nextLine();
        
        if (interestedInArts.equalsIgnoreCase("yes")) {
            System.out.print("Is the student interested in online coding platforms? (yes/no): ");
            String interestedInCoding = scanner.nextLine();
            
            if (interestedInCoding.equalsIgnoreCase("yes")) {
                System.out.print("Is the student interested in competitive coding? (yes/no): ");
                String interestedInCompetitiveCoding = scanner.nextLine();
                
                if (interestedInCompetitiveCoding.equalsIgnoreCase("yes")) {
                    System.out.println("The student is selected for competitive coding on online coding platforms.");
                } else {
                    System.out.println("The student is interested in Indian traditional arts but not competitive coding.");
                }
            } else {
                System.out.println("The student is interested in Indian traditional arts but not online coding platforms.");
            }
        } else {
            System.out.println("The student is not interested in Indian traditional arts.");
        }
        
        scanner.close();
    }
}

if else if else (if else ladder)

The syntax for an if-else if-else statement in Java allows you to create a sequence of conditions, where each condition is checked sequentially, and the corresponding block of code is executed based on the first condition that evaluates to true. If none of the conditions is true, the code inside the else block is executed.

Here’s an example that demonstrates the usage of if-else ladder:

//TraditionalArtSelection.java
import java.util.Scanner;
public class TraditionalArtSelection {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Welcome to the Indian Traditional Art Selection Program!");
        System.out.println("Please enter your preference: 1 for Music, 2 for Dance, 3 for Painting");
        int preference = scanner.nextInt();
        if (preference == 1) {
            System.out.println("You have selected Music!");
            System.out.println("Indian music is rich with classical and folk traditions.");
            System.out.println("You can explore various genres such as Hindustani and Carnatic.");
        } else if (preference == 2) {
            System.out.println("You have selected Dance!");
            System.out.println("Indian dance forms showcase cultural diversity and storytelling.");
            System.out.println("You can learn styles like Bharatanatyam, Kathak, Odissi, and more.");
        } else if (preference == 3) {
            System.out.println("You have selected Painting!");
            System.out.println("Indian paintings are known for their intricate details and vibrant colors.");
            System.out.println("You can explore styles like Madhubani, Tanjore, and Rajasthani.");
        } else {
            System.out.println("Invalid preference. Please enter a valid preference (1, 2, or 3).");
        }

        System.out.println("Thank you for using the program!");
    }
}
import java.util.Scanner;
public class CodingPlatformSelection {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Welcome to the Online Coding Platform Selection Program!");
        System.out.println("Please enter your interest: 1 for HackerRank, 2 for LeetCode, 3 for Codeforces");
        int interest = scanner.nextInt();
        if (interest == 1) {
            System.out.println("Great choice! You've selected HackerRank.");
            System.out.println("HackerRank offers a wide range of coding challenges and competitions.");
            System.out.println("You can enhance your problem-solving skills and learn from others' solutions.");
        } else if (interest == 2) {
            System.out.println("Awesome! You've chosen LeetCode.");
            System.out.println("LeetCode provides a platform to practice coding problems and improve your algorithms.");
            System.out.println("Challenge yourself with various types of problems and track your progress.");
        } else if (interest == 3) {
            System.out.println("Nice decision! You've picked Codeforces.");
            System.out.println("Codeforces hosts competitive programming contests and offers a ranking system.");
            System.out.println("Engage in real-time coding battles and refine your coding techniques.");
        } else {
            System.out.println("Invalid choice. Please select a valid option (1, 2, or 3).");
        }
        System.out.println("Remember, consistent practice is the key to becoming a better coder!");
    }
}
Scroll to Top