SlideShare a Scribd company logo
บทที่ 3
     โครงสร้า งควบคุม
   (Control Structures)

    อ.ธนิศ า เครือ ไวศยวรรณ
   คณะเทคโนโลยีส ารสนเทศ
สถาบัน เทคโนโลยีพ ระจอมเกล้า เจ้า
       คุณ ทหารลาดกระบัง
วัต ถุป ระสงค์
 แนะนำา คำา สัง โครงสร้า งควบคุม
               ่
 คำา สั่ง if,if..else    และ   if    แบบซ้อ น
 คำา สั่ง switch
 คำา สั่ง while   แล ะ   do..while
 คำา สั่ง for

 โครงสร้า งแบบซ้อ น         (Nested Structure)
 คำา สั่ง break    และ    continue
ลำา ดับ การทำา งานของโปรแกรม
ภาษาจาวามีโ ครงสร้า งควบคุม ทีจ ะกำา หนดลำา ดับ
                              ่
 การทำา งานของคำา สั่ง 3 แบบดัง นี้
 1. โครงสร้า งแบบตามลำา ดับ (Sequential
 Structure)
 2. โครงสร้า งแบบ เลือ กทำา (Selection Structure)
 3. โครงสร้า งแบบทำา ซำ้า (Repetition Structure)
โครงสร้า งแบบตามลำา ดับ
 ภาษาจาวาจะทำา งานตามลำา ดับ ของคำา สัง ทีม อ ยูใ น
                                       ่ ่ ี ่
  โปรแกรม
 เริ่ม ทำา งานจากเมธอดทีช อ ว่า
                         ่ ื่       main()
          public static void main(String args[])

 ทำา งานจากคำา สัง แรกของเมธอด main()
                  ่                            และ
 ทำา งานเรีย งตามลำา ดับ คำา สั่ง ไปเรื่อ ยๆ
โครงสร้า งแบบเลือ กทำา
 ชุด คำา สัง โครงสร้า งแบบเลือ กทำา จะประกอบไป
            ่
 ด้ว ยคำา สั่ง ดัง ต่อ ไปนี้
  • คำา สั่ง   if
  • คำา สั่ง   if..else
  • คำา สั่ง   if แบบซ้อ น   (nested if)
  • คำา สั่ง   switch
รูป แบบของ คำา สัง
                               ่      if

 มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้

      if (logical expression) {
            statements
      }
Flowchart แสดงลำา ดับ การทำา งาน
            ของ คำา สั่ง if


                              จริ                เท็
     ทำา ชุด คำา สั่ง               นิพ จน์      จ ไม่ม ีก ารทำา คำา สั่ง ใด
                              ง
ถ้า นิพ จน์ต รรกศาสตร์              ตรรกศาสตร์
                                                  ถ้า นิพ จน์ต รรกศาสตร
       เป็น จริง                                           เป็น เท็จ
                     ชุด
                   คำา สั่ง
ตัว อย่า ง โปรแกรมที่ใ ช้ค ำา สัง
                                ่              if

   public class SampleIf {
    public class SampleIf {
       public static void main(String args[]) {
        public static void main(String args[]) {
          int score = Integer.parseInt(args[0]);
           int score = Integer.parseInt(args[0]);
          if (score >= 50) {
           if (score >= 50) {
              System.out.println("You pass");
               System.out.println("You pass");
          }}
       }}
   }}
รูป แบบของคำา สัง
                          ่           if..else

 มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้

      if (logical expression) {
            true statements
      } else {
            false statements
      }
Flowchart แสดงลำา ดับ การทำา งาน
           ของ คำา สั่ง if..else
                       จริง                     เท็จ
                                   นิพ จน์
                                  ตรรกศาสตร์
      ทำา ชุด คำา สั่ง                                       ทำา ชุด คำา สั่ง
    สำา หรับ ค่า จริง                                      สำา หรับ ค่า เท็จ
ถ้า นิพ จน์ต รรกศาสตร์                                         ถ้า นิพ จน์
         เป็น จริง                                          ตรรกศาสตร์
              ชุด คำา สั่ง                     ชุด คำา สั่ง เป็น เท็จ
              สำา หรับ ค่า จริง                สำา หรับ ค่า เท็จ
ตัว อย่า ง โปรแกรมที่ใ ช้ค ำา สั่ง
                if..else

 public class SampleIfElse {
  public class SampleIfElse {
     public static void main(String args[]) {
      public static void main(String args[]) {
        int score = Integer.parseInt(args[0]);
         int score = Integer.parseInt(args[0]);
        if (score >= 50) {
         if (score >= 50) {
            System.out.println("You pass");
             System.out.println("You pass");
        } else {
         } else {
            System.out.println("You fail");
             System.out.println("You fail");
        }}
     }}
 }}
รูป แบบของคำา สั่ง         if แบบซ้อ น

 เราสามารถใช้คำา สัง if
                    ่      ทีม ห ลายเงือ นไขได้ ดัง นี้
                             ่ ี       ่
         if (logical expression 1) {
              if (logical expression 2) {
                    statements 1
              } else {
                    statements 2
              }
         } else {
              statements 3
         }
Flowchart ของคำา สั่ง                        if แบบซ้อ น


                                        นิพ จน์ เท็
                      จริ         ตรรกศาสต จ
                      ง           ร์ท ี่ 1
          จริ         นิพ จน์     เท็            ชุด คำา
          ง     ตรรกศาสต          จ             สั่ง ที่ 3
                ร์ท ี่ 2
    ชุด คำา                      ชุด คำา
   สั่ง ที่ 1                   สั่ง ที่ 2
รูป แบบของคำา สั่ง            if   แบบซ้อ น
 เราสามารถใช้คำา สัง if
                    ่      ทีม ห ลายเงือ นไขได้ ดัง นี้
                             ่ ี       ่
         if (logical expression 1) {
              statements 1
         } else {
              if (logical expression 2) {
                    statements 2
              } else {
                    statements 3
              }
         }
รูป แบบของ คำา สัง
                  ่         if..else if..else

 เราสามารถใช้คำา สัง if
                    ่      ทีม ห ลายเงือ นไขได้ ดัง นี้
                             ่ ี       ่
    if (logical expression 1) {
         statements 1
    } else if (logical expression 2) {
         statements 2
    } else {
         statements 3
    }
Flowchart ของคำา สั่ง                if..else
                if..else

                       นิพ จน์ เท็
          จริ    ตรรกศาสต จ
          ง      ร์ท ี่ 1
    ชุด คำา
   สั่ง ที่ 1           จริ         นิพ จน์     เท็
                        ง     ตรรกศาสต          จ
                              ร์ท ี่ 2
                  ชุด คำา                      ชุด คำา
                 สั่ง ที่ 2                   สั่ง ที่ 3
ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง           if..else
                    if..else

  public class SampleIfElseIf {
   public class SampleIfElseIf {
      public static void main(String args[]) {
       public static void main(String args[]) {
         int x = Integer.parseInt(args[0]);
          int x = Integer.parseInt(args[0]);
         if (x == 1) {
          if (x == 1) {
             System.out.println("Value is one");
              System.out.println("Value is one");
         } else if (x == 2) {
          } else if (x == 2) {
             System.out.println("Value is two");
              System.out.println("Value is two");
         } else {
          } else {
             System.out.println("Other than 1 or 2");
              System.out.println("Other than 1 or 2");
         }}
      }}
  }}
