C 語言中switch模組case default

2022-08-02 22:21:20 字數 965 閱讀 8817

package flowcontrol;

1. 2. publicclass switchcase

15. }

16.

17. // last default

18. publicstaticvoid testlast(int i)

29. }

30.

31. // middle default

32. publicstaticvoid testmiddle(int i)

44. }

45.

46. publicstaticvoid main(string args)

66.

67. }

輸出如下所示:

1. two

2. there

3. ------------------

4. default

5. one

6. two

7. there

8. |||||||||||||||||||||||||||||||||||

9. two

10. there

11. default

12. ----------------

13. default

14. |||||||||||||||||||||||||||||||||||

15. two

16. default

17. there

18. ----------------

19. default

20. there

分析如下:

(1)switch語句關鍵地方是進入點,有了進入點沒有break的情況下會執行到底,或遇到break

(2)沒有匹配值的時候default就是進入點,進入default以後會和普通進入點一樣,如果沒有break繼續執行下面語句

C語言中switch語句

在多分支選擇中,用if語句程式會冗長,可讀性低,用switch更合適。switch語句格式 switch 表示式 注意 1 當 表示式 與 各個常量 都不匹配時執行default後的語句n,也是可有 可無 2 switch後面括號裡的 表示式 的值為整型或字元型 3 若沒有default時,swit...

C語言中的switch詳解

switch語句 c語言中實現多分支選擇的語句 例 輸入乙個分數等級,輸出對應分數 switch grade 注 switch 括號中值的型別是整型 包括字元型 grade在這兒是字元型別。case標號只起乙個標記的作用,沒有條件檢查的功能,程式執行時,根據switch 中的值,找到與其相同的cas...

C 語言中switch語法的用處

switch用在程式設計中,如c語言中它經常跟case一起使用,是乙個判斷選擇 其功能就是控制業務流程流轉的。switch語句的語法如下 switch,case和default是關鍵字 switch controllingexpression 遵守switch語句規則 switch語句非常有用,但在...