งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

การประมวลผลแบบวน ( LOOP )

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "การประมวลผลแบบวน ( LOOP )"— ใบสำเนางานนำเสนอ:

1 การประมวลผลแบบวน ( LOOP )

2 การประมวลผลแบบวน ( LOOP )
วัตถุประสงค์การเรียนในบทนี้ หลังจากจบบทเรียนนี้แล้วนักศึกษาสามารถมีความเข้าใจเกี่ยวกับ การทำงานแบบวน (loop) for loop while loop do … while loop การยุติการทำงานของ loop ด้วยการใช้คำสั่ง break และ continue

3 The while Statement The while statement has the following syntax:
while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repetitively until the condition becomes false.

4 Logic of a while loop condition evaluated false statement true

5 คำสั่ง while (The while Looping (Repetition) Structure)
while (expression) { statement1; statement2; …………… ; } expression/loop condition :- เงื่อนไขในการทำงานซ้ำ/วนลูป statement/body of the loop :- คำสั่งที่จะทำซ้ำ

6 คำสั่ง while (The while Looping (Repetition) Structure)
expression/loop condition :- เงื่อนไขในการทำงานซ้ำ/วนลูป เป็นนิพจน์ตรรกศาสตร์ ให้ค่าเป็น จริง (true) หรือ เท็จ (false) เท่านั้น สำหรับคำสั่ง while จะทำงานซ้ำต่อเมื่อเงื่อนไข (expression) เป็นจริง จะหยุดทำงานซ้ำ/หลุดลูป เมื่อเงื่อนไขเป็นเท็จ ถ้าเงื่อนไขไม่มีค่าเป็นเท็จ โปรแกรมจะทำงานซ้ำไปเรื่อยๆ เรียกว่า infinite loop

7 คำสั่ง while (The while Looping (Repetition) Structure)
int i = 0; while (i <= 20) { System.out.print(i + “ ”); i = i + 5; } Output : …………………………….. พื้นที่การทำงานของลูป while i <= 20 เรียกว่าเงื่อนไขในการทำงานซ้ำ/วนลูป ตัวแปร i เป็นตัวแปรควบคุมการทำงานของลูป

8 คำสั่ง while (The while Looping (Repetition) Structure)
public class WhileTest { public static void main(String[ ] args) int i = 0; while (i <= 20) System.out.println(i +" "); i = i + 5; }

9 คำสั่ง while (The while Looping (Repetition) Structure)
int i = 20; while (i < 20) { System.out.print(i + “ ”); i = i + 5; } System.out.print(i); Output : …………………………….. int i = 20; while (i <= 20) { System.out.print(i + “ ”); i = i + 5; } System.out.print(i); Output : ……………………………..

10 ตัวอย่าง public class WhileTest2 {
public static void main(String[ ] args) int i = 20; while (i <= 20) { System.out.println(i +" "); i = i + 5; } System.out.print(i); }

11 คำสั่ง while (The while Looping (Repetition) Structure)
แบบที่ 1: การเพิ่มค่าตัวแปรควบคุมลูป (Counter-Controlled while Loops) รู้จำนวนครั้งที่ต้องการทำงานซ้ำ counter = 0; while (counter < N) { ……………………… ; ……………………… ; counter++; } N คือจำนวนรอบที่ต้องการทำงานซ้ำ/ต้องการทำงานกี่รอบ ให้นักศึกษานำส่วนของโปรแกรมข้างต้นมาปรับปรุงให้สามารถแสดงข้อความว่า Hello จำนวน 10 บรรทัด สร้างโปรแกรมชื่อ WhileTest3.java

12 คำสั่ง while (The while Looping (Repetition) Structure)
แบบที่ 1: การเพิ่มค่าตัวแปรควบคุมลูป (Counter-Controlled while Loops) หาค่าผลรวมของเลขจำนวนเต็ม ตั้งแต่ 1-12 counter = 1; sum = 0; while (counter <= 12) { sum = sum + counter; counter++; } ให้นักศึกษานำส่วนของโปรแกรมข้างต้นมาปรับปรุงให้สามารถหาค่าผลรวมของเลขจำนวนเต็ม ตั้งแต่ 1-12 แล้วแสดงผลลัพธ์ทางจอภาพ ….12 = โปรแกรมชื่อ WhileTest4.java

13 คำสั่ง while (The while Looping (Repetition) Structure)
แบบที่ 2: ระบุค่าที่ต้องการให้ออกจากลูป (Sentinel-Controlled while Loops) input the first data item into variable while (variable != sentinel) { ……………………….. ; ……………………….. ; input a data item into variable } int sentinal = -999 while (number != sentinal) { ……………………….. ; ……………………….. ; รับค่าจากผู้ใช้เพื่อทำให้เงื่อนไข หลัง while เป็นเท็จ; }

