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:

Java If condition Diagram

Algorithm of if else

  1. Starts with variable initialization
  2. Checks if marks >= 50
  3. Based on result:
  4. if block runs: prints "Inside if block" and "Pass!"
  5. or else block runs: prints "Inside else block" and "Fail!"
  6. Then always prints: "Hello from outside!"
  7. 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.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar