SlideShare a Scribd company logo
1 of 13
Computer Programming
                                                   Chapter 4 : Loops




                  Atit Patumvan
  Faculty of Management and Information Sciences
                Naresuna University
2



                                                Statements and Execution Flow


            •         ประโยค (statement) คือหน่วยของภาษาที่ใช้กําหนด การทํางาน
                      ของโปรแกรม

            •         ประกอบขึ้นจากลําดับของประโยค

            •         ผลลัพธ์ของโปรแกรมขึ้นอยู่กับความหมายและลําดับของประโยค

            •         ประโยคเป็นสิ่งที่ไม่อาจกูกเปลี่ยนแปลง ไม่สามารถเก็บค่า ไม่
                      สามารถนํามาเปรียบเทียบ

            •         ประโยคอาจประกอบขึ้นจากคําสั่งหรือการประมวลผลหลายคําสั่ง

            •         ผู้เขียนโปรแกรมต้องทราบลําดับก่อนหลังในการทํางานของ
                      ประโยค

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
3



                                                                            While Statement


            •         ประโยค while ใช้ในการควบคุมประโยคให้ถูกซ้ําจนกว่าเงื่อนไข
                      บางอย่างเป็นเท็จจึงจะหยุดดําเนินการ


              while ( <boolean_expression>)
                    <statements>;
                                                                                                [ ! boolean_expression ]


                                                                                              [ boolean_expression ]


                                                                                        statements




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
4



                                                       Factorial Number Calculator


    01: import java.util.Scanner;
    02:
    03: public class WhileDemo {
    04:
    05:     public static void main(String[] args) {
    06:         Scanner in = new Scanner(System.in);
    07:         int n = in.nextInt();
    08:         int i = 1, f = 1;
    09:         while (i++ < n) {
    10:             f *= i;
    11:         }
    12:         System.out.println(n + "! = " + f);
    13:     }
    14: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
5



                                                                                     Do Statement


            •         ประโยค do ใช้ในการควบคุมประโยคให้ถูกซ้ําจนกว่าเงื่อนไขบาง
                      อย่างเป็นเท็จจึงจะหยุดดําเนินการ

            •         คล้ายคลึงกับ ประโยค while แตกต่างที่การตรวจสอบเงือนไขการ
                      หยุดจะประมวลผลหลังการดําเนินการ



            do {
               <statements>;
            }while ( <boolean_expression>);                                                             statements



                                                                                       [ boolean_expression ]   [ ! boolean_expression ]



Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
6



                                                       Factorial Number Calculator


    01: import java.util.Scanner;
    02:
    03: public class DoDemo {
    04:
    05:     public static void main(String[] args) {
    06:         Scanner in = new Scanner(System.in);
    07:         int n = in.nextInt();
    08:         int i = 1, f = 1;
    09:         do {
    10:              f *= i;
    11:         } while (i++ < n);
    12:         System.out.println(n + "! = " + f);
    13:     }
    14: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
7



                                                                                 For Statement


            •         ประโยค for ใช้ในการควบคุมประโยคให้ถูกซ้ําที่มีตัวแปรนับรอบ
                      เรียกว่า index

            •         เหมาะกับการทําซ้ําที่รู้ว่าจะต้องทํากี่ครั้ง
            for(<initial_exp>; <condition_exp>; <update_exp>)
               <statements>;


                                                                                     statements
                                                                                            [ ! condition_exp ]


                                                                                          [ condition_exp ]

                      statements                                                     statements



Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
8



                                                       Factorial Number Calculator


    01: import java.util.Scanner;
    02:
    03: public class ForDemo {
    04:
    05:     public static void main(String[] args) {
    06:         Scanner in = new Scanner(System.in);
    07:         int n = in.nextInt();
    08:         int i, f = 1;
    09:         for (i = 1; i <= n; i++) {
    10:             f *= i;
    11:         }
    12:         System.out.println(n + "! = " + f);
    13:     }
    14: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
9



                                             Variable Accessibility in For Loop


    01: public class ForScope {
    02:
    03:     public static void main(String[] args) {
    04:         int i = 1;
    05:         for (int j = 0; j < 10; j++) {
    06:             // both i and j are accessible here
    07:         }
    08:         // only i is accessible here
    09:     }
    10: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
10



                                                      Out of Condition in For Loop



    01: public class ForOutCondition {
    02:
    03:     public static void main(String[] args) {
    04:         for (double j = 0.0; j != 1.0; j += 0.1) {
    05:             System.out.println(j);
    06:         }
    07:         System.out.println("Stop");
    08:     }
    09: }


    01: public class ForOutCondition1 {
    02:
    03:     public static void main(String[] args) {
    04:         for (int j = 1; j == 10; j += 2) {
    05:             System.out.println(j);
    06:         }
    07:         System.out.println("Stop");
    08:     }
    09: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
11



                                                                                     Infinity Loop



    01: public class ForInfinityLoop {
    02:
    03:     public static void main(String[] args) {
    04:         for (;;) {
    05:             System.out.println("Hello");
    06:         }
    07:         System.out.println("Stop");
    08:     }
    09: }


    01: public class WhileInfinityLoop {
    02:
    03:     public static void main(String[] args) {
    04:         while (true) {
    05:             System.out.println("Hello");
    06:         }
    07:         System.out.println("Stop");
    08:     }
    09: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
12



                                                                            Break Statement


            •         ใช้ในการสิ้นสุดการทํางานในคําสั่ง switch while do และ for

  01: public class BreakDemo {
  02:
  03:     public static void main(String[] args) {
  04:         for (int i = 1; i < 200; i++) {
  05:             System.out.println(i);
  06:             if (i == 3) {
  07:                 break;
  08:             }
  09:         }
  10:     }
  11: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
13



                                                                      Continue Statement


            •         ใช้ในการสิ้นสุดการทํางานของในคําสั่ง while do และ for และให้
                      กลับไปเริ่มวงรอบอีกครั้ง
  01: public class ContinueDemo {
  02:
  03:     public static void main(String[] args) {
  04:         for (int i = 1; i < 20; i++) {
  05:             if ((i%2)==0) {
  06:                 continue;
  07:             }
  08:             System.out.println(i);
  09:         }
  10:     }
  11: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University

More Related Content

More from Atit Patumvan

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agricultureAtit Patumvan
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556Atit Patumvan
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationAtit Patumvan
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6Atit Patumvan
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 1
การบริหารเชิงคุณภาพ ชุดที่ 1การบริหารเชิงคุณภาพ ชุดที่ 1
การบริหารเชิงคุณภาพ ชุดที่ 1Atit Patumvan
 
Write native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDTWrite native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDTAtit Patumvan
 
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic BindingChapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic BindingAtit Patumvan
 
OOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceOOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceAtit Patumvan
 

More from Atit Patumvan (20)

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agriculture
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computation
 
Media literacy
Media literacyMedia literacy
Media literacy
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2
 
การบริหารเชิงคุณภาพ ชุดที่ 1
การบริหารเชิงคุณภาพ ชุดที่ 1การบริหารเชิงคุณภาพ ชุดที่ 1
การบริหารเชิงคุณภาพ ชุดที่ 1
 
Write native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDTWrite native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDT
 
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic BindingChapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
 
OOP Chapter 8 : Inheritance
OOP Chapter 8 : InheritanceOOP Chapter 8 : Inheritance
OOP Chapter 8 : Inheritance
 

Computer Programming Chapter 4 : Loops

  • 1. Computer Programming Chapter 4 : Loops Atit Patumvan Faculty of Management and Information Sciences Naresuna University
  • 2. 2 Statements and Execution Flow • ประโยค (statement) คือหน่วยของภาษาที่ใช้กําหนด การทํางาน ของโปรแกรม • ประกอบขึ้นจากลําดับของประโยค • ผลลัพธ์ของโปรแกรมขึ้นอยู่กับความหมายและลําดับของประโยค • ประโยคเป็นสิ่งที่ไม่อาจกูกเปลี่ยนแปลง ไม่สามารถเก็บค่า ไม่ สามารถนํามาเปรียบเทียบ • ประโยคอาจประกอบขึ้นจากคําสั่งหรือการประมวลผลหลายคําสั่ง • ผู้เขียนโปรแกรมต้องทราบลําดับก่อนหลังในการทํางานของ ประโยค Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 3. 3 While Statement • ประโยค while ใช้ในการควบคุมประโยคให้ถูกซ้ําจนกว่าเงื่อนไข บางอย่างเป็นเท็จจึงจะหยุดดําเนินการ while ( <boolean_expression>) <statements>; [ ! boolean_expression ] [ boolean_expression ] statements Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 4. 4 Factorial Number Calculator 01: import java.util.Scanner; 02: 03: public class WhileDemo { 04: 05: public static void main(String[] args) { 06: Scanner in = new Scanner(System.in); 07: int n = in.nextInt(); 08: int i = 1, f = 1; 09: while (i++ < n) { 10: f *= i; 11: } 12: System.out.println(n + "! = " + f); 13: } 14: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 5. 5 Do Statement • ประโยค do ใช้ในการควบคุมประโยคให้ถูกซ้ําจนกว่าเงื่อนไขบาง อย่างเป็นเท็จจึงจะหยุดดําเนินการ • คล้ายคลึงกับ ประโยค while แตกต่างที่การตรวจสอบเงือนไขการ หยุดจะประมวลผลหลังการดําเนินการ do { <statements>; }while ( <boolean_expression>); statements [ boolean_expression ] [ ! boolean_expression ] Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 6. 6 Factorial Number Calculator 01: import java.util.Scanner; 02: 03: public class DoDemo { 04: 05: public static void main(String[] args) { 06: Scanner in = new Scanner(System.in); 07: int n = in.nextInt(); 08: int i = 1, f = 1; 09: do { 10: f *= i; 11: } while (i++ < n); 12: System.out.println(n + "! = " + f); 13: } 14: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 7. 7 For Statement • ประโยค for ใช้ในการควบคุมประโยคให้ถูกซ้ําที่มีตัวแปรนับรอบ เรียกว่า index • เหมาะกับการทําซ้ําที่รู้ว่าจะต้องทํากี่ครั้ง for(<initial_exp>; <condition_exp>; <update_exp>) <statements>; statements [ ! condition_exp ] [ condition_exp ] statements statements Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 8. 8 Factorial Number Calculator 01: import java.util.Scanner; 02: 03: public class ForDemo { 04: 05: public static void main(String[] args) { 06: Scanner in = new Scanner(System.in); 07: int n = in.nextInt(); 08: int i, f = 1; 09: for (i = 1; i <= n; i++) { 10: f *= i; 11: } 12: System.out.println(n + "! = " + f); 13: } 14: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 9. 9 Variable Accessibility in For Loop 01: public class ForScope { 02: 03: public static void main(String[] args) { 04: int i = 1; 05: for (int j = 0; j < 10; j++) { 06: // both i and j are accessible here 07: } 08: // only i is accessible here 09: } 10: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 10. 10 Out of Condition in For Loop 01: public class ForOutCondition { 02: 03: public static void main(String[] args) { 04: for (double j = 0.0; j != 1.0; j += 0.1) { 05: System.out.println(j); 06: } 07: System.out.println("Stop"); 08: } 09: } 01: public class ForOutCondition1 { 02: 03: public static void main(String[] args) { 04: for (int j = 1; j == 10; j += 2) { 05: System.out.println(j); 06: } 07: System.out.println("Stop"); 08: } 09: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 11. 11 Infinity Loop 01: public class ForInfinityLoop { 02: 03: public static void main(String[] args) { 04: for (;;) { 05: System.out.println("Hello"); 06: } 07: System.out.println("Stop"); 08: } 09: } 01: public class WhileInfinityLoop { 02: 03: public static void main(String[] args) { 04: while (true) { 05: System.out.println("Hello"); 06: } 07: System.out.println("Stop"); 08: } 09: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 12. 12 Break Statement • ใช้ในการสิ้นสุดการทํางานในคําสั่ง switch while do และ for 01: public class BreakDemo { 02: 03: public static void main(String[] args) { 04: for (int i = 1; i < 200; i++) { 05: System.out.println(i); 06: if (i == 3) { 07: break; 08: } 09: } 10: } 11: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University
  • 13. 13 Continue Statement • ใช้ในการสิ้นสุดการทํางานของในคําสั่ง while do และ for และให้ กลับไปเริ่มวงรอบอีกครั้ง 01: public class ContinueDemo { 02: 03: public static void main(String[] args) { 04: for (int i = 1; i < 20; i++) { 05: if ((i%2)==0) { 06: continue; 07: } 08: System.out.println(i); 09: } 10: } 11: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University