switch statement usage in C ා

1 not every case needs to contain break. If the case statement is empty, it can not contain break, and the control flow ...

1 not every case needs to contain break. If the case statement is empty, it can not contain break, and the control flow will continue the subsequent case until the break is encountered.

char grade = 'C'; switch (grade) { case 'A': case 'B': case 'C': Console.WriteLine("CCC"); break; case 'D': Console.WriteLine("DDD"); break; default: Console.WriteLine("Invalid grades"); break; }

Output CCC no matter grad = A or B or C

2 if there is a processing statement in a case statement, it must contain a break or other jump statement.

char grade = 'A'; switch (grade) { case 'A': Console.WriteLine("AAA"); case 'B': case 'C': Console.WriteLine("CCC"); break; case 'D': Console.WriteLine("DDD"); break; default: Console.WriteLine("Invalid grades"); break; }

Tip error: control cannot run from one case label ("case 'A':") to another

3 a switch statement can have an optional default case that appears at the end of the switch. Default case can be used to perform a task when none of the above cases are true. The break statement in the default case is not required.

char grade = 'D'; switch (grade) { case 'A': Console.WriteLine("AAA"); break; case 'B': case 'C': Console.WriteLine("CCC"); break; case 'D': default: Console.WriteLine("Other achievements"); break; }

Output other grades when grade=D or other values (not ABC)

break must also be added to the default statement block

4 switch nesting

int a = 100; int b = 201; switch (a) { case 100: Console.WriteLine("100"); switch (b) { case 200: Console.WriteLine("200"); break; case 201: Console.WriteLine("201"); break; } break; case 101: Console.WriteLine("101"); break; default: Console.WriteLine("Other values"); break; }

Output 100, 101

5 switch and for loop

five point one The break statement is used to terminate the nearest block of closed code. The break in switch does not terminate the for loop
for(int i = 0; i < 5; i++) { switch (i) { case 1: Console.Write("111"); break; case 2: Console.Write("222"); break; case 3: Console.Write("333"); break; case 4: Console.Write("444"); break; default: Console.Write("other"); break; } Console.WriteLine("\t The first" + i.ToString() + "Subcycle"); }

111.png

five point two The continue statement cannot be used in the switch alone. It can be used in the switch within the loop to skip the current loop and enter the next one directly.
for(int i = 0; i < 5; i++) { switch (i) { case 1: Console.Write("111"); break; case 2: Console.Write("222"); continue; break; case 3: Console.Write("333"); break; case 4: Console.Write("444"); break; default: Console.Write("other"); break; } Console.WriteLine("\t The first" + i.ToString() + "Subcycle"); }

222.png

Note that the second cycle does not output

continue is not recommended in switch

16 May 2020, 11:08 | Views: 8120

Add new comment

For adding a comment, please log in
or create account

0 comments