14 ตัวอย่าง แบบที่ 2: ระบุค่าที่ต้องการให้ออกจากลูป
import javax.swing.JOptionPane; public class WhileTest3 { public static void main(String[ ] args) { int number=0; int sentinel = -999; while (number != sentinel) { String data = JOptionPane.showInputDialog(null,"Enter Number"); number = Integer.parseInt(data); } System.out.println("Hello outloop "); System.exit(0);

15 คำสั่ง while (The while Looping (Repetition) Structure)
แบบที่ 3: กำหนดตัวแปรสำหรับหยุดการทำงานของลูป (Flag-Controlled while Loops) ตัวแปรสำหรับหยุดการทำงานจะเป็นตัวแปรที่เก็บค่าเป็น boolean boolean found = false; while (!found) { ………………… ; if (expression) found = true; }

16 คำสั่ง while (The while Looping (Repetition) Structure)
แบบที่ 3: กำหนดตัวแปรสำหรับหยุดการทำงานของลูป (Flag-Controlled while Loops) ตัวอย่างเช่น boolean found = false; int i = 0; while (!found) { i += 5; if (i < 20) found = true; } Output: ………………………………. ให้นักศึกษานำส่วนของโปรแกรมข้างต้นมาปรับปรุงให้สามารถกำหนดการทำงานออกนอก loopได้ โปรแกรมชื่อ WhileTest5.java

17 Example ตัวอย่างการทดลองบวกเลข 1 ถึง 100
public class Series { public static void main (String[] args) { int x = 1; int total = 0; while (x <= 100) { total += x; x = x + 1; } System.out.println ("The series from 1 to 100 is " + total);

18 Sum.java class Sum { public static void main(String[] args) {
int i, sum; i = 0; sum = 0; while(i <= 10) { sum += i; i++; } System.out.println("Sum = " + sum); System.out.println("Average = " + (double)sum / (i-1));

19 ตัวอย่างการใช้ while import javax.swing.JOptionPane;
public class PrintLine { public static void main(String[] args) int n= 10; int i = 1; while(i<=n) System.out.print("*"); i = i + 1; } //end while } //end main method } //end class a **********

20 import javax.swing.JOptionPane;
class Testwhile3 { public static void main(String[] args) float score; double d; int sum,start,stop; char ch; boolean b; String data,data2, message; data = JOptionPane.showInputDialog("ค่าเริ่มต้นที่จะหาผลรวม : "); start = Integer.parseInt(data); data = JOptionPane.showInputDialog("ค่าสูงสุดที่จะหาผลรวม : "); stop = Integer.parseInt(data); sum = 0; message = "ผลรวมของเลข : "+start; message = message + "\n ถึงเลข : "+stop; JOptionPane.showMessageDialog(null,message); while (start <= stop) sum+=start; start+=1; } message = "ผลรวมที่ได้คือ : "+sum; System.exit(0);

21 { public static void main(String[] args)
import javax.swing.JOptionPane; class Testwhile4 { public static void main(String[] args) { int i,sum; String message; //data = JOptionPane.showInputDialog(" "); //stop = Integer.parseInt(data); sum = 0; i=1; while (i <= 10) { sum+=i; //message = "i = "+ i + "SUM = "+sum+" รอบที่ : " + i + " ผลรวม = " + sum; //JOptionPane.showMessageDialog(null,message); System.out.println("i = "+i+" Sum = "+sum); i+=1; } //System.exit(0);

22 import javax.swing.JOptionPane;
class Testloop1{ public static void main(String args[]) { int n = 1,score;String data; while (true) { data = JOptionPane.showInputDialog("Enter Score =(999 to stop)"); score = Integer.parseInt(data); if (score== 999) break; } System.exit(0);

23 ห้องพฤหัส ถึงที่นี่ การใช้ while ซ้อนกัน
import javax.swing.JOptionPane; public class PrintLine { public static void main(String[] args) int col = 10, row=3,countCol,countRow=1; while(countRow <= row) countCol= 1; while (countCol<=col) System.out.print("*"); countCol = countCol + 1; } //end while (countCol<=n) countRow = countRow + 1; } //end while (countRow <= row) } //end main method } //end class **********

24 The do Statement The do statement has the following syntax: do
{ statement; } while ( condition ) Uses both the do and while reserved words The statement is executed once initially, then the condition is evaluated The statement is repetitively executed until the condition becomes false

25 Logic of a do loop statement true condition evaluated false

26 คำสั่ง do … while (The do … while Looping (Repetition) Structure)
{ statement1; statement2; …………… ; } while (expression) ; expression/loop condition :- เงื่อนไขในการทำงานซ้ำ/วนลูป statement/body of the loop :- คำสั่งที่จะทำซ้ำ

27 Comparing the while and do loops
statement true condition evaluated false while loop true condition evaluated statement false do loop

28 คำสั่ง do … while (The do … while Looping (Repetition) Structure)
int i = 0; do { System.out.print(i + “ ”); i = i + 5; } while (i <= 20) ; Output : …………………………….. พื้นที่การทำงานของลูป do…while

29 คำสั่ง do … while (The do … while Looping (Repetition) Structure)
int i = 11; while (i <= 10) { System.out.print(i + “ ”); i = i + 5; } System.out.print(i); Output : …………………………….. int i = 11; do { System.out.print(i + “ ”); i = i + 5; } while (i <= 10) ; System.out.print(i); Output : …………………………….. จากส่วนของ code ด้านขวา ให้นักศึกษาปรับปรุงให้สามารถแสดงผลลัพธ์ได้ โดยให้ชื่อโปรแกรมว่า DoWhile1.java

30 public class Testdo { public static void main (String args[])
{int data =1; int sum = 0; do { sum += data ; data = data+1; System.out.println ("data = " +data+" Sum : " + sum) ; } while (data <=10) ; System.out.println("========================="); System.out.println ("data = " +(data-1)+" Sum : " + sum) ; } } // Testdo

31 public class DoWhiletest { public static void main(String args[])
{ int a = 1,sum = 0; // Initial Counter do { sum+=a; System.out.println("a = "+a+ " sum = "+sum); a++; // Increase Counter } while ( a <= 10 );

32 The for Statement The for statement has the following syntax:
The initialization portion is executed once before the loop begins The statement is executed until the condition becomes false Reserved word for ( initialization ; condition ; increment ) statement; The increment portion is executed at the end of each iteration

33 The for Statement A for loop is equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; }

34 Logic of a for loop initialization condition evaluated false statement
true increment

35 คำสั่ง for (The for Looping (Repetition) Structure)
for (initial statement; loop condition; update statement) { statement1; statement2; }

36 คำสั่ง for (The for Looping (Repetition) Structure)
คำสั่งกำหนดค่าเริ่มต้นการทำงานของลูป (initial statement) ตรวจสอบเงื่อนไข (loop condition) ถ้าเงื่อนไขเป็นจริง จะทำงานคำสั่งถัดจากคำสั่ง for เพิ่มค่าตัวแปรที่ควบคุมลูป (update statement) ทำซ้ำข้อที่ 2-3 จนกว่าเงื่อนไขจะกลายเป็นเท็จ จึงจะหยุดการทำงานของลูป for จะเห็นว่าคำสั่งกำหนดค่าเริ่มต้นการทำงานของลูป จะทำงานเพียงแค่ครั้งเดียว และทำงานเป็นคำสั่งแรกเสมอ

37 class For1 { public static void main(String[] args) int i,sum; sum = 0; for (i=1;i<=10;i++) sum+=i; System.out.println("i ="+i+ " sum "+sum); }

38 Nested Control Structures
public class NestedLoop1 { public static void main (String args[]) { int i, j; for (i = 1; i <= 5; i++) { for (j = 1; j <= i; j++) System.out.print("*"); System.out.println(); } * ** *** **** *****

39 Nested Control Structures
public class NestedLoop1 { public static void main (String args[]) { int i, j; for (i = 5; i >= 1; i- -) { for (j = 1; j <= i; j++) System.out.print("*"); System.out.println(); } ***** **** *** ** *

40 การใช้ break และ continue
Break statement ใช้บังคับออกจากลูป หรือออกจากคำสั่ง while, for,do…while หรือ switch for (int i=1 ;i <=10 ; i++) { if (i==5) break; System.out.println(i + “ “); } //end for จาก Code ให้นำไปปรับปรุงให้สามารถทำงานแสดงผลลัพธ์ได้ ดังภาพ(TestBreak.java)

41 การใช้ break และ continue
ใช้กับคำสั่ง while, for , do…while เพื่อข้ามคำสั่งที่เหลือต่อจาก continue เพื่อกระโดดไปต้นลูป for (int i=1 ;i <=10 ; i++) { if (i==5) continue; System.out.println(i + “ “); } //end for จาก Code ให้นำไปปรับปรุงให้สามารถทำงานแสดงผลลัพธ์ได้ ดังภาพ(TestContinue.java)

42 แบบฝึกหัดส่งครั้งที่ 2ต่อไป
ทำลงในกระดาษเขียนผลลัพธ์ และ Code ส่ง จำนวน 16 ข้อ

43 แบบฝึกหัด : คำสั่ง for จงพิจารณาโปรแกรมต่อไปนี้
for (i = 12; i <= 25; i++) System.out.print(i + “ ”); ผลลัพธ์ ถ้าเปลี่ยนคำสั่ง i++ เป็น i - - ผลลัพธ์จะเป็นอย่างไร

44 แบบฝึกหัด : คำสั่ง for จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ num = 0;
for (i = 1; i <= 4; i++) { num = num + 10 * (i - 1); System.out.print(num + “ ”); } Output: ……………………………… จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ j = 2; for (i = 0; i <= 5; i++) { System.out.print(j + “ ”); j = 2 * j + 3; } Output: ………………………………

45 แบบฝึกหัด : คำสั่ง for จงพิจารณาโปรแกรมต่อไปนี้ s = 0;
for (i = 0; i < 5; i++) { s = 2 * s + i; System.out.print(s + “ ”); } Output: ……………………………… จากโปรแกรมในข้อที่ 4 ถ้าเติมเครื่องหมาย ; หลังวงเล็บของ for จะได้ผลลัพธ์อย่างไร จากโปรแกรมในข้อที่ 4 ถ้าเปลี่ยนคำสั่งกำหนดค่าเริ่มต้นจาก i = 0 เป็น i = 5 จะได้ผลลัพธ์อย่างไร

46 แบบฝึกหัด : คำสั่ง for จงหาผลลัพธ์ของโปรแกรมต่อไปนี้
for (i = 1; i <= 1; i++) System.out.print(“*”); for (i = 2; i >= 1; i++) for (i = 1; i <= 1; i - -) System.out.print(“*”); for (i = 12; i >= 9; i - -)

47 แบบฝึกหัด : คำสั่ง for จงหาผลลัพธ์ของโปรแกรมต่อไปนี้
for (i = 0; i <= 5; i++) System.out.print(“*”); for (i = 1; i <= 5; i++) { System.out.print(“*”); i = i + 1; } a = 5; b = 3; c = 8; for (j = 1; j < a; j++) { d = b + c; b = c; c = d; System.out.print(c + “ ”); }

48 แบบฝึกหัด : คำสั่ง for จงหาผลลัพธ์ของโปรแกรมต่อไปนี้
for (j = 0; j < 8; j++) { System.out.print(j * 15 + “-”); if (j != 7) System.out.println((j + 1) * ); else System.out.println((j + 1) * 25); }

49 ให้เขียนโปรแกรมชื่อ Multiplicationtable.java
เขียน Code ข้อ 15. ให้เขียนโปรแกรมชื่อ Multiplicationtable.java ให้สามารถทำการแสดงผลลัพธ์สูตรคูณแม่ 12 ออกทางจอภาพ

50 ให้เขียนโปรแกรมชื่อ FindMinvalue.java ให้สามารถทำงานดังนี้
เขียน Code ข้อ 16. ให้เขียนโปรแกรมชื่อ FindMinvalue.java ให้สามารถทำงานดังนี้ รับค่าตัวเลขจำนวนเต็ม 10 จำนวน เก็บค่าสูงสุดของค่าตัวเลขที่รับเข้าไป แสดงค่าตัวเลขที่มีค่าน้อยที่สุดออกมาทางจอภาพ

51 The End

52

53 ให้เขียนโปรแกรม FindGrade.java
ให้ทำงานดังนี้ รับค่าคะแนน (score) ผู้สอบจำนวน 10 คนโดยในแต่ละครั้งที่รับคะแนนให้แสดงค่าคะแนนและเกรดที่ได้รับจากเกณฑ์คะแนนดังนี้ 0-50 =f,51-60=c,61-70=b,80-100=A จากนั้นเมื่อรับค่าคะแนนจบครบแล้ว ให้คำนวณหาคะแนนเฉลี่ยของทั้ง 10 คนและแสดงเฉพาะค่าคะแนนเฉลี่ยออกทางจอภาพ หมายเหตุ.หากขณะป้อนค่าคะแนน หากป้อนค่าคะแนนเป็น -999 ให้ยุติการทำงานโดยไม่แสดงค่าใดๆ


ดาวน์โหลด ppt การประมวลผลแบบวน ( LOOP )

งานนำเสนอที่คล้ายกัน


Ads by Google