switch語法中break,default作用說明


關於java中switch使用的一些說明
switch(表達式)
{
case 常量表達式1:語句1;
....
case 常量表達式2:語句2;
default:語句;
}
default就是如果沒有符合的case就執行它,default並不是必須的.
case后的語句可以不用大括號.
switch語句的判斷條件可以接受int,byte,char,short,不能接受其他類型.
如果使用long類型的話編譯時會有錯誤產生,這點在使用上要注意,其他的數據類型都不行。 
簡單地說就是能夠自動轉換程int類型的數據類型才行。 
而case是指switch小括號中的變量會出現且你想要處理的值,它除了可以是個整數、字符之外,還可以是一些簡單的算術表達式,不過算數表達式的結果要滿足剛剛所說的四種數據類型。一旦case匹配,就會順序執行后面的程序代碼,而不管后面的case是否匹配,直到遇見break,利用這一特性可以讓好幾個case執行統一語句
Java代碼   收藏代碼
  1. 1.package flowcontrol;     
  2. 2.    
  3. 3.public class SwitchCase {     
  4. 4.    // first default     
  5. 5.    public static void testFirst(int i) {     
  6. 6.        switch (i) {     
  7. 7.        default:     
  8. 8.            System.out.println("default");// first default     
  9. 9.        case 1:     
  10. 10.            System.out.println("one");     
  11. 11.        case 2:     
  12. 12.            System.out.println("two");     
  13. 13.        case 3:     
  14. 14.            System.out.println("there");     
  15. 15.        }     
  16. 16.    }     
  17. 17.    
  18. 18.    // last default     
  19. 19.    public static void testLast(int i) {     
  20. 20.        switch (i) {     
  21. 21.        case 1:     
  22. 22.            System.out.println("one");     
  23. 23.        case 2:     
  24. 24.            System.out.println("two");     
  25. 25.        case 3:     
  26. 26.            System.out.println("there");     
  27. 27.        default:     
  28. 28.            System.out.println("default");// last default     
  29. 29.        }     
  30. 30.    }     
  31. 31.    
  32. 32.    // middle default     
  33. 33.    public static void testMiddle(int i) {     
  34. 34.        switch (i) {     
  35. 35.        case 1:     
  36. 36.            System.out.println("one");     
  37. 37.        case 2:     
  38. 38.            System.out.println("two");     
  39. 39.        default:     
  40. 40.            System.out.println("default");// middle default     
  41. 41.        case 3:     
  42. 42.            System.out.println("there");     
  43. 43.    
  44. 44.        }     
  45. 45.    }     
  46. 46.    
  47. 47.    public static void main(String[] args) {     
  48. 48.        // first default     
  49. 49.        testFirst(2);     
  50. 50.        System.out.println("------------------");     
  51. 51.        testFirst(9);     
  52. 52.    
  53. 53.        System.out.println("|||||||||||||||||||||||||||||||||||");     
  54. 54.    
  55. 55.        // last default     
  56. 56.        testLast(2);     
  57. 57.        System.out.println("----------------");     
  58. 58.        testLast(9);     
  59. 59.    
  60. 60.        System.out.println("|||||||||||||||||||||||||||||||||||");     
  61. 61.        // middle default     
  62. 62.        testMiddle(2);     
  63. 63.        System.out.println("----------------");     
  64. 64.        testMiddle(9);     
  65. 65.    
  66. 66.    }     
  67. 67.    
  68. 68.}    
輸入結果:
Java代碼   收藏代碼
  1. 1.two     
  2. 2.there     
  3. 3.------------------     
  4. 4.default    
  5. 5.one     
  6. 6.two     
  7. 7.there     
  8. 8.|||||||||||||||||||||||||||||||||||     
  9. 9.two     
  10. 10.there     
  11. 11.default    
  12. 12.----------------     
  13. 13.default    
  14. 14.|||||||||||||||||||||||||||||||||||     
  15. 15.two     
  16. 16.default    
  17. 17.there     
  18. 18.----------------     
  19. 19.default    
  20. 20.there  

看了結果,可以這樣理解: 
(1)switch語句關鍵地方是進入點,有了進入點沒有break的情況下會執行到底 
(2)沒有匹配值的時候default就是進入點,進入default以后會和普通進入點一樣,如果沒有break繼續執行下面語句 
(3)如果有break 則是從進入點到 break中間的語句執行





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM