A little attention to the Switch statement in C #

zhaozj2021-02-16  88

(First published in www.kunwsoft.com) ??????? Everyone is sure that the Switch statement is not unfamiliar, it enables the program to make a selection from multiple actions according to the value of the control expression (see logic process And a bit similar to multi-branch sentence if-else). There is also this statement in C and Java, but in C #, this statement has changed. First look at an example: ????? switch (letter) ?????????????????????????? case 'a': ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????. ?????? case 'b': ??????????????????????????? console.writeline ("bbbbb");??? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????. A '"This part will generate an error because the program will be executed until" Case' B '", that is, happening, but in C #, it is forbidden to hang. Therefore, you must use "BREAK" "goto" or "Return" to prevent crossing. But the situation below is the case: ????? switch (letter) ?????????????????????????? case 'a': ??????????????????????????????????????????????????????????????????????? ?? Console.writeline ("aaaaa"); ????????????????????????????????????????????????????????????? ?????? case 'c': ??????????????????????????? console.writeline ("ccccc");??? ???????????????????????? break;? ??????} ??????? (on top of this situation, I believe I need Explain, it can also be understood.) When there is another problem, it is: In special cases, make the program can carry this cross, how to implement it? In fact, this is also very simple, don't mention it before, you can use the "goto" statement to prevent the program's crossing, actually use it to implement cross, just use it to jump to another "case" Yet! ???????? Summary: In the Switch in C and Java, the program is allowed to cross, and the C # is not allowed, but it can be achieved using the "GOTO" statement.

转载请注明原文地址:https://www.9cbs.com/read-11566.html

New Post(0)