What does a break statement do in the switch statement?

The switch statement is a selection control statement that is used to handle multiple choices and transfer control to the case statements within its body. The following code snippet shows an example of the use of the switch statement in C#:

switch(choice)
{
 case 1:
 console.WriteLine("First");
 break;
 case 2:
 console.WriteLine("Second");
 break;
 default:
 console.WriteLine("Wrong choice");
 break;
}

In switch statements, the break statement is used at the end of a case statement. The break statement is mandatory in C# and it avoids the fall through of one case statement to another.

Tagged . Bookmark the permalink.

Leave a Reply