DEV Community

vimala jeyakumar
vimala jeyakumar

Posted on

Switch statement & Ternary operator

Switch Statement:

-->The switch statement in Java is a multi-way branch statement. In simple words, the Java switch statement executes one statement from multiple conditions.
-->It is an alternative to an if-else-if ladder statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
-->The expression can be of type byte, short, char, int, long, (enums, String, or wrapper classes(TBD).

Note: Java switch expression must be of byte, short, int, long(with its Wrapper type), enums and string. Beginning with JDK7, it also works with enumerated types (Enums in java), the String class, and Wrapper classes.

Syntax:

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

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


Enter fullscreen mode Exit fullscreen mode

Flowchart:

Image description

Example program:

1.package day1if;
public class SizePrinter {

    public static void main(String[] args) {
        int sizeNumber = 15;

        switch (sizeNumber) {
            case 1:
                System.out.println("Extra Small");
                break;
            case 2:
                System.out.println("Small");
                break;
            case 3:
                System.out.println("Medium");
                break;
            case 4:
                System.out.println("Large");
                break;
            case 5:
                System.out.println("Extra Large");
                break;
            default:
                System.out.println("Invalid size number");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Invalid size number

2.package day1if;

public class Days{

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

        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:
            dayString = "Invalid day";
        }
        System.out.println(dayString);
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:
Thursday

Ternary Operator:

-->The ternary operator is a shorthand way of writing an if-else statement. It takes three operands: a condition, a result for when the condition is true, and a result for when the condition is false.

Note: Every code using an if-else statement cannot be replaced with a ternary operator.

Syntax:

condition ? result_if_true : result_if_false
Enter fullscreen mode Exit fullscreen mode

Flowchart:

Image description

Image description

Example program:

package day1if;
public class TernaryOperator
{  
public static void main(String args[])   
{  
int x, y;  
x = 20;  
y = (x == 1) ? 61: 90;  
System.out.println("Value of y is: " + y);  
y = (x == 20) ? 61: 90;  
System.out.println("Value of y is: " + y);  
}  
}  
Enter fullscreen mode Exit fullscreen mode

Output:
Value of y is: 90
Value of y is: 61

Reference:

->https://www.geeksforgeeks.org/switch-statement-in-java/

->https://www.geeksforgeeks.org/conditional-statements-in-programming/

->https://www.javatpoint.com/ternary-operator-in-java

Top comments (0)