⬅ Previous Topic
Java Basic Input and OutputNext Topic ⮕
Java If Statement⬅ Previous Topic
Java Basic Input and OutputNext Topic ⮕
Java If StatementConditional statements allow your program to choose different paths of execution depending on whether a condition is true or false. Think of them as the 'brain' of your logic – making choices on your behalf.
Java provides several ways to handle conditions:
if
statementif-else
statementelse-if
ladderswitch
statementif
StatementThe if
statement checks a condition and executes the block of code inside it only 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.");
}
}
}
The number is positive.
The program checks if number > 0
. Since 10 is greater than 0, it prints the message. If the number were negative, nothing would be printed.
public class IfExample {
public static void main(String[] args) {
int number = -5;
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
The program checks if number > 0
. Since -5 is not greater than 0, if block does not execute and nothing has been printed.
if-else
StatementUse this when you have two scenarios — one when the condition is true and another when it's false.
public class IfElseExample {
public static void main(String[] args) {
int number = -5;
if (number >= 0) {
System.out.println("Non-negative number");
} else {
System.out.println("Negative number");
}
}
}
Negative number
Since the given condition is false, else block executes.
This pattern is useful when your program must respond differently depending on a single true/false condition.
else-if
LadderWhen you have multiple conditions to evaluate in a sequence, else-if
ladders help maintain readability and logic flow.
public class ElseIfExample {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Grade A");
} else if (score >= 75) {
System.out.println("Grade B");
} else if (score >= 60) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
}
}
Grade B
Start with the highest condition and work your way down to the least. This avoids overlapping ranges or incorrect classifications.
switch
StatementUse a switch
when you have a single variable that can take multiple constant values (like enums, numbers, or strings in Java 7+).
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("Another day");
}
}
}
Wednesday
break
statement to prevent fall-through unless you want cumulative behavior.default
case to handle unexpected inputs.break
in switch
, which may lead to incorrect results.=
instead of comparison ==
in conditions.if-else
, leading to confusion and logic errors.Use Case | Statement Type |
---|---|
Single true/false condition | if |
Binary decision path | if-else |
Multiple ranges or tiers | else-if ladder |
Discrete values for one variable | switch |
Conditional statements bring your Java programs to life by adding the power of choice. They're the difference between rigid, robotic scripts and adaptive, intelligent logic. Start small with if
, branch into else
, scale with else-if
, and simplify with switch
.
int number = -3;
if (number > 0) {
System.out.println("Positive");
}
switch
statement in Java?else-if
ladder?break
keyword in a switch
statement can lead to unintended execution of multiple cases.⬅ Previous Topic
Java Basic Input and OutputNext Topic ⮕
Java If StatementYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.