if-else-if Statement in Java
In Java, the if-else-if statement allows you to test multiple conditions in a sequential manner. You start with an initial if statement to test the first condition, and if it's false, you can have multiple else if statements to test additional conditions.
Here's the basic syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
} else {
// Code to execute if none of the conditions are true
}
Here's a breakdown of the components:
- if: This is the keyword that initiates the initial if statement.
- condition1: This is the first condition to be evaluated by the initial if statement.
- else if: This keyword is used to specify additional conditions to test if the previous condition(s) were false.
- condition2, condition3, etc.: These are additional conditions to be evaluated sequentially by the else if statements.
- else (optional): The else block is used to specify code that should execute when none of the conditions are true.
Here's a practical example of an if-else-if statement in Java:
package example;
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your marks: ");
int marks = scanner.nextInt();
if (marks >= 90) {
System.out.println("A grade");
} else if (marks >= 80) {
System.out.println("B grade");
} else if (marks >= 70) {
System.out.println("C grade");
} else if (marks >= 60) {
System.out.println("D grade");
} else {
System.out.println("F grade");
}
}
}
In this example:
- The first if statement checks if marks are greater than or equal to 90 and prints "A grade" if true.
- If the first condition is false, it moves to the next else if statement and checks if marks are greater than or equal to 80 and prints "B grade" if true.
- This process continues until it either finds a true condition and executes the associated code or reaches the else block, which serves as a default case if none of the conditions are met.
IMPORTANT: CLICK HERE for switch statement |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EXERCISES
Here are some exercises to help you practice using the if-else-if statement in Java:
- Temperature Converter: Create a program that converts a temperature from Fahrenheit to Celsius and vice versa. Use if-else-if statements to determine the conversion direction based on user input.
- GPA Calculator: Write a program that calculates a student's GPA based on their letter grades and credit hours. Use if-else-if statements to map letter grades to grade points and compute the GPA.
- Income Tax Calculator: Develop a program that calculates income tax based on different income brackets. Use if-else-if statements to determine the appropriate tax rate and calculate the tax owed.
- Traffic Light Simulator: Simulate a traffic light system with red, yellow, and green lights. Use if-else-if statements to control the sequence of lights and their durations.
- Number to Word Converter: Create a program that converts a given integer (e.g., 1 to 10) into its word representation (e.g., "one" to "ten"). Use if-else-if statements to handle different cases.
- BMI Categories: Extend the BMI exercise mentioned earlier by using if-else-if statements to categorize individuals into different BMI categories (e.g., underweight, normal weight, overweight, obese).
- Day of the Week: Write a program that prompts the user for a number between 1 and 7 and then displays the corresponding day of the week (e.g., 1 for "Sunday," 2 for "Monday"). Use if-else-if statements for mapping.
- Season Identifier: Create a program that identifies the season (e.g., winter, spring, summer, fall) based on a user-entered month and day. Use if-else-if statements to determine the season.
- Grade Distribution: Given a list of student grades, categorize them into different grade ranges (e.g., A, B, C, D, F) using if-else-if statements and display the distribution.
- Character Classification: Write a program that classifies a character entered by the user as a vowel, consonant, digit, or special character using if-else-if statements.
- Rock-Paper-Scissors Game: Implement a text-based rock-paper-scissors game where the user plays against the computer. Use if-else-if statements to determine the winner of each round.
- Menu Selection: Create a text-based menu with different options (e.g., for a restaurant menu or a game menu). Use if-else-if statements to execute the selected option.
- Number Comparison: Extend the number comparison exercise by using if-else-if statements to compare three numbers and find the largest or smallest among them.
- Quadratic Equation Solver: Modify the quadratic equation exercise mentioned earlier to handle cases with if-else-if statements to determine the number and nature of roots.
These exercises will help you become more proficient in using if-else-if statements for handling multiple conditions and decision-making in your Java programs.
Comments
Post a Comment