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
.
if
StatementThe 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
}
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
}
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
}
Nested conditional statements are conditional statements inside other conditional statements. This allows us to check for multiple conditions in a hierarchical manner.
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.
Imagine you're developing a program for a school. You need to determine a student's grade based on their score:
If the score is 90 or above, the grade is "A".
If the score is between 80 and 89, the grade is "B".
If the score is between 70 and 79, the grade is "C".
If the score is between 60 and 69, the grade is "D".
If the score is below 60, the grade is "F".
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");
}
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.
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:
If the score is 90 or above, print "Grade: A".
If the score is between 80 and 89, print "Grade: B".
If the score is between 70 and 79, print "Grade: C".
If the score is between 60 and 69, print "Grade: D".
If the score is below 60, print "Grade: F".
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!
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.
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.
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:
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.
You can use various methods of the Scanner
class to read different types of input. Here are some common methods:
nextInt()
: Reads an integer.
nextDouble()
: Reads a double.
nextLine()
: Reads a line of text.
next()
: Reads a single word.
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();
}
}
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();
}
}