250 likes | 357 Views
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 ; }.
E N D
886201 หลักการโปรแกรม 1 Lecture 9: การทำซ้ำ (for)
The for Statement • Syntax for (ForInit ; ForExpression; PostExpression) Action • Example for ( inti = 0; i < 3; i++ ) {cout << “iis " << i << endl; }
Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;
Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;
Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
Execution Trace i 0 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl; i is 0
Execution Trace i 1 for (int i = 0; i < 3; ++i) {cout << "i is " << i << endl; } cout << "all done" << endl;
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
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
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
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
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
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
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
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
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
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
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
i = 10 i>=1 F T print i Example: พิมพ์ค่า 10 ลงมาจนถึง 1 for (inti=10; i >= 1; i--) cout << i << “ ”; i=i-1
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
แบบฝึกหัด • ให้เขียนโปรแกรมเพื่อแสดงค่า x2 + x – 4 เมื่อ x=5, 6, …,12 • ให้เขียนโปรแกรมเพื่อแสดงสูตรคูณแม่ 5