25
886201 หหหหหหหหหหหหหห 1 Lecture 9: หหหหหหหหหห (for)

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

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

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

ทบทวนเรื่��อง while

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

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

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

The for Statement•Syntax

for (ForInit ; ForExpression; PostExpression)

Action

•Examplefor ( int i = 0; i < 3; i++ )

{ cout << “i is " << i

<< endl;}

Page 5: 886201   หลักการ โปรแกรม  1
Page 6: 886201   หลักการ โปรแกรม  1

Execution Trace

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

i 0

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

Execution Trace

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

i 0

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

Execution Trace

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

i is 0

i 0

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

Execution Trace

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

i is 0

i 0

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

Execution Trace

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

i is 0

i 1

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

Execution Trace

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

i 1

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

Execution Trace

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

i is 0i is 1

i 1

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

Execution Trace

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

i is 0i is 1

i 1

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

Execution Trace

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

i is 0i is 1

i 2

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

Execution Trace

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

i is 0i is 1

i 2

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

Execution Trace

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

i is 0i is 1i is 2

i 2

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

Execution Trace

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

i is 0i is 1i is 2

i 2

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

Execution Trace

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

i is 0i is 1i is 2

i 3

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

Execution Trace

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

i is 0i is 1i is 2

i 3

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

Execution Trace

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

i is 0i is 1i is 2all done

i 3

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

Example: หาผลัรวมของเลัข 1 ถึ�ง 10

T

F

i=i+1

i<=10

sum = sum+i

i = 1

sum = 0

sum = 0;

for (int i=1; i <= 10; i++ )

sum += i;

RESULT:sum =1+2+3+...+10

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

Example: พิ�มพิ�เลัขคี่��ทำ��อยู่� ในช่ วง 1 ถึ�ง 10

T

F

i+=2

i < 10

i = 1

print i

for (int i=1; i<10;

i+=2)

cout << i << “ ”;

RESULT:

1 3 5 7 9

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

Example: พิ�มพิ�คี่ า 10 ลังมาจนถึ�ง 1

i = 10

print i

i=i-1

T

Fi>=1

for (int i=10; i >= 1; i--)

cout << i << “ ”;

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

initialization condition statements update

for (int count = 1; count <= 10; count++){ cout << count << endl;}

int count = 1; // Initialize the counterwhile (count <= 10) // Check the counter{ cout << count << endl;

count++; // Update the counter}

initialization condition statements update

while vs. for

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

แบบฝึ�กหั�ด• ให%เข�ยู่นโปรแกรมเพิ&�อแสดงคี่ า x2 + x – 4 เม&�อ x=5, 6, …,12

• ให%เข�ยู่นโปรแกรมเพิ&�อแสดงส�ตรคี่�ณแม 5