KEMBAR78
Computer Programming Chapter 4 : Loops | PDF
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

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