Conditional Statement

The basic conditional statements are if, if-else, and else-if, but as an intermediate or advanced Java developer, you should also know about the switch statement and the conditional (ternary) operator.

If statement: This is the most basic form of control flow statement. It allows the program to decide to perform an action or not based on whether a condition is true or not.

int age = 20;

if (age > 18) {

    System.out.println("You are an adult");

}

In the above code, if the condition age > 18 is true, then the message "You are an adult" is printed. If the condition is false, nothing happens.


If-Else statement: The if-else statement provides an alternative option if the if condition is false.

int age = 15;

if (age > 18) {

    System.out.println("You are an adult");

} else {

    System.out.println("You are not an adult");

}

In this example, if age > 18 is false, then the message "You are not an adult" is printed.


Else-If statement: The else-if statement allows for multiple conditions to be checked in sequence. The first condition that evaluates to true has its corresponding code block executed, and all subsequent conditions are skipped.

int age = 20;

if (age > 18) {

    System.out.println("You are an adult");

} else if (age > 13) {

    System.out.println("You are a teenager");

} else {

    System.out.println("You are not a teenager or an adult");

}


Switch statement: This allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

int day = 2;

String dayType;

switch (day) {

    case 1:

        dayType = "Monday";

        break;

    case 2:

        dayType = "Tuesday";

        break;

    // ... other cases ...

    default:

        dayType = "Invalid day";

}

System.out.println(dayType);


This switch statement is used to map day values to their string names. It's an efficient way to write a conditional when checking the same variable for many conditions.

In Java 14 and later, there's also the "enhanced" switch statement, which can be used as either a statement or an expression and that does not require the break keyword.

int day = 2;

String dayType = switch (day) {

    case 1 -> "Monday";

    case 2 -> "Tuesday";

    // ... other cases ...

    default -> "Invalid day";

};

System.out.println(dayType);


Ternary Operator: This is a shortcut for the if-else statement and is a one liner replacement for if-then-else statement. It is used to decide which value to assign to a variable based on a boolean value.

int time = 20;

String result = (time < 18) ? "Good day." : "Good evening.";

System.out.println(result);

The above code checks if time is less than 18. If this condition is true, it assigns "Good day." to the result variable. Otherwise, it assigns "Good evening.".

These conditional statements allow you to make decisions in your code based on the state of the program. They are a key part of any programming language and provide flexibility and control over the program flow.



Comments

Popular posts from this blog

Wrapper

DataType and Variable