AAP-2.I Write and determine the result of nested conditional statements

What are Conditional Statements?

Conditional statements are used to perform different actions based on different conditions. In Java, the most common conditional statements are if, else if, and else.

The Basic if Statement

The if statement checks a condition, and if the condition is true, it executes the block of code inside the if statement.

if (condition) {
    // code to be executed if the condition is true
}

Adding else

The else statement allows us to execute a block of code if the condition in the if statement is false.

if (condition) {
    // code to be executed if the condition is true
} else {
    // code to be executed if the condition is false
}

Adding else if

The else if statement lets us check multiple conditions.

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 both conditions are false
}

What are Nested Conditional Statements?

Nested conditional statements are conditional statements inside other conditional statements. This allows us to check for multiple conditions in a hierarchical manner.

Example of a Nested Conditional Statement

Let's consider an example where we want to check if a number is positive, negative, or zero, and if it is positive, we also want to check if it is even or odd.

int number = 5;

if (number > 0) {
    // number is positive
    if (number % 2 == 0) {
        // number is even
        System.out.println("The number is positive and even.");
    } else {
        // number is odd
        System.out.println("The number is positive and odd.");
    }
} else if (number < 0) {
    // number is negative
    System.out.println("The number is negative.");
} else {
    // number is zero
    System.out.println("The number is zero.");
}

In this example, we first check if the number is greater than 0. If it is, we then check if the number is even or odd using another if statement inside the first if statement.

Real-World Example

Imagine you're developing a program for a school. You need to determine a student's grade based on their score:

Additionally, if the score is an "A", we want to check if it's a perfect score of 100.

int score = 95;

if (score >= 90) {
    if (score == 100) {
        System.out.println("Grade: A (Perfect Score!)");
    } else {
        System.out.println("Grade: A");
    }
} else if (score >= 80) {
    System.out.println("Grade: B");
} else if (score >= 70) {
    System.out.println("Grade: C");
} else if (score >= 60) {
    System.out.println("Grade: D");
} else {
    System.out.println("Grade: F");
}

Full Program Example

Here is a complete Java program that demonstrates nested conditional statements. This program asks the user to enter a number and then determines if the number is positive, negative, or zero, and if it's positive, whether it is even or odd.

import java.util.Scanner;

public class NestedConditionalExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Ask the user to enter a number
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        
        // Check if the number is positive, negative, or zero
        if (number > 0) {
            // The number is positive
            if (number % 2 == 0) {
                // The number is even
                System.out.println("The number is positive and even.");
            } else {
                // The number is odd
                System.out.println("The number is positive and odd.");
            }
        } else if (number < 0) {
            // The number is negative
            System.out.println("The number is negative.");
        } else {
            // The number is zero
            System.out.println("The number is zero.");
        }
        
        scanner.close();
    }
}

You can read more about Scanner Class after the programming problem on this page.

Programming Problem

Now it's your turn! Write a Java program that asks the user to enter a student's score and then prints out the grade according to the following criteria:

Additionally, if the score is an "A", check if it's a perfect score of 100 and print "Perfect Score!" along with the grade.

Good luck!

Understanding the Java Scanner Class

The Java Scanner class is part of the java.util package and is used for obtaining input of primitive types like int, double, etc., and strings. It's a simple way to read user input from various sources, such as the keyboard, files, and streams.

Why Use the Scanner Class?

When writing programs, you often need to get input from users. The Scanner class provides a convenient way to read input, making it easy to interact with users in your programs.

How to Use the Scanner Class

1. Import the Scanner Class

Before you can use the Scanner class, you need to import it into your program. Add the following line at the top of your Java file:

2. Create a Scanner Object

To read input, you need to create an instance of the Scanner class. You can do this by passing System.in to the Scanner constructor, which tells the scanner to read from the keyboard.

3. Reading Input

You can use various methods of the Scanner class to read different types of input. Here are some common methods:

Example of Using the Scanner Class

Here’s a simple program that demonstrates how to use the Scanner class to read an integer, a double, and a string from the user:

import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the keyboard
        Scanner scanner = new Scanner(System.in);

        // Read an integer
        System.out.print("Enter an integer: ");
        int intValue = scanner.nextInt();
        System.out.println("You entered: " + intValue);

        // Read a double
        System.out.print("Enter a double: ");
        double doubleValue = scanner.nextDouble();
        System.out.println("You entered: " + doubleValue);

        // Read a line of text
        scanner.nextLine(); // Consume the leftover newline
        System.out.print("Enter a line of text: ");
        String line = scanner.nextLine();
        System.out.println("You entered: " + line);

        // Close the scanner
        scanner.close();
    }
}

Real-World Example

Let's say you're creating a program for a simple quiz game. You can use the Scanner class to read the user's answers:

import java.util.Scanner;

public class QuizGame {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the Quiz Game!");

        System.out.print("Question 1: What is the capital of France? ");
        String answer1 = scanner.nextLine();

        if (answer1.equalsIgnoreCase("Paris")) {
            System.out.println("Correct!");
        } else {
            System.out.println("Incorrect. The correct answer is Paris.");
        }

        System.out.print("Question 2: What is 5 + 3? ");
        int answer2 = scanner.nextInt();

        if (answer2 == 8) {
            System.out.println("Correct!");
        } else {
            System.out.println("Incorrect. The correct answer is 8.");
        }

        scanner.close();
    }
}