java

Java Decision Making Statements


Decision Making Statements

Decision-making statements allow you to control the flow of your program based on certain conditions.

 

1.  if statement

It is used to when we want to test a condition.
 

Diagram

Syntax

if (condition) {
    // Code to be executed if the condition is true
}

 

Example

public class LoginProgram {

    public static void main(String[] args) {
    
        int password = 2308;
      
        if (password==2308) {
            System.out.println("Login successful!");
        } 
        System.out.println("Thank You !!");
    }
}

 

2. if-else statement

It is used to execute two statements either if statement or else statement for a single condition.

Diagram

 

Syntax

if (condition) {
    // Code to be executed if the condition is true
}
else {
	// Code to be executed if the condition is false
}

 

Example

public class LoginProgram {

    public static void main(String[] args) {
    
        int password = 2308;
      
        if (password==2308) {
            System.out.println("Login successful!");
        } 
       else {
       		System.out.println("Incorrect password");
       }
    }
}

 

3. else-if statement

It is used to when we have only when we have multiple conditions to execute.

 

Diagram

Syntax

if (condition 1) {
    // Code to be executed if condition 1 is true
} else if (condition 2) {
    // Code to be executed if condition 2 is true
} else if (condition 3) {
    // Code to be executed if condition 3 is true
} else {
    // Code to be executed if none of the conditions is true
}

 

Example

class NumberCheck {
    public static void main(String[] args) {
    
        int x = 10;

		// Check if the number is negative, positive, or zero
        if (number < 0) {
            System.out.println("The number is negative.");
        } else if (number > 0) {
            System.out.println("The number is positive.");
        } else {
            System.out.println("The number is equal to zero.");
        }

	}
}

 

4. nested if-else statement

Whenever we define if else block inside another if else block called nested if else statement.

Diagram

Syntax

if (condition1) {
    // Code to be executed if condition1 is true

    if (condition2) {
        // Code to be executed if both condition1 and condition2 are true
    } else {
        // Code to be executed if condition1 is true but condition2 is false
    }

} else {
    // Code to be executed if condition1 is false

    if (condition3) {
        // Code to be executed if condition1 is false and condition3 is true
    } else {
        // Code to be executed if both condition1 and condition3 are false
    }
}

 

Example

public class GreatestNumber {
    public static void main(String[] args) {
     	int num1=10, num2=20, num3=30;
        // Find the greatest number among the three using nested if-else
        if (num1 >= num2) {
            if (num1 >= num3) {
                System.out.println("The greatest number is: " + num1);
            } else {
                System.out.println("The greatest number is: " + num3);
            }
        } else {
            if (num2 >= num3) {
                System.out.println("The greatest number is: " + num2);
            } else {
                System.out.println("The greatest number is: " + num3);
            }
        }
    }
}

 

5. Switch Statement

It is a multiple choice decision making statement, it is used when we want to execute only case out of multiple cases.

Diagram

 

Syntax

switch (expression) {
    case value1:
        // Code to be executed if expression equals value1
        break;
    case value2:
        // Code to be executed if expression equals value2
        break;
    // Additional cases as needed
    default:
        // Code to be executed if none of the cases match the expression
}

 

Example

import java.util.Scanner;

public class DayOfWeek {
    public static void main(String[] args) {
     	int dayNumber;
    	String dayOfWeek;
    	
        // Get user input
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number (1-7): ");
        dayNumber = scanner.nextInt();


        switch (dayNumber) {
            case 1:
                dayOfWeek = "Sunday";
                break;
            case 2:
                dayOfWeek = "Monday";
                break;
            case 3:
                dayOfWeek = "Tuesday";
                break;
            case 4:
                dayOfWeek = "Wednesday";
                break;
            case 5:
                dayOfWeek = "Thursday";
                break;
            case 6:
                dayOfWeek = "Friday";
                break;
            case 7:
                dayOfWeek = "Saturday";
                break;
            default:
                dayOfWeek = "Invalid day number";
        }

        // Print the result
        System.out.println("The day of the week is: " + dayOfWeek);
    }
}