๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Java Conditional Statements with Examples  


Introduction

  • "Is it raining ?"
    • "No" - Then lets play cricket.
    • "Yes" - Ohh, we cant play cricket.
  • Just like in real life, in programming also, we face situations where we need to make decisions based on certain conditions.
  • In Java, we use conditional statements to decide which part of the code should run depending on the condition.
  • Conditional Statments helps us to:
    • Control the flow of the program.
    • Decide which block of code should be executed when certain conditions are met.
  • Examples of Conditional Statements in Java:
    1. if: Runs code if a condition is true.
    2. if-else: Runs one block of code if true, and another block if false.
    3. if-else if ladder: Checks multiple conditions one by one and runs the block of code for the first true condition.
    4. switch: Chooses a block of code to run based on specific cases.
    These are explained deeply as below:

"if" Statement in Java
  • The if statement in Java evaluates a boolean condition.
  • If the condition is true, the block of code inside the if statement is executed.
  • Syntax:
    if(condition)
    {
        // this block will be executed if the condition is true
    }
  • Program:
    public class IfExample
    {
        public static void main(String[] args)
        {
            int number = 10;
    
            // Check if the number is positive
            if (number > 0)
            {
                System.out.println("The number is positive.");
            }
        }
    }
    Output:
    The number is positive.

"if-else" Statement in Java
  • The if-else statement in Java evaluates a boolean condition.
  • If the condition is true, the block of code inside the if is executed; otherwise, the code inside the else block runs.
  • Syntax:
    if(condition)
    {
        // this block will be executed if the condition is true
    }
    else
    {
        // this block will be executed if condition is false   
    }
  • Program:
    public class IfElseExample
    {
        public static void main(String[] args)
        {
            int number = -5;
    
            // Check if the number is positive or negative
            if (number > 0)
            {
                System.out.println("The number is positive.");
            }
            else
            {
                System.out.println("The number is negative.");
            }
        }
    }
    Output:
    The number is negative.

"if-else if" Ladder Statement in Java
  • The if-else if ladder in Java evaluates multiple boolean conditions in sequence.
  • If any of the condition is true, the block of code associated with that condition is executed; if none of the conditions are true, the optional else block runs.
  • Syntax:
    if (condition1) 
    {
        // Code to execute if condition1 is true
    }
    else if (condition2)
    {
        // Code to execute if condition2 is true
    }
    // ---- more else-if blocks as needed ----
    else
    {
        // Code to execute if none of the above conditions are true
    }
  • Program:
    public class IfElseIfLadderExample
    {
        public static void main(String[] args)
        {
            int marks = 75;
    
            // Determine the grade based on marks
            if (marks >= 90)
            {
                System.out.println("Grade: A");
            }
            else if (marks >= 75)
            {
                System.out.println("Grade: B");
            }
            else if (marks >= 50)
            {
                System.out.println("Grade: C");
            }
            else
            {
                System.out.println("Grade: F");
            }
        }
    }
    Output:
    Grade: B

"switch" Statement in Java
  • The switch statement in Java runs one block of code based on matching a condition.
  • It checks multiple cases for a value and runs the matching case.
  • If no case matches, the optional default block runs.
  • Syntax:
    switch (expression)
    {
        case value1:
            // Code to execute if expression equals value1
            break;
        case value2:
            // Code to execute if expression equals value2
            break;
        // ---- more cases as needed ----
        default:
            // Code to execute if no case matches (optional)
            break;
    }
  • Program:
    public class SwitchExample
    {
        public static void main(String[] args)
        {
            int day = 3;
    
            // Determine the day of the week
            switch (day)
            {
                case 1:
                    System.out.println("Monday");
                    break;
                case 2:
                    System.out.println("Tuesday");
                    break;
                case 3:
                    System.out.println("Wednesday");
                    break;
                case 4:
                    System.out.println("Thursday");
                    break;
                case 5:
                    System.out.println("Friday");
                    break;
                case 6:
                    System.out.println("Saturday");
                    break;
                case 7:
                    System.out.println("Sunday");
                    break;
                default:
                    System.out.println("Invalid day");
            }
        }
    }
    Output:
    Wednesday