รูป แบบของ คำา สัง
                           ่               switch

 มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้

      switch (expression)      {
            case value 1:      statements 1
                                      break;
            case value 2:      statements 2
                                      break;
                  :                   :
            case value N:      statements N
                                      break;
                  default:     statements N+1;
      }
Flowchart แสดงลำา ดับ การทำา งาน
       ของคำา สั่ง switch

    ตัว แปร
              =                     ชุด คำา
              ค่า ที่
                        จริง
                                สั่ง ที่ 1
              1
              =                     ชุด คำา
              ค่า ที่
                         จริง   สั่ง ที่ 2
              2         :
               =                 ชุด คำา สั่ง
               ค่า ที่
                       จริง     ที่ N
               N
              = ค่า    จริง     ชุด คำา สั่ง ที่
              default           N+1
คำา สั่ง   switch

 นิพ จน์ต้อ งมีช นิด ข้อ มูล เป็น char, byte, short   หรือ
  int เท่า นั้น
 ชนิด ข้อ มูล ของนิพ จน์และค่า ที่     1 ถึง N ต้อ งเป็น
    ชนิด เดีย วกัน
 ถ้า ค่า ของนิพ จน์ตรงกับ ค่า ใด      จะทำา ชุด คำา สัง ของ
                                                       ่
  ค่า นัน
        ้
 ถ้า ค่า ของนิพ จน์ไม่ต รงกับ ค่า ใดเลย จะทำา ชุด คำา
  สั่ง ของ default
   default   จะมีห รือ ไม่ม ก ็ไ ด้
                             ี
ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง                switch


public class SampleSwitch {
   public static void main(String args[]) {
      int x = Integer.parseInt(args[0]);
      switch(x) {
         case 1: System.out.println("Value is one");
                          break;
         case 2: System.out.println("Value is two");
                          break;
         default: System.out.println("Other than 1 or 2");
      }
   }
}
โครงสร้า งแบบทำา ซำ้า
 เป็น คำา สั่ง ทีใ ช้ใ นการสั่ง ให้ช ุด คำา สั่ง ใดๆทำา ซำ้า
                  ่
  หลายครั้ง ตามเงื่อ นไขทีร ะบุ
                          ่
 ประกอบด้ว ยคำา สั่ง
   • while
   • do..while
   • for
รูป แบบของ คำา สัง
                            ่             while

 มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้

      initial statements
      while (logical expression)      {
            statements
            update statements
      }
Flowchart แสดงลำา ดับ การทำา งาน
         ของคำา สั่ง while
                                       คำา สั่ง กำา หนด
                             ค่า เริ่ม ต้น

                                     นิพ จน์
     ทำา ชุด คำา สั่ง ซำ้า          ตรรกศาส               เท็จ
ถ้า นิพ จน์ต รรกศาสตร์                  ตร์ จริง
       ยัง เป็น จริง                  ชุด คำา สั่ง


                                      คำา สั่ง
                                เปลี่ย นแปลงค่า
ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง                     while


          public class SampleWhile {
           public class SampleWhile {
          public static void main(String args[]) {
           public static void main(String args[]) {
              int i = 1;
               int i = 1;
              while(i <= 10) {
               while(i <= 10) {
                  System.out.print(i+" ");
                   System.out.print(i+" ");
                  i++;
                   i++;
              }}
          }}
     }}



          ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม
                    ี่
            1   2   3   4   5   6   7   8   9   10
คำา สั่ง    do..while

 มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้

      initial statements
      do {
             statements
             update statements
      } while (logical expression);
Flowchart ของคำา สั่ง                      do..while

                                     มีก ารทำา ชุด คำา สั่ง
                                     อย่า งน้อ ยหนึ่ง ครั้ง เสมอ
                                        ชุด คำา สั่ง
      ทำา ชุด คำา สั่ง ซำ้า
ถ้า นิพ จน์ต รรกศาสตร์                คำา สั่ง
        ยัง เป็น จริง           เปลี่ย นแปลงค่า

                          จริ
                          ง      นิพ จน์
                                 ตรรกศาสตร์
                                         เท็
                                         จ
ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง
                do..while

  public class SampleDoWhile {
   public class SampleDoWhile {
      public static void main(String args[]) {
       public static void main(String args[]) {
         int i = 1;
          int i = 1;
         do {
          do {
             System.out.print(i+" ");
              System.out.print(i+" ");
             i++;
              i++;
         } while (i <= 10);
          } while (i <= 10);
      }}
  }}



    ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม
              ี่
        1   2   3   4   5   6   7   8   9   10
รูป แบบของ คำา สัง
                              ่       for

มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้
   for (initial statements; logical expression;
                                   update statements)
  {
         statements
  }
Flowchart แสดงลำา ดับ การทำา งาน
           ของคำา สั่ง for
                                       คำา สั่ง กำา หนด
                             ค่า เริ่ม ต้น

                                     นิพ จน์
     ทำา ชุด คำา สั่ง ซำ้า          ตรรกศาส               เท็จ
ถ้า นิพ จน์ต รรกศาสตร์                  ตร์ จริง
       ยัง เป็น จริง                  ชุด คำา สั่ง


                                      คำา สั่ง
                                เปลี่ย นแปลงค่า
คำา สั่ง   for

 คำา สั่ง กำา หนดค่า เริ่ม ต้น และคำา สั่ง เปลี่ย นแปลงค่า
  อาจมีม ากกว่า อย่า งละ 1 คำา สั่ง โดยจะใช้
  เครื่อ งหมาย ',' ในการแยกคำา สั่ง
 ตัว อย่า ง
     for (int i=0,j=0; i<4; i++,j+=2) {
           ...
     }
 ขอบเขตของตัว แปรทีป ระกาศในคำา สั่ง กำา หนดค่า
                    ่
  จะใช้ไ ด้เ ฉพาะภายในบล็อ กคำา สั่ง       for   เท่า นัน
                                                        ้
ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง                 for


   public class SampleFor {
    public class SampleFor {
       public static void main(String args[]) {
        public static void main(String args[]) {
          for (int i=1; i<=10; i++) {
           for (int i=1; i<=10; i++) {
              System.out.print(i+" ");
               System.out.print(i+" ");
          }}
       }}
   }}



     ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม
               ี่
        1   2   3   4   5   6   7   8   9   10
ตัว อย่า งโปรแกรมแสดงขอบเขต
            ของตัว แปร
  public class VariableScope {
   public class VariableScope {
      public static void main(String args[]) {
       public static void main(String args[]) {
         for (int i=1; i<10; i++) {
          for (int i=1; i<10; i++) {
             System.out.print(i+" ");
              System.out.print(i+" ");
         }}
         System.out.println("i = "+i); //illegal
          System.out.println("i = "+i); //illegal
      }}
  }}
โครงสร้า งแบบซ้อ น (Nested
             Structure)
 เราสามารถทีจ ะเขีย นคำา สั่ง โครงสร้า งควบคุม ใดๆ
             ่
  ซ้อ นอยูภ ายในได้
          ่
 ตัว อย่า ง   เช่น การเขีย นโครงสร้า งทำา ซำ้า แบบซ้อ น
  (for   อยู่ใ น for)
 โครงสร้า งควบคุม ภายในและภายนอกไม่จ ำา เป็น
  ต้อ งเป็น คำา สั่ง ชนิด เดีย วกัน
