How To Use Switch Statements In C#

1 min read

What is a switch statement?

A switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression.

How does a switch statement works

  • The switch expression is evaluated once
  • In switch, the value of the expression is compared with the values of each case

Code sample

What are the differences between the break keyword and default keyword?

break: This keyword stop the execution of more code and case testing inside the switch block. When a match is found, and the job is done, it’s time for a break. There is no need for more testing.

default: The default keyword is optional and specifies some code to run if there is no case match.

Code sample:

 

Thanks for reading…

Happy Coding!

Leave a Reply

Your email address will not be published.