The switch is a selection statement that chooses a single switch section to execute from a list of candidates based on a pattern match with the match expression.

The Code:

New Project (73).png

The Output of Your Program:

New Project (74).png

Notes:

switch statement can include any number of cases. However, no two case labels may contain the same constant value.
The break; statement that ends each case will be covered shortly.

The default Case

In a switch statement, the optional default case is executed when none of the previous cases match.

The Code:

New Project (75).png

The Output of Your Program:

New Project (76).png

The break Statement

The break statement in C# has following two usage:

  • When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.
  • It can be used to terminate a case in the switch statement.

If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.

c# break statement

if_Audio_-_Video_-_Game_26_2298369 (1)       if_Audio_-_Video_-_Game_27_2298366 (1)

Leave a comment