ตัว อย่า งโครงสร้า งแบบซ้อ น
              (Nested Loop)
 ต้อ งการจะพิม พ์
   *****
   *****
   *****
 โครงสร้า งทำา ซำ้า ทีอ ยูภ ายใน
                       ่ ่         จะพิม พ์เ ครื่อ งหมาย
 ‘*’   เท่า กับ จำา นวนคอลัม น์ (column)
 โครงสร้า งทำา ซำ้า ทีอ ยูภ ายนอก
                       ่ ่     จะทำา คำา สั่ง
 โครงสร้า งภายในเท่า กับ จำา นวนแถว (row)
ตัว อย่า งโปรแกรมที่ใ ช้ค ำา สั่ง
      โครงสร้า งแบบซ้อ น
   public class NestedFor {
    public class NestedFor {
       public static void main(String args[]) {
        public static void main(String args[]) {
          for (int i=1; i<=3; i++) {
           for (int i=1; i<=3; i++) {
              for (int j=1; j<=5; j++) {
               for (int j=1; j<=5; j++) {
                 System.out.print('*');
                  System.out.print('*');
              }}
              System.out.println();
               System.out.println();
          }}
       }}
   }}


                              ******
ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม
          ี่                  ******
                                   ******
คำา สั่ง   break   และ    continue

 คำา สั่ง break
             จะทำา ให้ห ยุด สิ้น สุด การทำา งานของ
  โครงสร้า งแบบทำา ซำ้า
 ส่ว นคำา สัง continue
             ่      จะข้า มการทำา งานคำา สั่ง ที่เ หลือ
  ภายในบล็อ ก { } โดยไปเริ่ม การทำา ซำ้า ในรอบต่อ
  ไปใหม่
 ตัว อย่า งเช่น
           for(int i = 1; i<= 5; i++) {
                 if (i == 3)
                       break;
                 System.out.println(i);
           }
           for(int i = 1; i<= 5; i++) {
                 if (i == 3)
                       continue;
คำา สั่ง อื่น ๆในการควบคุม loop
 label       : statements;
     •   label เป็น การระบุต ำา แหน่ง ของ loop กรณีท ี่ม ี loop
         ซ้อ นกัน
   break    [label];
     •   คำา สั่ง ให้ส ิ้น สุด การทำา งานใน loop
   continue     [label];
     •    คำา สั่ง ให้ข ้า มการทำา งานของคำา สั่ง ที่เ หลือ ทั้ง หมดใน
         loop
ตัว อย่า งโปรแกรมที่ใ ช้ค ำา สั่ง
           โครงสร้า งแบบซ้อ น
public class SampleBreak2 {
 public class SampleBreak2 {
     public static void main(String args[]) {
      public static void main(String args[]) {
           int i, j, product;
            int i, j, product;
 outer:
  outer:   for (i=1; i<=3; i++) {
            for (i=1; i<=3; i++) {
                  for (j=1; j<=3; j++)
                   for (j=1; j<=3; j++)
                     product = i*j;
                      product = i*j;
                     if (j==3) break outer;
                      if (j==3) break outer;
                         System.out.println(i+" * "+j+" = "+product);
                          System.out.println(i+" * "+j+" = "+product);
                  }}
             }}
            System.out.println("Outside nested loops.");
             System.out.println("Outside nested loops.");
        }}
}}


                               1 * 1 = 1
 ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม 2 = 2
           ี่                  1 *
                               Outside nested loops.
สรุป เนื้อ หาของบท
 คำา สั่ง โครงสร้า งควบคุม      เป็น คำา สัง ทีใ ช้ใ นการ
                                            ่ ่
  กำา หนดลำา ดับ การทำา งานของคำา สัง ต่า งๆ โดยมี
                                             ่
  สามรูป แบบคือ โครงสร้า งแบบตามลำา ดับ
  โครงสร้า งแบบเลือ กทำา และ โครงสร้า งแบบทำา ซำ้า
 คำา สั่ง ทีเ ป็น คำา สั่ง ของโครงสร้า งแบบเลือ กทำา คือ
             ่
  คำา สั่ง if,if..else หรือ switch
 คำา สั่ง if..else แตกต่า งจากคำา สัง if ตรงที่ค ำา สัง
                                        ่                  ่
  if..else จะมีก ารทำา คำา สั่ง สำา หรับ ค่า เท็จ ถ้า นิพ จน์
  ตรรกศาสตร์เ ป็น เท็จ ส่ว นคำา สัง if จะไม่ม ก ารทำา
                                      ่              ี
  คำา สั่ง ใดถ้า นิพ จน์ต รรกศาสตร์เ ป็น เท็จ
 คำา สั่ง if   หรือ   if..else   สามารถทีจ ะซ้อ นอยูข ้า งใน
                                           ่         ่
  คำา สั่ง if   หรือ   if..else   อื่น ได้
สรุป เนื้อ หาของบท
 คำา สั่ง switch   จะมีล ัก ษณะโครงสร้า งการทำา งาน
  คล้า ยคลึง กับ คำา สั่ง if..else if..else.. แต่ช นิด
  ข้อ มูล ของตัว แปรทีจ ะนำา มาใช้ก ับ คำา สั่ง switch จะ
                              ่
  ต้อ งเป็น ชนิด char, byte, short หรือ int เท่า นั้น
 คำา สั่ง ทีเ ป็น คำา สั่ง ของโครงสร้า งแบบทำา ซำ้า คือ คำา
             ่
  สั่ง while,do..while หรือ for
 คำา สั่ง while แตกต่า งจากคำา สัง do..while ตรงที่ คำา
                                       ่
  สั่ง while จะไม่ม ก ารทำา ชุด คำา สั่ง เลยถ้า นิพ จน์
                          ี
  ตรรกศาสตร์เ ป็น เท็จ ส่ว นคำา สัง do..while จะมีก าร
                                         ่
  ทำา ชุด คำา สัง หนึง ครั้ง ถ้า นิพ จน์ต รรกศาสตร์เ ป็น เท็จ
                  ่     ่
 คำา สั่ง for มีล ัก ษณะการทำา งานทีเ หมือ นกับ คำา สั่ง
                                           ่
  while แต่จ ะมีก ารรวมคำา สัง กำา หนดค่า เริ่ม ต้น
                                   ่
  นิพ จน์ต รรกศาสตร์แ ละคำา สั่ง เปลี่ย นแปลงค่า ไว้
สรุป เนื้อ หาของบท
 คำา สั่ง for    จะใช้ใ นกรณีท ท ราบจำา นวนครั้ง ในการ
                                    ี่
  ทำา ซำ้า ที่แ น่น อน ส่ว นคำา สัง while หรือ do..while
                                  ่
  นิย มใช้ใ นกรณีท ไ ม่ท ราบจำา นวนครั้ง ในการทำา ซำ้า
                         ี่
  ล่ว งหน้า
 คำา สั่ง โครงสร้า งทัง หมดทีก ล่า วมาแล้ว ข้า งต้น
                            ้          ่
  สามารถนำา มาใช้ร ่ว มกัน เป็น ลัก ษณะแบบซ้อ นได้
  เช่น คำา สัง switch อยูภ ายในคำา สั่ง while หรือ คำา สั่ง
                ่               ่
  for อยูภ ายในคำา สั่ง for ซึ่ง เรีย กโครงสร้า งใน
            ่
  ลัก ษณะนี้ว ่า โครงสร้า งแบบซ้อ น
 คำา สั่ง break จะทำา ให้ห ยุด สิน สุด การทำา งานของ
                                         ้
  โครงสร้า งแบบทำา ซำ้า ส่ว นคำา สั่ง continue จะข้า ม
  การทำา งานคำา สั่ง ทีเ หลือ ภายในบล็อ ก { } โดยไป
                              ่
  เริ่ม การทำา ซำ้า ในรอบต่อ ไปใหม่
