Conditional statements are crucial in programming as they allow your code to make decisions based on certain conditions. In Java, conditional statements help you control the flow of your program, deciding which actions to take when specific conditions are met.
In this blog, we’ll explore the different types of conditional statements in Java, learn how they work, and go through easy examples to understand them better.
1. What Are Conditional Statements in Java?
Conditional statements let your program choose what to do next based on whether something is true or false. Think of them like decision points in a flowchart—if the condition is true, one path is taken; if it’s false, another path is chosen.
2. Types of Conditional Statements in Java
Java provides several types of conditional statements that allow you to check conditions and decide what code should be executed:
- if statement – Executes a block of code if the condition is true.
- if-else statement – Executes one block if the condition is true, and another block if the condition is false.
- else-if ladder – Allows checking multiple conditions in sequence.
- switch statement – Chooses from many possible options based on the value of a variable.
Let’s break down each one with simple examples.
3. The if
Statement: Checking a Single Condition
The if
statement checks a condition, and if it’s true, the code inside the if
block is executed. If the condition is false, the program moves on without executing the if
block.
Here’s the basic syntax:
if (condition) {
// Code to execute if condition is true
}
JavaExample:
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
JavaIn this example, since age
is 20, which is greater than or equal to 18, the message “You are eligible to vote” is printed.
4. The if-else
Statement: Adding an Alternative
The if-else
statement is like a fork in the road. If the condition is true, one block of code is executed. If the condition is false, the program executes the code in the else
block.
Here’s how it looks:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
JavaExample:
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
JavaIn this case, the condition is false because the age
is less than 18, so the output will be “You are not eligible to vote.”
5. The else-if
Ladder: Checking Multiple Conditions
The else-if
ladder helps when you need to check several conditions. It’s a chain of if
and else-if
statements. As soon as one condition is found true, the corresponding block is executed, and the rest are skipped.
Here’s the syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if all conditions are false
}
JavaExample:
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else {
System.out.println("Grade: C");
}
JavaHere, the first condition is false (marks
is not greater than or equal to 90), but the second condition is true (marks
is 85), so the output will be “Grade: B.”
6. The switch
Statement: Choosing from Multiple Options
The switch
statement is useful when you have a variable and want to compare it against multiple values. Each value is called a “case.” If the variable matches one of the cases, the corresponding block of code is executed.
Here’s the structure:
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no case matches
}
JavaExample:
int day = 3;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}
JavaSince the value of day
is 3, the output will be “Tuesday.”
7. Conclusion
Conditional statements are a core feature of any programming language, and in Java, they allow you to control how your program behaves under different conditions. By mastering if
, if-else
, else-if
, and switch
statements, you’ll be able to write flexible, dynamic programs that can make decisions.
Next up, we’ll explore loops in Java, which allow you to repeat actions efficiently. Till then check out below for more interesting blogs and don’t forget to subscribe to our newsletter
Stay tuned!
Discover more from Lets Crack Faang
Subscribe to get the latest posts sent to your email.