java語言分支語句之分支語句

來源:互聯網
上載者:User

標籤:

1. 什麼是java語句的執行結構  

      程式流程式控制制。

2. java語句有哪些結構

      有三種結構:

          順序結構(程式從上到下一行一行的執行代碼,沒有判斷和中轉)

          選擇結構(條件陳述式)          

          迴圈結構:迴圈語句功能(再迴圈條件滿足情況下,反覆執行特定代碼)

               迴圈語句分類(for迴圈   while迴圈   do/while迴圈)

                 for迴圈語句格式:for(初始設定式1;迴圈田間運算式2;迴圈後的運算式)

                {

                  迴圈語句體;

                }  

             具體代碼如下:

              

                         public class Demo10
                              {
                                   public static void main(String[]args)
                                      {
                                          for(int i=0;i<5;i++)
                                               {
                                                    System.out.println(i);
                                                }
                                      }

                               }

                        while迴圈語句格式:while(條件運算式){迴圈體語句}

            具體代碼如下:

                public class Demo10

                    {

                      public static void main(String[]args)

                          {  

                             int i=0;

                             while(i)

                               {

                                System.out.println(i);

                                i++;  

                               }

                          }

                    }

                                          do—while 迴圈語句格式   do

                            {

                               迴圈體語句;                            

                            }

                              while(條件運算式語句);

                    具體代碼如下:

                      public class Demo11

                        {

                           public static void main(String[]args)

                              {

                                int i=0;

                                  do    

                                    {

                                      System.out.println(i)

                                      i++;

                                    }

                                     while(i>5);

                              }

                        }

                        

                

                

3. 掌握分支結構()

4. 掌握if語句的用法   

                                if(條件陳述式){。。。}

            if(條件陳述式){。。。}else{。。。}

            if(條件陳述式){。。。}else if(條件陳述式){。。。}

            if(條件陳述式){。。。}else if(條件陳述式){。。。}else{。。。}

       具體代碼如下:

        public class Demo1

          {

            public static void main(String[]args)

              {

               int i=3;

               if (i>4)

                 {

                    System.out.println(i);

                 } 

              }

          }

5. 掌握switch語句的用法

        switch

          switch(1)

            {

             case 0: 語句塊1;break;

             case 1:語句塊2; break;

             case 2:  語句塊3; break;

               case 3: 語句塊4;break;

              ······

              case n; 語句塊n; break;

              default:語句塊n+1;break;

            }

   具體代碼如下:                

public class Demo9
{
        public static void main(String [] args)
          {
      int i=3; //用來表示第幾季度。
      switch(i)
        {
          case 1:
            System.out.println("這是第一季度");
            break;
          case 2:
            System.out.println("這是第二季度");
                                  break;
          case 3:
            System.out.println("這是第三季度");
            break;
          case 4:
            System.out.println("這是第四季度");
            break;
            default:
              System.out.println("輸出有誤");

            }
      }

}

     條件陳述式—switch語句有關規則

      1 運算式的傳回值必須是以下幾種類型之一:

        int  byte  char  short

      2  case子語句取值必須是常量,且所有case子語句中的取值應是不同的

        3 default語句是可選的

      4 break語句用來在執行完一個case分支後程式跳出switch語句塊  

java語言分支語句之分支語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.