แบบฝึก หัด
 แบบฝึก หัด ที่   1 การใช้ค ำา สั่ง   if..else
   • ใช้ค ำา สั่ง if..else เพื่อ ตรวจสอบค่า ของ b2-4ac (โดย
     รับ ค่า a,b และ c เข้า มาทาง command line) ถ้า มีค ่า
    เป็น ลบให้พ ิม พ์ข ้อ ความว่า No answer แต่ถ ้า มีค ่า เป็น
    บวกหรือ ศูน ย์ใ ห้ค ำา นวณหาคำา ตอบและพิม พ์ค ำา ตอบทั้ง
    สองค่า ออกมาทางจอภาพ
 แบบฝึก หัด ที่   2 การใช้ค ำา สั่ง   if   แบบซ้อ น
   • จงเขีย นโปรแกรมคำา นวณหาและพิม พ์ค ่า เกรดที่เ หมาะ
    สมตามคะแนนที่ร ับ เข้า มาจากผู้ใ ช้ โดยมีเ กณฑ์ต ามนี้
     • คะแนนตั้ง แต่ 90 ถึง 100       ได้เ กรด A
     • คะแนนตั้ง แต่ 80 ถึง 89 ได้เ กรด B
     • คะแนนตั้ง แต่ 70 ถึง 79 ได้เ กรด C
     • คะแนนตั้ง แต่ 60 ถึง 69 ได้เ กรด D
     • คะแนนที่ต ำ่า กว่า 60          ได้เ กรด F
แบบฝึก หัด
 แบบฝึก หัด ที่    3 การใช้ค ำา สั่ง     for   แบบซ้อ น
   • จงเขีย นโปรแกรมภาษาจาวาโดยใช้ค ำา สั่ง for        แบบ
    ซ้อ นเพื่อ พิม พ์ร ูป ดัง ต่อ ไปนี้
      1
      2    2
      3    3       3
      4    4       4      4
      5    5       5      5      5

More Related Content

PPT
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
KEk YourJust'one
 
PDF
Tech30101 ch8
Pawida Chumpurat
 
PDF
02 controlflow php
JubNatthawan Phoonson
 
PDF
Java-Chapter 04 Iteration Statements
Wongyos Keardsri
 
PDF
Java Programming: คลาสอินพุตและเอาต์พุต
Thanachart Numnonda
 
PDF
Java-Chapter 09 Advanced Statements and Applications
Wongyos Keardsri
 
PDF
Java Programming: อะเรย์และคอลเล็กชั่น
Thanachart Numnonda
 
PPT
ตัวแปรชุดและตัวแปรอักขระ PPT
Areeya Onnom
 
การเขียนคำสั่งควบคุมแบบมีทางเลือก กลุ่ม 3
KEk YourJust'one
 
Tech30101 ch8
Pawida Chumpurat
 
02 controlflow php
JubNatthawan Phoonson
 
Java-Chapter 04 Iteration Statements
Wongyos Keardsri
 
Java Programming: คลาสอินพุตและเอาต์พุต
Thanachart Numnonda
 
Java-Chapter 09 Advanced Statements and Applications
Wongyos Keardsri
 
Java Programming: อะเรย์และคอลเล็กชั่น
Thanachart Numnonda
 
ตัวแปรชุดและตัวแปรอักขระ PPT
Areeya Onnom
 

What's hot (19)

PPT
ตัวแปรชุดและตัวแปรอักขระ
Areeya Onnom
 
PDF
9789740328766
CUPress
 
PDF
9789740328766
Chirawat Wangka
 
PDF
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Thanachart Numnonda
 
PDF
Java Programming: หลักการเชิงอ็อบเจกต์
Thanachart Numnonda
 
PDF
บทที่ 6 อาร์เรย์
Theeravaj Tum
 
PDF
Java-Chapter 07 One Dimensional Arrays
Wongyos Keardsri
 
PDF
Java-Chapter 05 String Operations
Wongyos Keardsri
 
PPT
งานนำเสนอ1อ ทรงศักดิ์
Aeew Autaporn
 
DOC
แบบฝึกทักษะเรื่อง จำนวนเชิงซ้อน
chatchai
 
PDF
Java-Chapter 02 Data Operations and Processing
Wongyos Keardsri
 
PPT
Java Programming [8/12] : Arrays and Collection
IMC Institute
 
PPT
งานนำเสนอ1
Mittapan Chantanyakan
 
PDF
งานนำเสนอ1
prapassonmook
 
PDF
Java-Chapter 12 Classes and Objects
Wongyos Keardsri
 
PPT
ความรู้เบื้องต้นเกี่ยวกับโครงสร้างข้อมูลและอัลกอริทึม
waradakhantee
 
PDF
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
 
ตัวแปรชุดและตัวแปรอักขระ
Areeya Onnom
 
9789740328766
CUPress
 
9789740328766
Chirawat Wangka
 
Java Programming: การเขียนโปรแกรมภาษาจาวาเชิงอ็อบเจกต์
Thanachart Numnonda
 
Java Programming: หลักการเชิงอ็อบเจกต์
Thanachart Numnonda
 
บทที่ 6 อาร์เรย์
Theeravaj Tum
 
Java-Chapter 07 One Dimensional Arrays
Wongyos Keardsri
 
Java-Chapter 05 String Operations
Wongyos Keardsri
 
งานนำเสนอ1อ ทรงศักดิ์
Aeew Autaporn
 
แบบฝึกทักษะเรื่อง จำนวนเชิงซ้อน
chatchai
 
Java-Chapter 02 Data Operations and Processing
Wongyos Keardsri
 
Java Programming [8/12] : Arrays and Collection
IMC Institute
 
งานนำเสนอ1
Mittapan Chantanyakan
 
งานนำเสนอ1
prapassonmook
 
Java-Chapter 12 Classes and Objects
Wongyos Keardsri
 
ความรู้เบื้องต้นเกี่ยวกับโครงสร้างข้อมูลและอัลกอริทึม
waradakhantee
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
 
Ad

Viewers also liked (17)

PPT
Java Programming [11/12] : Input and Output Classes
IMC Institute
 
PPT
๋Java Programming [7/12] : GUI Event Handling
IMC Institute
 
PPT
Java Programming [12/12] : Thread
IMC Institute
 
PPT
Java Programming [10/12]: Java Applet
IMC Institute
 
PDF
Hand-on Exercise Java Web Programming
IMC Institute
 
PPT
Java Programming [6/12] : Object Oriented Java Programming
IMC Institute
 
PPT
Java Programming [5/12] : Build Graphical User Interface
IMC Institute
 
PDF
Cloud Computing ฉบับ SME
IMC Institute
 
PPT
Java Programming [2/12] : Overview of Java Programming Language
IMC Institute
 
PPT
Java Programming [9/12]: Exception Handling
IMC Institute
 
PPT
Java Programming [4/12] : Object Oriented Concept
IMC Institute
 
PDF
การบริหารจัดการระบบ Cloud Computing สำหรับองค์กรธุรกิจ SME
IMC Institute
 
PDF
Introduction to Big Data
IMC Institute
 
PDF
Introduction to Data Mining, Business Intelligence and Data Science
IMC Institute
 
PDF
การใช้เทคโนโลยี 3G กับงานด้านสังคม
IMC Institute
 
PDF
IT Security Trends 2013
IMC Institute
 
