Switch Statements:

==> The switch statement in Java is a multi-way branch statement.

==> In simple words, the Java switch statement executes one
statement from multiple conditions

Syntax:

switch(expression)
{
  case value1 :
     // Statements
     break; // break is optional

  case value2 :
     // Statements
     break; // break is optional
     ....
     ....
     ....
   default : 
     // default Statement
}

Example Program:

// Class
public class Days {

    public static void main(String[] args)
    {
        int day = 5;
        String dayString;

        // Switch statement with int data type
        switch (day) {

        case 1:
            dayString = "Monday";
            break;

        case 2:
            dayString = "Tuesday";
            break;

        case 3:
            dayString = "Wednesday";
            break;

        case 4:
            dayString = "Thursday";
            break;

        case 5:
            dayString = "Friday";
            break;

        case 6:
            dayString = "Saturday";
            break;

        case 7:
            dayString = "Sunday";
            break;

        // Default case
        default:
            dayString = "Invalid day";
        }
        System.out.println(dayString);
    }
}

Output:

  Friday

Default Ex pr:

public class Days {

    public static void main(String[] args)
    {
        int day = 8;
        String dayString;

        // Switch statement with int data type
        switch (day) {

        case 1:
            dayString = "Monday";
            break;

        case 2:
            dayString = "Tuesday";
            break;

        case 3:
            dayString = "Wednesday";
            break;

        case 4:
            dayString = "Thursday";
            break;

        case 5:
            dayString = "Friday";
            break;

        case 6:
            dayString = "Saturday";
            break;

        case 7:
            dayString = "Sunday";
            break;

        // Default case
        default:
            dayString = "Invalid day";
        }
        System.out.println(dayString);
    }
}

Output:

    Invalid day

Ternary Operator :

==> Ternary operator is the only operator in java that takes three operands.

==> A ternary operator starts with a condition followed by a question mark (?), then an expression to execute if the condition is ‘true; followed by a colon (:), and finally the expression to execute if the condition is ‘false’.

==> This operator is frequently used as a one line replacement for if…else statement.

Syntax of Ternary Operator:

variable = Condition ? Expression1: Expression2

Example program:

public class JavaExample {
  public static void main(String[] args) {

    // declared and initialized a int variable

    int num = -101;

    //Using ternary operator, we are assigning the "Positive"
    //or "Negative" String value to the String variable 'sign'
    //If num>0 then "Positive" is assigned to 'sign' else "Negative"

    String sign = (num > 0) ? "Positive" : "Negative";

    //Display output

    System.out.println(num+ " is a "+sign+ " Number");
  }
}

Output:

-101 is a Negative Number

reference:https://www.geeksforgeeks.org/switch-statement-in-java/
reference:https://www.javatpoint.com/java-switch

Author Of article : AmalaReegan Read full article