The if-else Statement
What is if-else?
The if-else statement allows a program to choose between two blocks of code based on a condition.
- If the condition is true, the code inside the if block runs.
- If the condition is false, the code inside the else block runs.
It’s useful when you want to handle both possibilities of a condition.
Key Points:
- It allows execution of one of two blocks depending on the boolean condition.
- If the condition is true → if block is executed.
- If the condition is false → else block is executed.
- Only one block runs at a time — either if or else, never both.
- Conditions often involve relational (>, <, ==, etc.) or logical (&&,||, !) operators.
Syntax:
if (condition) {
// if condition is true
} else {
// if condition is false
}
- condition is a boolean expression (like marks > 70)
- Either the if block or the else block will run — never both
Flow diagram for the following examples:
Algorithm of if else
- Starts with variable initialization
- Checks if marks >= 50
- Based on result:
- if block runs: prints "Inside if block" and "Pass!"
- or else block runs: prints "Inside else block" and "Fail!"
- Then always prints: "Hello from outside!"
- Ends
Example 1:
class ShikshaSanchar {
public static void main(String[] args) {
int marks = 45;
if (marks >= 50) {
System.out.println("Inside if block!");
System.out.println("Pass!");
} else {
System.out.println("Inside else block!");
System.out.println("Fail!");
}
System.out.println("Hello from outside!");
}
}
Output:
Inside else block!
Fail!
Hello from outside!
Explanation:
- Variable marks is initialized with 45.
- The condition marks >= 50 is false, so:
- if block is skipped.
- else block is executed.
- It prints:
- "Inside else block!"
- "Fail!"
- After the if-else, the statement outside both blocks is always executed:
- "Hello from outside!"
Example 2:
class ShikshaSanchar {
public static void main(String[] args) {
int marks = 90;
if (marks >= 50) {
System.out.println("Inside if block!");
System.out.println("Pass!");
} else {
System.out.println("Inside else block!");
System.out.println("Fail!");
}
System.out.println("Hello from outside!");
}
}
Output:
Inside if block!
Pass!
Hello from outside!
Explanation:
- Variable marks is assigned 90.
- Condition marks >= 50 is true.
- So, only the if block runs:
- Prints: "Inside if block!" and "Pass!"
- The else block is skipped completely.
- After if-else, "Hello from outside!" is printed — this always runs.
Important Points about if-else in Java:
- if-else is used to handle two alternative cases based on a condition.
- The else block must immediately follow the if block — nothing should come in between.
- You cannot use else alone — it must be attached to an if.
- Only one block executes:
- If condition is true → if block runs.
- If condition is false → else block runs.
- Curly braces {} are optional if the block contains only one statement.
int marks = 80;
if (marks >= 50)
System.out.println("Pass!");
else
System.out.println("Fail!");
- Each block contains only one statement, so curly braces are not required.
- But if you have more than one line, braces are mandatory to group the block.
- Conditions must return a boolean value (true/false).
- You can use logical operators (&&, ||, !) to combine multiple conditions in the if.
Summary:
The if-else statement is used to execute one of two code blocks based on a condition.
- If the condition is true → the if block runs.
- Else → the else block runs.
- Only one block executes, never both.