Java Conditional Statements
Java Conditional Statements
Java conditional statements are used to control the flow of execution in a program based on specific conditions. These conditions evaluate to either true or false. Conditional statements allow decision-making and execution of specific code blocks depending on whether the condition is met.
Here’s a detailed explanation of Java's primary conditional statements, with syntax and examples:
1. if Statement
The if statement checks a condition. When the condition evaluates as true, the code block inside the if statement is executed
if (condition) { // Instructions to run if the condition is true }
public class IfExample { public static void main(String[] args) { int number = 10; if (number > 0) { System.out.println("The number is positive."); } } }
Explanation:
Here, the condition number > 0 is evaluated. Since 10 > 0 is true, the message "The number is positive." is displayed.
2. if-else Statement
The if-else statement provides an alternative path of execution if the condition evaluates to false.
if (condition) { // Instructions or code to run if the condition is true } else { // Instructions or Code to execute if the condition is false }
public class IfElseExample { public static void main(String[] args) { int number = -5; if (number > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is not positive."); } } }
Explanation:
If number > 0 is false (as in this case), the code in the else block executes, displaying "The number is not positive."
3. if-else-if Ladder
This allows checking multiple conditions in a sequential manner.
if (condition1) { // Code if condition1 is true } else if (condition2) { // Code if condition2 is true } else { // CActions to perform if none of the conditions are met. }
public class IfElseIfExample { public static void main(String[] args) { int marks = 75; if (marks >= 90) { System.out.println("Grade: A"); } else if (marks >= 75) { System.out.println("Grade: B"); } else if (marks >= 50) { System.out.println("Grade: C"); } else { System.out.println("Grade: F"); } } }
Explanation:
The conditions are checked in order, and the first condition that evaluates to true determines which code block runs. Here, marks >= 75 is true, so "Grade: B" is printed.
4. switch Statement
A switch statement checks the value of a variable or expression and runs the corresponding case block.
switch (expression) { case value1: // Code to execute if expression == value1 break; case value2: // Code to execute if expression == value2 break; // Additional cases default: // Instructions or code to execute if no case matches. }
public class SwitchExample { public static void main(String[] args) { int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Invalid day"); } } }
Explanation:
The switch statement compares the variable day with each case. If it matches 3, it runs the related code and exits the switch
Key Points to Remember:
- Always use {} braces for blocks in if, else, and else if statements, even for single-line statements, to improve readability and avoid errors.
- switch is ideal for situations with multiple discrete options.
- Avoid forgetting the break statement in switch cases to prevent unintended fall-through behavior.
This detailed breakdown should give you a clear understanding of Java conditional statements!