PDF
ICT Overview & Opportunity in Thailand
IMC Institute
 
Java Programming [11/12] : Input and Output Classes
IMC Institute
 
๋Java Programming [7/12] : GUI Event Handling
IMC Institute
 
Java Programming [12/12] : Thread
IMC Institute
 
Java Programming [10/12]: Java Applet
IMC Institute
 
Hand-on Exercise Java Web Programming
IMC Institute
 
Java Programming [6/12] : Object Oriented Java Programming
IMC Institute
 
Java Programming [5/12] : Build Graphical User Interface
IMC Institute
 
Cloud Computing ฉบับ SME
IMC Institute
 
Java Programming [2/12] : Overview of Java Programming Language
IMC Institute
 
Java Programming [9/12]: Exception Handling
IMC Institute
 
Java Programming [4/12] : Object Oriented Concept
IMC Institute
 
การบริหารจัดการระบบ Cloud Computing สำหรับองค์กรธุรกิจ SME
IMC Institute
 
Introduction to Big Data
IMC Institute
 
Introduction to Data Mining, Business Intelligence and Data Science
IMC Institute
 
การใช้เทคโนโลยี 3G กับงานด้านสังคม
IMC Institute
 
IT Security Trends 2013
IMC Institute
 
ICT Overview & Opportunity in Thailand
IMC Institute
 
Ad

Similar to Java Programming [3/12]: Control Structures (20)

PDF
เทอม 2 คาบ 10 หลักการแก้ปัญหาด้วยคอมพิวเตอร์
Mrpopovic Popovic
 
PPTX
Computer Programming 3
Saranyu Srisrontong
 
PPTX
บทที่3 การควบคุมโปรแกรม
pennapa34
 
PDF
Java Programming: โครงสร้างควบคุม
Thanachart Numnonda
 
PDF
chapter 3 คำสั่งควบคุม
อัครเดช โพธิญาณ์
 
PPT
โปรแกรมแปลงค่าเงิน JAVA
Tor' Thepparat
 
PDF
งานทำ Blog บทที่ 6
รัสนา สิงหปรีชา
 
PPT
04 conditional
a-num Sara
 
PDF
Programming
sa
 
PPTX
เอสสสสสส (1)
Siriwan Wisetsing
 
PPT
โปรแกรมแปลงค่าเงิน JAVA
Tor' Thepparat
 
PDF
บทที่ 3 คำสั่งควบคุมโปรแกรม
Sutinun Goodour
 
PPSX
บทที่1ProgramFlowchart สำหรับนักเรียนชั้นมัธยม
bessza011
 
PPT
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
อัครเดช โพธิญาณ์
 
PDF
ฟังก์ชั่น If หลายทางเลือก
เทวัญ ภูพานทอง
 
PDF
Know009
the pooh
 
PPT
Control structure
Nitigan Nakjuatong
 
PPTX
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
View Nudchanad
 
เทอม 2 คาบ 10 หลักการแก้ปัญหาด้วยคอมพิวเตอร์
Mrpopovic Popovic
 
Computer Programming 3
Saranyu Srisrontong
 
บทที่3 การควบคุมโปรแกรม
pennapa34
 
Java Programming: โครงสร้างควบคุม
Thanachart Numnonda
 
chapter 3 คำสั่งควบคุม
อัครเดช โพธิญาณ์
 
โปรแกรมแปลงค่าเงิน JAVA
Tor' Thepparat
 
งานทำ Blog บทที่ 6
รัสนา สิงหปรีชา
 
04 conditional
a-num Sara
 
Programming
sa
 
เอสสสสสส (1)
Siriwan Wisetsing
 
โปรแกรมแปลงค่าเงิน JAVA
Tor' Thepparat
 
บทที่ 3 คำสั่งควบคุมโปรแกรม
Sutinun Goodour
 
บทที่1ProgramFlowchart สำหรับนักเรียนชั้นมัธยม
bessza011
 
บทที่ 3 คำสั่งควบคุม ส่วนที่ 1
อัครเดช โพธิญาณ์
 
ฟังก์ชั่น If หลายทางเลือก
เทวัญ ภูพานทอง
 
Know009
the pooh
 
Control structure
Nitigan Nakjuatong
 
บทที่ 6-เงื่อนไข-การตัดสินใจ-การควบคุม
View Nudchanad
 

More from IMC Institute (20)

PDF
นิตยสาร Digital Trends ฉบับที่ 14
IMC Institute
 
PDF
Digital trends Vol 4 No. 13 Sep-Dec 2019
IMC Institute
 
PDF
บทความ The evolution of AI
IMC Institute
 
PDF
IT Trends eMagazine Vol 4. No.12
IMC Institute
 
PDF
เพราะเหตุใด Digitization ไม่ตอบโจทย์ Digital Transformation
IMC Institute
 
PDF
IT Trends 2019: Putting Digital Transformation to Work
IMC Institute
 
PDF
มูลค่าตลาดดิจิทัลไทย 3 อุตสาหกรรม
IMC Institute
 
PDF
IT Trends eMagazine Vol 4. No.11
IMC Institute
 
PDF
แนวทางการทำ Digital transformation
IMC Institute
 
PDF
บทความ The New Silicon Valley
IMC Institute
 
PDF
นิตยสาร IT Trends ของ IMC Institute ฉบับที่ 10
IMC Institute
 
PDF
แนวทางการทำ Digital transformation
IMC Institute
 
PDF
The Power of Big Data for a new economy (Sample)
IMC Institute
 
PDF
บทความ Robotics แนวโน้มใหม่สู่บริการเฉพาะทาง
IMC Institute
 
PDF
IT Trends eMagazine Vol 3. No.9
IMC Institute
 
PDF
Thailand software & software market survey 2016
IMC Institute
 
PPTX
Developing Business Blockchain Applications on Hyperledger
IMC Institute
 
PDF
Digital transformation @thanachart.org
IMC Institute
 
PDF
บทความ Big Data จากบล็อก thanachart.org
IMC Institute
 
PDF
กลยุทธ์ 5 ด้านกับการทำ Digital Transformation
IMC Institute
 
นิตยสาร Digital Trends ฉบับที่ 14
IMC Institute
 
Digital trends Vol 4 No. 13 Sep-Dec 2019
IMC Institute
 
บทความ The evolution of AI
IMC Institute
 
IT Trends eMagazine Vol 4. No.12
IMC Institute
 
เพราะเหตุใด Digitization ไม่ตอบโจทย์ Digital Transformation
IMC Institute
 
IT Trends 2019: Putting Digital Transformation to Work
IMC Institute
 
มูลค่าตลาดดิจิทัลไทย 3 อุตสาหกรรม
IMC Institute
 
IT Trends eMagazine Vol 4. No.11
IMC Institute
 
แนวทางการทำ Digital transformation
IMC Institute
 
บทความ The New Silicon Valley
IMC Institute
 
นิตยสาร IT Trends ของ IMC Institute ฉบับที่ 10
IMC Institute
 
แนวทางการทำ Digital transformation
IMC Institute
 
The Power of Big Data for a new economy (Sample)
IMC Institute
 
บทความ Robotics แนวโน้มใหม่สู่บริการเฉพาะทาง
IMC Institute
 
IT Trends eMagazine Vol 3. No.9
IMC Institute
 
Thailand software & software market survey 2016
IMC Institute
 
Developing Business Blockchain Applications on Hyperledger
IMC Institute
 
Digital transformation @thanachart.org
IMC Institute
 
บทความ Big Data จากบล็อก thanachart.org
IMC Institute
 
