Java Switch Statement Example and its Non-Equivalent in C#

Java Switch Statement Example and its Non-Equivalent in C#

Introduction:

This article demonstrates the usage of a switch statement in Java and its equivalent implementation in C#. The example showcases a scenario where an int variable represents a day, and based on its value, a corresponding day name is assigned using the switch statement. The article explains the syntax and execution flow of the switch statement in both languages and highlights the necessary modifications when converting the Java code to C#. It also highlights the distinction between the String class in Java and the string class in C#. The article concludes by emphasizing the structural similarities between the switch statements in both Java and C#, with the primary difference being the handling of console output using System.out.println() in Java and Console.WriteLine() in C#.

The following will be demonstrated by the example codes in both languages:

In the examples, we have an int variable named day initialized with a value of 3. The switch statement evaluates the value of day and compares it with each case statement. If a match is found, the corresponding code block is executed until the break statement is encountered, which terminates the switch statement.

Java Switch Statement Example:

// Java code

int day = 3;

String dayName;


switch (day) {

??case 1:

????dayName = "Monday";

????break;

??case 2:

????dayName = "Tuesday";

????break;

??case 3:

????dayName = "Wednesday";

????break;

??case 4:

????dayName = "Thursday";

????break;

??case 5:

????dayName = "Friday";

????break;

??case 6:

????dayName = "Saturday";

????break;

??case 7:

????dayName = "Sunday";

????break;

??default:

????dayName = "Invalid day";

????break;

}


System.out.println("The day is: " + dayName);


The algorithm of Java Example:

In this case, the value of day is 3, so the code block under case 3: is executed, assigning the string "Wednesday" to the dayName variable. Finally, the result is printed as "The day is: Wednesday".

If the value of day doesn't match any of the case statements, the code block under default: is executed, assigning the string "Invalid day" to the dayName variable.

C# Switch Statement Example:

// C# 8.0 or higher code

int day = 3;

string dayName = day switch

{

??1 => "Monday",

??2 => "Tuesday",

??3 => "Wednesday",

??4 => "Thursday",

??5 => "Friday",

??6 => "Saturday",

??7 => "Sunday",

??_ => "Invalid day"

};


Console.WriteLine("The day is: " + dayName);


The Conversion of Java code into C# code:

When converting the Java code to C#, the switch statement structure remains largely similar, with a few syntactical differences. The equivalent C# code using the traditional switch statement would look like this:

// C# Code

int day = 3;

string dayName;


switch (day)

{

??case 1:

????dayName = "Monday";

????break;

??case 2:

????dayName = "Tuesday";

????break;

??case 3:

????dayName = "Wednesday";

????break;

??case 4:

????dayName = "Thursday";

????break;

??case 5:

????dayName = "Friday";

????break;

??case 6:

????dayName = "Saturday";

????break;

??case 7:

????dayName = "Sunday";

????break;

??default:

????dayName = "Invalid day";

????break;

}


Console.WriteLine("The day is: " + dayName);

//End of Code


In the C# code, we define an int variable day with a value of 3, similar to the Java example. The switch statement evaluates the value of day and executes the code block corresponding to the matching case label. The break keyword is used to exit the switch statement once the correct case is executed.

However, in C# 8.0, there is a new switch expression syntax that provides a more concise way to write the switch statement. The equivalent C# 8.0 code would look like this:

// C# 8.0 or higher code

int day = 3;

string dayName = day switch

{

??1 => "Monday",

??2 => "Tuesday",

??3 => "Wednesday",

??4 => "Thursday",

??5 => "Friday",

??6 => "Saturday",

??7 => "Sunday",

??_ => "Invalid day"

};


Console.WriteLine("The day is: " + dayName);

//End of Code


This C# 8.0 syntax uses the switch expression with the => symbol to match the value of day and assign the corresponding day name to the dayName variable. The _ symbol serves as the default case. The resulting code achieves the same functionality as the Java example, but with fewer repetitive case and break keywords and fewer curly braces.

It's important to note that the C# 8.0 syntax for switch expressions is not available in earlier versions of C#.


Conclusion:

In conclusion, this article explored the usage of switch statements in Java and its equivalent implementation in C#. We examined a scenario where an integer variable represents a day, and based on its value, a corresponding day name is assigned using the switch statement.

The Java example showcased the traditional switch statement syntax, where each case is labeled with a value and terminated with a break statement. The code demonstrated the execution flow and the handling of a default case when no matches are found.

In C#, we compared two versions of the switch statement. The first version used the traditional switch statement structure, similar to Java, with case labels and break statements. The second version introduced the C# 8.0 switch expression syntax, which provided a more concise and expressive way to write the switch statement.

The Java and C# examples highlighted the structural similarities between the switch statements in both languages. However, it is important to note that the C# 8.0 syntax for switch expressions is not available in earlier versions of C#.

Additionally, we emphasized the distinction between the String class in Java and the string class in C#. Although both serve as representations of text, the difference lies in the class naming conventions.

While Java and C# have their own syntax variations, understanding the fundamental concept and usage of switch statements enables developers to utilize them effectively in their respective languages.

Overall, switch statements are powerful control flow constructs that simplify decision-making in code. Whether you are working with Java or C#, understanding how to use and convert switch statements between languages enhances your ability to write efficient and readable code.




要查看或添加评论,请登录

Shelvin Datt的更多文章

社区洞察

其他会员也浏览了