while dowhile for nestedfor 2 Looping Types 1

  • Slides: 28
Download presentation

หวขอ. . . Øคำสง while do-while for nested-for 2

หวขอ. . . Øคำสง while do-while for nested-for 2

คำสงวนซำ �ชนดของการวนซำ (Looping Types) การวนซำตามเงอนไข 1. controlled Loop) ◦ while ◦ do-while 2. (Conditiondo

คำสงวนซำ �ชนดของการวนซำ (Looping Types) การวนซำตามเงอนไข 1. controlled Loop) ◦ while ◦ do-while 2. (Conditiondo while(condition) yes no exit statement(s) while(condition) yes การวนซำตามจำนวนนบ Loop) l for i=1 to N i N statement(s) no exit (Counting-controlled i>N exit 3

6. 1 คำสง while(condition) �รปแบบ yes no exit statement(s) while (expression) statement; or while

6. 1 คำสง while(condition) �รปแบบ yes no exit statement(s) while (expression) statement; or while (expression) {statement; . . . } �ถาเงอนไขใน expression เปน TRUE จะทำ statement l แตถา FALSE จะทำใหจบการทำซำแบบ while l ตวอยางเชน 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 การพมพคา i=1 while i<=12 yes Print i no end i = i+1 4

ตวอยาง เขยนโปรแกรมอาน 6. 2 ขอมล ของนกศกษา คน คอ ID, Name, X (คะแนน) ดวย while

ตวอยาง เขยนโปรแกรมอาน 6. 2 ขอมล ของนกศกษา คน คอ ID, Name, X (คะแนน) ดวย while � ทละ และนบจำนวนนกศกษา ผลลพ while ดวยคา ID = -9 ◦ สมมต เงอนไข สำหรบออกจาก #include <stdio. h> 1. Enter ID, Name, X: 50011, _ AA, 80 (Dummy) ธ 2. Enter ID, Name, X: 50012, void main() _ BB, 67 { } 3. Enter ID, Name, X: 50013, _ CC, 55 4. Enter ID, Name, X: 50014, _ DD, 90 5. Enter ID, Name, X: 50015, _ EE, 75 6. Enter ID, Name, X: -9, _ 0, 0 #students = 5 int ID=0, X, n=0; char Name[20]; while (ID!=-9) { printf(“%d. Enter ID, Name, X : ”, n+1); scanf(“%d, %s, %d”, &ID, &Name, &X); n++; // count #students } printf(“#students =%dn”, - -n); 6

ตวอยาง �เขยนโปรแกรมคำนวณ 6. 4 ผลบวกของเลขนบ 100 หรอ Triangle number 1 100 (1+2+3+…+100 =เชน ?

ตวอยาง �เขยนโปรแกรมคำนวณ 6. 4 ผลบวกของเลขนบ 100 หรอ Triangle number 1 100 (1+2+3+…+100 =เชน ? ) 1+2+3+. . +10 1 #include <stdio. h> void main() { int i = 1, SUM = 0; while (i <= 100) { printf("%d+", i); SUM = SUM + i; i++; } printf(“=%dn”, SUM); } 1 - 1 3 6 10 15 21 28 36 45 55 start i=1 SUM= SUM =00 1 2 while i<=100 yes SUM=SUM+i i = i+1 3 no SUM end ผลลพ 1+ ธ 2+. . . 9+ 100 = 5050 8

6. 2 คำสง �รปแบบ do-while do do { statement; … } while (expression); �ทำ

6. 2 คำสง �รปแบบ do-while do do { statement; … } while (expression); �ทำ statement กอนทดสอบเงอนไข statement(s) while(condition) แต เมอเปรยบเทยบกบคำสง lทดสอบเงอนไข expression กอน lจงทำ statement yes no exit while(condition) yes no exit statement(s) 9

ตวอยาง 6. 5 เขยนโปรแกรมคำนวณ ผลบวกของเลขนบ 1 -100 (1+2+3+…+100 = ? ) โดยใช do-while #include

ตวอยาง 6. 5 เขยนโปรแกรมคำนวณ ผลบวกของเลขนบ 1 -100 (1+2+3+…+100 = ? ) โดยใช do-while #include <stdio. h> void main() { int i=1, SUM=0; do { printf("%d + ", i); SUM = SUM + i; i++; } while (i <= 100); printf(" = %dn", SUM); } start i=1 SUM= 0 1 SUM=SUM+i i = i+1 3 yes while i<=100 2 no SUM end ผลลพ 1+2+3+4+. . . +100+ = 5050 ธ 10

ตวอยาง 6. 9 เขยนโปรแกรมคำนวณ ผลคณของเลขนบ #include <stdio. h> ? ) โดย for void main()

ตวอยาง 6. 9 เขยนโปรแกรมคำนวณ ผลคณของเลขนบ #include <stdio. h> ? ) โดย for void main() { int i; float MUL = 1; for (i=1; i<=10; i++) { printf("%d x ", i); MUL = MUL*i; } printf(" = %. 0 fn", MUL); } 1 -10 (1 x 2 x 3 x…x 10 = start MUL = 1 for i=1 to 10 (i++) yes MUL = MULxi no MUL end ผลลพ 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 ธ=3628800 15

ตวอยาง 6. 10 เขยนโปรแกรมคำนวณคา N! เมอ เปนคาทรบจาก Keyboard เมอ Nx(N-1)x(N-2)x…x 3 x 2 x

ตวอยาง 6. 10 เขยนโปรแกรมคำนวณคา N! เมอ เปนคาทรบจาก Keyboard เมอ Nx(N-1)x(N-2)x…x 3 x 2 x 1 เชน 5! =<stdio. h> 5 x 4 x 3 x 2 x 1 #include void main() { int i, N; float Fac=1; printf("Enter N: "); scanf("%d", &N); for (i=N; i>=1; i--) Fac = Fac * i; printf("%d! = %. 0 fn", N, Fac); } N N! = start Input N for i=Nto 1 no (i--) yes Fac = Fac*i Print N, Fac end ผลลพ 5 Enter N : _ ธ 5! = 120 16

ตวอยาง 6. 11 เขยนโปรแกรมคำนวณตาราง สตรคณแม เมอ T เปนคาทรบจาก ผลลพ Enter Table = 2_ ธMultiplication

ตวอยาง 6. 11 เขยนโปรแกรมคำนวณตาราง สตรคณแม เมอ T เปนคาทรบจาก ผลลพ Enter Table = 2_ ธMultiplication Table #2 Keyboard T #include <stdio. h> void main() { int i, T; float R; printf("Enter Table = "); scanf("%d", &T); printf("Multiplication Table #%dn", T); for (i=1; i<=12; i++) { R = T * i; printf(" %d x %d = %. 0 fn", T, i, R); } } 2 x 1=2 2 x 2=4 2 x 3=6. . . 2 x 12 = 24 start Input T for i=1 to 12 no (i++) yes R = T*i Print T, i, R 17 end

ตวอยาง เขยนโปรแกรมพมพคา � มต (i: row, j: col) 6. 12 ขนาดNx. N j (1,

ตวอยาง เขยนโปรแกรมพมพคา � มต (i: row, j: col) 6. 12 ขนาดNx. N j (1, 2, 3, …, N) แบบ 2 #include <stdio. h> void main() { int i, j, N; printf(“Enter N = ”); scanf(“%d”, &N); for (i=1; i<=N; i++) { for (j=1; j<=N; j++) printf(“%d”, j); printf(“n”); } } start Input N For i=1 to N For j=1 to N no end yes Print j Print n ผลลพ Enter ธ 12345 N = 5_ 12345 20

ตวอยาง 6. 13 เขยนโปรแกรมพมพผลบวก i+j ขนาด Nx. N � เมอ i, j = 1,

ตวอยาง 6. 13 เขยนโปรแกรมพมพผลบวก i+j ขนาด Nx. N � เมอ i, j = 1, 2, 3, … , N #include <stdio. h> void main() { int i, j, N; printf(“Enter N = ”); scanf(“%d”, &N); for (i=1; i<=N; i++) { for (j=0; j<N; j++) printf(“%d”, i+j); printf(“n”); } } start Input N For i=1 to. N no end For j=0 to. N-1 yes Print i+j Print n ผลลพ Enter ธ 12345 N = 5_ 234567 456789 21

ตวอยาง 6. 14 เขยนโปรแกรมพมพ ตาราง Latin Square � ขนาด Nx. N เชน start N=

ตวอยาง 6. 14 เขยนโปรแกรมพมพ ตาราง Latin Square � ขนาด Nx. N เชน start N= 12345 5 234512 451234 #include <stdio. h> void main() { int i, j, N, L; printf(“Enter N = ”); scanf(“%d”, &N); for (i=1; i<=N; i++) { for (j=0; j<N; j++) { L = i+j; if (L>N) L = L-N; printf("%d ", L); } printf(“n”); } } Input N for i=1 to N i N for j=0 to N-1 L= i >N end j > N-1 j N(i 1+ j) L>N yes L = L-N no Print L ผลลพ Enter N = 5_ ธ 12345 1 23456 12 345678 123 56789 1234 22

CS-2 start Input N #include <stdio. h> i³n For i=2 to N-1 void main()

CS-2 start Input N #include <stdio. h> i³n For i=2 to N-1 void main() yes i³N i<n no N%i=0 { yes no Not Pr Prime unsigned long N, i; printf("Enter +N = "); scanf("%ld", &N); end for (i=2; i<N; i++) // divisor i = 2, 3, 4, 5, …, N-1 if (N%i == 0) break; // i < N (not prime) if (i>=N) printf(“N=%ld is prime numbern", N); else printf(“N=%ld is not prime numbern", N); } 26

CS-2(Efficient) start Input N N=1 no N=2 no yes Not pr yes Prime #include

CS-2(Efficient) start Input N N=1 no N=2 no yes Not pr yes Prime #include <stdio. h> yes Not pr N%2=0 void main() no end i>n/2 { For i=3 to N/2 yes i n/2 unsigned long N, i; i>N/2 N%i=0 no yes printf("Enter +N = "); scanf("%ld", &N); no Prime Not Pr if (N==1) printf(“N=%ld is not prime no. n", N); else if (N==2) printf(“N=%ld is prime no. n", N); end else if (N%2==0) printf(“N=%ld is not prime no. n", N); else { // Odd numners // some odd no. are prime for (i=3; i<N/2; i+=2) // divisor i=3, 5, 7, …, N/2 if (N%i == 0) break; // not prime if (i > N/2) printf(“N=%ld is prime no. n", N); else printf(“N=%ld is not prime no. n", N); } 28 }