กลยุทธ์ 5 ด้านกับการทำ Digital Transformation
IMC Institute
 

Java Programming [3/12]: Control Structures

  • 1. บทที่ 3 โครงสร้า งควบคุม (Control Structures) อ.ธนิศ า เครือ ไวศยวรรณ คณะเทคโนโลยีส ารสนเทศ สถาบัน เทคโนโลยีพ ระจอมเกล้า เจ้า คุณ ทหารลาดกระบัง
  • 2. วัต ถุป ระสงค์  แนะนำา คำา สัง โครงสร้า งควบคุม ่  คำา สั่ง if,if..else และ if แบบซ้อ น  คำา สั่ง switch  คำา สั่ง while แล ะ do..while  คำา สั่ง for  โครงสร้า งแบบซ้อ น (Nested Structure)  คำา สั่ง break และ continue
  • 3. ลำา ดับ การทำา งานของโปรแกรม ภาษาจาวามีโ ครงสร้า งควบคุม ทีจ ะกำา หนดลำา ดับ ่ การทำา งานของคำา สั่ง 3 แบบดัง นี้ 1. โครงสร้า งแบบตามลำา ดับ (Sequential Structure) 2. โครงสร้า งแบบ เลือ กทำา (Selection Structure) 3. โครงสร้า งแบบทำา ซำ้า (Repetition Structure)
  • 4. โครงสร้า งแบบตามลำา ดับ  ภาษาจาวาจะทำา งานตามลำา ดับ ของคำา สัง ทีม อ ยูใ น ่ ่ ี ่ โปรแกรม  เริ่ม ทำา งานจากเมธอดทีช อ ว่า ่ ื่ main() public static void main(String args[])  ทำา งานจากคำา สัง แรกของเมธอด main() ่ และ ทำา งานเรีย งตามลำา ดับ คำา สั่ง ไปเรื่อ ยๆ
  • 5. โครงสร้า งแบบเลือ กทำา  ชุด คำา สัง โครงสร้า งแบบเลือ กทำา จะประกอบไป ่ ด้ว ยคำา สั่ง ดัง ต่อ ไปนี้ • คำา สั่ง if • คำา สั่ง if..else • คำา สั่ง if แบบซ้อ น (nested if) • คำา สั่ง switch
  • 6. รูป แบบของ คำา สัง ่ if  มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้ if (logical expression) { statements }
  • 7. Flowchart แสดงลำา ดับ การทำา งาน ของ คำา สั่ง if จริ เท็ ทำา ชุด คำา สั่ง นิพ จน์ จ ไม่ม ีก ารทำา คำา สั่ง ใด ง ถ้า นิพ จน์ต รรกศาสตร์ ตรรกศาสตร์ ถ้า นิพ จน์ต รรกศาสตร เป็น จริง เป็น เท็จ ชุด คำา สั่ง
  • 8. ตัว อย่า ง โปรแกรมที่ใ ช้ค ำา สัง ่ if public class SampleIf { public class SampleIf { public static void main(String args[]) { public static void main(String args[]) { int score = Integer.parseInt(args[0]); int score = Integer.parseInt(args[0]); if (score >= 50) { if (score >= 50) { System.out.println("You pass"); System.out.println("You pass"); }} }} }}
  • 9. รูป แบบของคำา สัง ่ if..else  มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้ if (logical expression) { true statements } else { false statements }
  • 10. Flowchart แสดงลำา ดับ การทำา งาน ของ คำา สั่ง if..else จริง เท็จ นิพ จน์ ตรรกศาสตร์ ทำา ชุด คำา สั่ง ทำา ชุด คำา สั่ง สำา หรับ ค่า จริง สำา หรับ ค่า เท็จ ถ้า นิพ จน์ต รรกศาสตร์ ถ้า นิพ จน์ เป็น จริง ตรรกศาสตร์ ชุด คำา สั่ง ชุด คำา สั่ง เป็น เท็จ สำา หรับ ค่า จริง สำา หรับ ค่า เท็จ
  • 11. ตัว อย่า ง โปรแกรมที่ใ ช้ค ำา สั่ง if..else public class SampleIfElse { public class SampleIfElse { public static void main(String args[]) { public static void main(String args[]) { int score = Integer.parseInt(args[0]); int score = Integer.parseInt(args[0]); if (score >= 50) { if (score >= 50) { System.out.println("You pass"); System.out.println("You pass"); } else { } else { System.out.println("You fail"); System.out.println("You fail"); }} }} }}
  • 12. รูป แบบของคำา สั่ง if แบบซ้อ น  เราสามารถใช้คำา สัง if ่ ทีม ห ลายเงือ นไขได้ ดัง นี้ ่ ี ่ if (logical expression 1) { if (logical expression 2) { statements 1 } else { statements 2 } } else { statements 3 }
  • 13. Flowchart ของคำา สั่ง if แบบซ้อ น นิพ จน์ เท็ จริ ตรรกศาสต จ ง ร์ท ี่ 1 จริ นิพ จน์ เท็ ชุด คำา ง ตรรกศาสต จ สั่ง ที่ 3 ร์ท ี่ 2 ชุด คำา ชุด คำา สั่ง ที่ 1 สั่ง ที่ 2
  • 14. รูป แบบของคำา สั่ง if แบบซ้อ น  เราสามารถใช้คำา สัง if ่ ทีม ห ลายเงือ นไขได้ ดัง นี้ ่ ี ่ if (logical expression 1) { statements 1 } else { if (logical expression 2) { statements 2 } else { statements 3 } }
  • 15. รูป แบบของ คำา สัง ่ if..else if..else  เราสามารถใช้คำา สัง if ่ ทีม ห ลายเงือ นไขได้ ดัง นี้ ่ ี ่ if (logical expression 1) { statements 1 } else if (logical expression 2) { statements 2 } else { statements 3 }
  • 16. Flowchart ของคำา สั่ง if..else if..else นิพ จน์ เท็ จริ ตรรกศาสต จ ง ร์ท ี่ 1 ชุด คำา สั่ง ที่ 1 จริ นิพ จน์ เท็ ง ตรรกศาสต จ ร์ท ี่ 2 ชุด คำา ชุด คำา สั่ง ที่ 2 สั่ง ที่ 3
  • 17. ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง if..else if..else public class SampleIfElseIf { public class SampleIfElseIf { public static void main(String args[]) { public static void main(String args[]) { int x = Integer.parseInt(args[0]); int x = Integer.parseInt(args[0]); if (x == 1) { if (x == 1) { System.out.println("Value is one"); System.out.println("Value is one"); } else if (x == 2) { } else if (x == 2) { System.out.println("Value is two"); System.out.println("Value is two"); } else { } else { System.out.println("Other than 1 or 2"); System.out.println("Other than 1 or 2"); }} }} }}
  • 18. รูป แบบของ คำา สัง ่ switch  มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้ switch (expression) { case value 1: statements 1 break; case value 2: statements 2 break; : : case value N: statements N break; default: statements N+1; }
  • 19. Flowchart แสดงลำา ดับ การทำา งาน ของคำา สั่ง switch ตัว แปร = ชุด คำา ค่า ที่ จริง สั่ง ที่ 1 1 = ชุด คำา ค่า ที่ จริง สั่ง ที่ 2 2 : = ชุด คำา สั่ง ค่า ที่ จริง ที่ N N = ค่า จริง ชุด คำา สั่ง ที่ default N+1
  • 20. คำา สั่ง switch  นิพ จน์ต้อ งมีช นิด ข้อ มูล เป็น char, byte, short หรือ int เท่า นั้น  ชนิด ข้อ มูล ของนิพ จน์และค่า ที่ 1 ถึง N ต้อ งเป็น ชนิด เดีย วกัน  ถ้า ค่า ของนิพ จน์ตรงกับ ค่า ใด จะทำา ชุด คำา สัง ของ ่ ค่า นัน ้  ถ้า ค่า ของนิพ จน์ไม่ต รงกับ ค่า ใดเลย จะทำา ชุด คำา สั่ง ของ default  default จะมีห รือ ไม่ม ก ็ไ ด้ ี
  • 21. ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง switch public class SampleSwitch { public static void main(String args[]) { int x = Integer.parseInt(args[0]); switch(x) { case 1: System.out.println("Value is one"); break; case 2: System.out.println("Value is two"); break; default: System.out.println("Other than 1 or 2"); } } }
  • 22. โครงสร้า งแบบทำา ซำ้า  เป็น คำา สั่ง ทีใ ช้ใ นการสั่ง ให้ช ุด คำา สั่ง ใดๆทำา ซำ้า ่ หลายครั้ง ตามเงื่อ นไขทีร ะบุ ่  ประกอบด้ว ยคำา สั่ง • while • do..while • for
  • 23. รูป แบบของ คำา สัง ่ while  มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้ initial statements while (logical expression) { statements update statements }
  • 24. Flowchart แสดงลำา ดับ การทำา งาน ของคำา สั่ง while คำา สั่ง กำา หนด ค่า เริ่ม ต้น นิพ จน์ ทำา ชุด คำา สั่ง ซำ้า ตรรกศาส เท็จ ถ้า นิพ จน์ต รรกศาสตร์ ตร์ จริง ยัง เป็น จริง ชุด คำา สั่ง คำา สั่ง เปลี่ย นแปลงค่า
  • 25. ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง while public class SampleWhile { public class SampleWhile { public static void main(String args[]) { public static void main(String args[]) { int i = 1; int i = 1; while(i <= 10) { while(i <= 10) { System.out.print(i+" "); System.out.print(i+" "); i++; i++; }} }} }} ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม ี่ 1 2 3 4 5 6 7 8 9 10
  • 26. คำา สั่ง do..while  มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้ initial statements do { statements update statements } while (logical expression);
  • 27. Flowchart ของคำา สั่ง do..while มีก ารทำา ชุด คำา สั่ง อย่า งน้อ ยหนึ่ง ครั้ง เสมอ ชุด คำา สั่ง ทำา ชุด คำา สั่ง ซำ้า ถ้า นิพ จน์ต รรกศาสตร์ คำา สั่ง ยัง เป็น จริง เปลี่ย นแปลงค่า จริ ง นิพ จน์ ตรรกศาสตร์ เท็ จ
  • 28. ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง do..while public class SampleDoWhile { public class SampleDoWhile { public static void main(String args[]) { public static void main(String args[]) { int i = 1; int i = 1; do { do { System.out.print(i+" "); System.out.print(i+" "); i++; i++; } while (i <= 10); } while (i <= 10); }} }} ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม ี่ 1 2 3 4 5 6 7 8 9 10
  • 29. รูป แบบของ คำา สัง ่ for มีร ูป แบบการใช้ค ำา สั่ง ดัง นี้ for (initial statements; logical expression; update statements) { statements }
  • 30. Flowchart แสดงลำา ดับ การทำา งาน ของคำา สั่ง for คำา สั่ง กำา หนด ค่า เริ่ม ต้น นิพ จน์ ทำา ชุด คำา สั่ง ซำ้า ตรรกศาส เท็จ ถ้า นิพ จน์ต รรกศาสตร์ ตร์ จริง ยัง เป็น จริง ชุด คำา สั่ง คำา สั่ง เปลี่ย นแปลงค่า
  • 31. คำา สั่ง for  คำา สั่ง กำา หนดค่า เริ่ม ต้น และคำา สั่ง เปลี่ย นแปลงค่า อาจมีม ากกว่า อย่า งละ 1 คำา สั่ง โดยจะใช้ เครื่อ งหมาย ',' ในการแยกคำา สั่ง  ตัว อย่า ง for (int i=0,j=0; i<4; i++,j+=2) { ... }  ขอบเขตของตัว แปรทีป ระกาศในคำา สั่ง กำา หนดค่า ่ จะใช้ไ ด้เ ฉพาะภายในบล็อ กคำา สั่ง for เท่า นัน ้
  • 32. ตัว อย่า งโปรแกรมที่ใช้ค ำา สั่ง for public class SampleFor { public class SampleFor { public static void main(String args[]) { public static void main(String args[]) { for (int i=1; i<=10; i++) { for (int i=1; i<=10; i++) { System.out.print(i+" "); System.out.print(i+" "); }} }} }} ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม ี่ 1 2 3 4 5 6 7 8 9 10
  • 33. ตัว อย่า งโปรแกรมแสดงขอบเขต ของตัว แปร public class VariableScope { public class VariableScope { public static void main(String args[]) { public static void main(String args[]) { for (int i=1; i<10; i++) { for (int i=1; i<10; i++) { System.out.print(i+" "); System.out.print(i+" "); }} System.out.println("i = "+i); //illegal System.out.println("i = "+i); //illegal }} }}
  • 34. โครงสร้า งแบบซ้อ น (Nested Structure)  เราสามารถทีจ ะเขีย นคำา สั่ง โครงสร้า งควบคุม ใดๆ ่ ซ้อ นอยูภ ายในได้ ่  ตัว อย่า ง เช่น การเขีย นโครงสร้า งทำา ซำ้า แบบซ้อ น (for อยู่ใ น for)  โครงสร้า งควบคุม ภายในและภายนอกไม่จ ำา เป็น ต้อ งเป็น คำา สั่ง ชนิด เดีย วกัน
  • 35. ตัว อย่า งโครงสร้า งแบบซ้อ น (Nested Loop)  ต้อ งการจะพิม พ์ ***** ***** *****  โครงสร้า งทำา ซำ้า ทีอ ยูภ ายใน ่ ่ จะพิม พ์เ ครื่อ งหมาย ‘*’ เท่า กับ จำา นวนคอลัม น์ (column)  โครงสร้า งทำา ซำ้า ทีอ ยูภ ายนอก ่ ่ จะทำา คำา สั่ง โครงสร้า งภายในเท่า กับ จำา นวนแถว (row)
  • 36. ตัว อย่า งโปรแกรมที่ใ ช้ค ำา สั่ง โครงสร้า งแบบซ้อ น public class NestedFor { public class NestedFor { public static void main(String args[]) { public static void main(String args[]) { for (int i=1; i<=3; i++) { for (int i=1; i<=3; i++) { for (int j=1; j<=5; j++) { for (int j=1; j<=5; j++) { System.out.print('*'); System.out.print('*'); }} System.out.println(); System.out.println(); }} }} }} ****** ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม ี่ ****** ******
  • 37. คำา สั่ง break และ continue  คำา สั่ง break จะทำา ให้ห ยุด สิ้น สุด การทำา งานของ โครงสร้า งแบบทำา ซำ้า  ส่ว นคำา สัง continue ่ จะข้า มการทำา งานคำา สั่ง ที่เ หลือ ภายในบล็อ ก { } โดยไปเริ่ม การทำา ซำ้า ในรอบต่อ ไปใหม่  ตัว อย่า งเช่น for(int i = 1; i<= 5; i++) { if (i == 3) break; System.out.println(i); } for(int i = 1; i<= 5; i++) { if (i == 3) continue;
  • 38. คำา สั่ง อื่น ๆในการควบคุม loop  label : statements; • label เป็น การระบุต ำา แหน่ง ของ loop กรณีท ี่ม ี loop ซ้อ นกัน  break [label]; • คำา สั่ง ให้ส ิ้น สุด การทำา งานใน loop  continue [label]; • คำา สั่ง ให้ข ้า มการทำา งานของคำา สั่ง ที่เ หลือ ทั้ง หมดใน loop
  • 39. ตัว อย่า งโปรแกรมที่ใ ช้ค ำา สั่ง โครงสร้า งแบบซ้อ น public class SampleBreak2 { public class SampleBreak2 { public static void main(String args[]) { public static void main(String args[]) { int i, j, product; int i, j, product; outer: outer: for (i=1; i<=3; i++) { for (i=1; i<=3; i++) { for (j=1; j<=3; j++) for (j=1; j<=3; j++) product = i*j; product = i*j; if (j==3) break outer; if (j==3) break outer; System.out.println(i+" * "+j+" = "+product); System.out.println(i+" * "+j+" = "+product); }} }} System.out.println("Outside nested loops."); System.out.println("Outside nested loops."); }} }} 1 * 1 = 1 ผลลัพ ธ์ท ไ ด้จ ากการรัน โปรแกรม 2 = 2 ี่ 1 * Outside nested loops.
  • 40. สรุป เนื้อ หาของบท  คำา สั่ง โครงสร้า งควบคุม เป็น คำา สัง ทีใ ช้ใ นการ ่ ่ กำา หนดลำา ดับ การทำา งานของคำา สัง ต่า งๆ โดยมี ่ สามรูป แบบคือ โครงสร้า งแบบตามลำา ดับ โครงสร้า งแบบเลือ กทำา และ โครงสร้า งแบบทำา ซำ้า  คำา สั่ง ทีเ ป็น คำา สั่ง ของโครงสร้า งแบบเลือ กทำา คือ ่ คำา สั่ง if,if..else หรือ switch  คำา สั่ง if..else แตกต่า งจากคำา สัง if ตรงที่ค ำา สัง ่ ่ if..else จะมีก ารทำา คำา สั่ง สำา หรับ ค่า เท็จ ถ้า นิพ จน์ ตรรกศาสตร์เ ป็น เท็จ ส่ว นคำา สัง if จะไม่ม ก ารทำา ่ ี คำา สั่ง ใดถ้า นิพ จน์ต รรกศาสตร์เ ป็น เท็จ  คำา สั่ง if หรือ if..else สามารถทีจ ะซ้อ นอยูข ้า งใน ่ ่ คำา สั่ง if หรือ if..else อื่น ได้
  • 41. สรุป เนื้อ หาของบท  คำา สั่ง switch จะมีล ัก ษณะโครงสร้า งการทำา งาน คล้า ยคลึง กับ คำา สั่ง if..else if..else.. แต่ช นิด ข้อ มูล ของตัว แปรทีจ ะนำา มาใช้ก ับ คำา สั่ง switch จะ ่ ต้อ งเป็น ชนิด char, byte, short หรือ int เท่า นั้น  คำา สั่ง ทีเ ป็น คำา สั่ง ของโครงสร้า งแบบทำา ซำ้า คือ คำา ่ สั่ง while,do..while หรือ for  คำา สั่ง while แตกต่า งจากคำา สัง do..while ตรงที่ คำา ่ สั่ง while จะไม่ม ก ารทำา ชุด คำา สั่ง เลยถ้า นิพ จน์ ี ตรรกศาสตร์เ ป็น เท็จ ส่ว นคำา สัง do..while จะมีก าร ่ ทำา ชุด คำา สัง หนึง ครั้ง ถ้า นิพ จน์ต รรกศาสตร์เ ป็น เท็จ ่ ่  คำา สั่ง for มีล ัก ษณะการทำา งานทีเ หมือ นกับ คำา สั่ง ่ while แต่จ ะมีก ารรวมคำา สัง กำา หนดค่า เริ่ม ต้น ่ นิพ จน์ต รรกศาสตร์แ ละคำา สั่ง เปลี่ย นแปลงค่า ไว้
  • 42. สรุป เนื้อ หาของบท  คำา สั่ง for จะใช้ใ นกรณีท ท ราบจำา นวนครั้ง ในการ ี่ ทำา ซำ้า ที่แ น่น อน ส่ว นคำา สัง while หรือ do..while ่ นิย มใช้ใ นกรณีท ไ ม่ท ราบจำา นวนครั้ง ในการทำา ซำ้า ี่ ล่ว งหน้า  คำา สั่ง โครงสร้า งทัง หมดทีก ล่า วมาแล้ว ข้า งต้น ้ ่ สามารถนำา มาใช้ร ่ว มกัน เป็น ลัก ษณะแบบซ้อ นได้ เช่น คำา สัง switch อยูภ ายในคำา สั่ง while หรือ คำา สั่ง ่ ่ for อยูภ ายในคำา สั่ง for ซึ่ง เรีย กโครงสร้า งใน ่ ลัก ษณะนี้ว ่า โครงสร้า งแบบซ้อ น  คำา สั่ง break จะทำา ให้ห ยุด สิน สุด การทำา งานของ ้ โครงสร้า งแบบทำา ซำ้า ส่ว นคำา สั่ง continue จะข้า ม การทำา งานคำา สั่ง ทีเ หลือ ภายในบล็อ ก { } โดยไป ่ เริ่ม การทำา ซำ้า ในรอบต่อ ไปใหม่
  • 43. แบบฝึก หัด  แบบฝึก หัด ที่ 1 การใช้ค ำา สั่ง if..else • ใช้ค ำา สั่ง if..else เพื่อ ตรวจสอบค่า ของ b2-4ac (โดย รับ ค่า a,b และ c เข้า มาทาง command line) ถ้า มีค ่า เป็น ลบให้พ ิม พ์ข ้อ ความว่า No answer แต่ถ ้า มีค ่า เป็น บวกหรือ ศูน ย์ใ ห้ค ำา นวณหาคำา ตอบและพิม พ์ค ำา ตอบทั้ง สองค่า ออกมาทางจอภาพ  แบบฝึก หัด ที่ 2 การใช้ค ำา สั่ง if แบบซ้อ น • จงเขีย นโปรแกรมคำา นวณหาและพิม พ์ค ่า เกรดที่เ หมาะ สมตามคะแนนที่ร ับ เข้า มาจากผู้ใ ช้ โดยมีเ กณฑ์ต ามนี้ • คะแนนตั้ง แต่ 90 ถึง 100 ได้เ กรด A • คะแนนตั้ง แต่ 80 ถึง 89 ได้เ กรด B • คะแนนตั้ง แต่ 70 ถึง 79 ได้เ กรด C • คะแนนตั้ง แต่ 60 ถึง 69 ได้เ กรด D • คะแนนที่ต ำ่า กว่า 60 ได้เ กรด F
  • 44. แบบฝึก หัด  แบบฝึก หัด ที่ 3 การใช้ค ำา สั่ง for แบบซ้อ น • จงเขีย นโปรแกรมภาษาจาวาโดยใช้ค ำา สั่ง for แบบ ซ้อ นเพื่อ พิม พ์ร ูป ดัง ต่อ ไปนี้ 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5