How to write Java if-else
Statements
In Java, if-else
statements are used to control the flow of a program based on certain conditions. The if
statement executes a block of code if a condition evaluates to true
. The else
statement can follow an if
and provides an alternative block of code to execute if the condition is false
.
1. The Basic if
Statement
The if
statement allows you to execute a block of code only if a certain condition is true
.
Syntax:
if (condition) {
// Code to execute if the condition is true
}
Example:
public class Main {
public static void main(String[] args) {
int x = 10;
if (x > 5) {
System.out.println("x is greater than 5"); // Outputs: x is greater than 5
}
}
}
In this example, the condition x > 5
is true
, so the code inside the if
block is executed.
2. The else
Statement
The else
statement follows an if
and provides a block of code that runs if the if
condition evaluates to false
.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example:
public class Main {
public static void main(String[] args) {
int x = 3;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is not greater than 5"); // Outputs: x is not greater than 5
}
}
}
In this example, since x > 5
is false
, the code inside the else
block is executed.
3. The else if
Statement
The else if
statement allows you to test multiple conditions. If the first if
condition is false
, it moves on to check the next else if
condition. You can use multiple else if
statements, and an optional else
statement can be added at the end.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if both conditions are false
}
Example:
public class Main {
public static void main(String[] args) {
int x = 7;
if (x > 10) {
System.out.println("x is greater than 10");
} else if (x > 5) {
System.out.println("x is greater than 5 but less than or equal to 10"); // Outputs this
} else {
System.out.println("x is 5 or less");
}
}
}
Here, since x
is 7, the first condition is false, and the second else if
condition (x > 5
) is true, so the second block of code is executed.
4. Nested if
Statements
You can nest if-else
statements within each other to check multiple conditions. A nested if
statement means placing an if
or else if
statement inside another if
or else
block.
Example:
public class Main {
public static void main(String[] args) {
int x = 10;
int y = 20;
if (x == 10) {
if (y == 20) {
System.out.println("x is 10 and y is 20"); // Outputs this
}
}
}
}
In this example, both conditions are true
, so the innermost if
block is executed.
5. Ternary Operator
Java also provides a shorthand for if-else
statements called the ternary operator. This operator allows you to write a concise if-else
statement in a single line.
Syntax:
variable = (condition) ? expressionIfTrue : expressionIfFalse;
Example:
public class Main {
public static void main(String[] args) {
int x = 10;
String result = (x > 5) ? "x is greater than 5" : "x is not greater than 5";
System.out.println(result); // Outputs: x is greater than 5
}
}
Here, the condition x > 5
is true
, so the expression "x is greater than 5"
is assigned to result
and printed.
6. Boolean Expressions in if-else
Conditions in if-else
statements are typically Boolean expressions. A Boolean expression is one that evaluates to true
or false
, using comparison or logical operators like ==
, >
, <
, &&
, ||
, etc.
Example:
public class Main {
public static void main(String[] args) {
int age = 18;
if (age >= 18 && age < 65) {
System.out.println("You are an adult"); // Outputs: You are an adult
} else {
System.out.println("You are not an adult");
}
}
}
Here, the condition age >= 18 && age < 65
is true
, so the first block of code runs.
7. Common Mistakes
- Missing Braces: Although you can omit curly braces
{}
for single-line statements, it’s good practice to always use them to avoid errors.
// Without braces
if (x > 5)
System.out.println("x is greater than 5"); // Works fine for single line
// With braces (Best practice)
if (x > 5) {
System.out.println("x is greater than 5");
}
- Using
=
Instead of==
: Don’t confuse the assignment operator (=
) with the equality operator (==
).
if (x == 5) { // Correct (checking equality)
// Code here
}
Summary
- The
if
statement checks if a condition istrue
and executes a block of code. - The
else
statement provides an alternative code block to execute if theif
condition isfalse
. - The
else if
statement lets you check multiple conditions. - Ternary operators allow you to write compact
if-else
statements. - Boolean expressions, comparison operators, and logical operators are commonly used in
if-else
conditions.