1 / 25

886201 หลักการ โปรแกรม 1

886201 หลักการ โปรแกรม 1. Lecture 9: การทำซ้ำ ( for). ทบทวนเรื่อง while. ทบทวนเรื่อง do-while. The for Statement. Syntax for ( ForInit ; ForExpression ; PostExpression ) Action Example for ( int i = 0 ; i < 3 ; i ++ ) { cout << “i is " << i << endl ; }.

Download Presentation

886201 หลักการ โปรแกรม 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 886201 หลักการโปรแกรม 1 Lecture 9: การทำซ้ำ (for)

  2. ทบทวนเรื่อง while

  3. ทบทวนเรื่อง do-while

  4. The for Statement • Syntax for (ForInit ; ForExpression; PostExpression) Action • Example for ( inti = 0; i < 3; i++ ) {cout << “iis " << i << endl; }

  5. Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;

  6. Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;

  7. Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0

  8. Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0

  9. Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0

  10. Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;

  11. Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1

  12. Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1

  13. Execution Trace i 2 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1

  14. Execution Trace i 2 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1

  15. Execution Trace i 2 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2

  16. Execution Trace i 2 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2

  17. Execution Trace i 3 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2

  18. Execution Trace i 3 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2

  19. Execution Trace i 3 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 all done

  20. sum = 0 i = 1 F i<=10 T sum = sum+i i=i+1 Example: หาผลรวมของเลข 1 ถึง 10 sum = 0; for (inti=1;i <= 10;i++ ) sum += i; RESULT: sum =1+2+3+...+10

  21. Example: พิมพ์เลขคี่ที่อยู่ในช่วง 1 ถึง 10 for (inti=1; i<10; i+=2) cout << i << “ ”; RESULT: 1 3 5 7 9 i = 1 i < 10 F T print i i+=2

  22. i = 10 i>=1 F T print i Example: พิมพ์ค่า 10 ลงมาจนถึง 1 for (inti=10; i >= 1; i--) cout << i << “ ”; i=i-1

  23. while vs. for int count = 1;// Initialize the counter while(count <= 10)// Check the counter { cout << count << endl; count++;// Update the counter } initializationconditionstatementsupdate for(int count = 1;count <= 10;count++) { cout << count << endl; } initializationconditionstatementsupdate

  24. แบบฝึกหัด • ให้เขียนโปรแกรมเพื่อแสดงค่า x2 + x – 4 เมื่อ x=5, 6, …,12 • ให้เขียนโปรแกรมเพื่อแสดงสูตรคูณแม่ 5

More Related