Menu

Java Topic

Java If...Else

The if...else statement in Java is used to perform conditional execution of code blocks based on whether a condition evaluates to true or false. It allows for branching in your program's flow, enabling different actions based on varying conditions.

1. Basic If Statement

The simplest form of the if statement executes a block of code if a specified condition is true:

if (condition) {
    // code to execute if condition is true
}

2. If-Else Statement

The if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false:

if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

3. If-Else If-Else Statement

The if-else if-else statement lets you check multiple conditions sequentially. It evaluates each condition in order, executing the corresponding block of code for the first true condition:

if (condition1) {
    // code to execute if condition1 is true
} else if (condition2) {
    // code to execute if condition2 is true
} else {
    // code to execute if none of the above conditions are true
}

4. Nested If Statements

You can nest if statements within each other to perform more complex conditional logic:

if (condition1) {
    if (condition2) {
        // code to execute if both condition1 and condition2 are true
    }
}

5. Ternary Operator

The ternary operator is a shorthand for the if-else statement. It returns a value based on a condition:

result = (condition) ? value1 : value2;


This assigns value1 to result if condition is true, otherwise value2 is assigned.

This code checks if the age is greater than or equal to 18 and prints a message accordingly.

6. Example

Here is an example that demonstrates the usage of if...else statements:

int age = 20;

if (age >= 18) {
    System.out.println("You are an adult.");
} else {
    System.out.println("You are a minor.");
}

 

Java Switch

The switch statement in Java is used to execute one block of code among many options based on the value of an expression. It provides a way to handle multiple possible values of a variable in a more readable and organized manner compared to multiple if-else statements.

1. Basic Syntax

The basic syntax of a switch statement is as follows:

switch (expression) {
    case value1:
        // code to execute if expression equals value1
        break;;
    case value2:
        // code to execute if expression equals value2
        break;;
    default:
        // code to execute if expression does not match any case
}

2. Switch Expression

The expression in a switch statement must evaluate to a value of type byte, short, char, int, String (from Java 7 onwards), or an enum type. As of Java 12, the switch statement also supports switch expressions, which can return a value:

String dayType = switch (day) {
    case MONDAY, case TUESDAY, case WEDNESDAY, case THURSDAY, case FRIDAY -> "Weekday";
    case SATURDAY, case SUNDAY -> "Weekend";
};

3. Case Labels

case labels must be unique within the same switch block. If a case block does not end with a break statement, execution will "fall through" to the subsequent cases:

switch (day) {
    case MONDAY:
        System.out.println("Monday");
    case TUESDAY:
        System.out.println("Tuesday");
        break;;
    default:
        System.out.println("Invalid day");
}

In this example, if day is MONDAY, it will print "Monday" and then continue to execute the code for TUESDAY until it encounters the break statement.

4. Default Case

The default case is optional but recommended. It executes if none of the case values match the expression:

switch (day) {
    case MONDAY:
        System.out.println("Monday");
        break;;
    default:
        System.out.println("Invalid day");
}

5. Enhanced Switch (Java 12+)

Java 12 introduced an enhanced switch statement with new features:

String result = switch (day) {
    case MONDAY, case FRIDAY -> "Weekday";
    case SATURDAY, case SUNDAY -> "Weekend";
};
switch (day) {
    case MONDAY -> {
        System.out.println("Start of work week");
        System.out.println("Prepare for the week");
    }
    case FRIDAY -> System.out.println("End of work week");
    default -> System.out.println("Invalid day");
}
  • Switch Expressions: Allow a switch to return a value and use the -> syntax:
  • Block Bodies: Allow multiple statements in each case block:

6. Example

Here’s a simple example using a switch statement to determine the day of the week:

int day = 3;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;;
    case 2:
        System.out.println("Tuesday");
        break;;
    case 3:
        System.out.println("Wednesday");
        break;;
    default:
        System.out.println("Invalid day");
}

This example checks the value of day and prints the corresponding day of the week.

 

 

Comments (0)

No comments yet. Be the first to comment!

LecturreAI Assistant

Hello! I'm LecturreAI

What would you like to learn today?