DEV Community

AmalaReegan
AmalaReegan

Posted on

2

Day17; Switch Statements & Ternary Operator

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

Image description

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

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);
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

  Friday
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

    Invalid day
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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");
  }
}
Enter fullscreen mode Exit fullscreen mode

Output:

-101 is a Negative Number
Enter fullscreen mode Exit fullscreen mode

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

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more