DEV Community

Ilakkiya
Ilakkiya

Posted on

SWITCH STATEMENT AND TERNARY:

1. 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[TBD], String, or wrapper classes (Integer, Short, Byte, Long).

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.[TBD]
Enter fullscreen mode Exit fullscreen mode

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

case value2 :
// Statements
break; // break is optional
....
....
....
default :
// default Statement
}
//Break-It will moves to next statement.

FLOWCHART:

Image description

EXAMPLE PROGRAM:

public class Days {
public static void main(String[] args)
{
int day = 5;
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);
}
}

output:Friday

2.TERNARY STATEMENT:

In Java, the ternary operator is a type of Java conditional operator.

The ternary operator (? :) consists of three operands. It is used to evaluate Boolean expressions. The operator decides which value will be assigned to the variable. It is the only conditional operator that accepts three operands. It can be used instead of the if-else statement. It makes the code much more easy, readable, and shorter.

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

SYNTAX:

variable = (condition) ? expression1 : expression2  
Enter fullscreen mode Exit fullscreen mode

The above statement states that if the condition returns true, expression1 gets executed, else the expression2 gets executed and the final result stored in a variable.

FLOWCHART:

Image description

Image description

EXAMPLE PROGRAM:

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

}

}

output:
Value of y is: 90
Value of y is: 61

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

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