The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Pirapong Pridachom, 2019-08-27 22:45:10

9 คำสั่งวนซ้ำ

9 คำสั่งวนซ้ำ

คำ้ สง่ั วนซำ้ (Looping)

ครพู รี ะพงษ์ ปรีดำชม

1

หวั ขอ

•ค้ำสั่ง while
•คำ้ สั่ง do-while
•ค้ำส่ัง for

2

ค้ำสั่งวนซำ้

• ชนดิ ของกำรวนซำ้

1) กำรวนตำมเงอ่ื นไข

• while
• do-while

2) กำรวนซ้ำตำมจำ้ นวนนบั

• for

• รูปแบบ คำ้ ส่งั while 3
4
while (expression) while (expression)
statement; { statement;

statement;
...
}

คำ้ สง่ั while

• ตวั อยำง X=1 x=1

X=1 while (x<=5) while (x<=5)
{ printf(“A”); { printf(“%d”,x);
while (x<=5)
x++; x++; x++;
} }

5

ค้ำส่ัง do-while

• รปู แบบ

do
{ statement;

statement;

} while (expression);

6

คำ้ สั่ง do-while

• ตวั อยำง x=1;

x=1; do
{ printf(“%d”,x);
do
{ printf(“P”); x++;

x++; } while (x<=5);

} while (x<=5);

7

คำ้ สั่ง for

• รปู แบบ for (exp1;exp2;exp3) for (exp1;exp2;exp3)
Statement; { statement;

statement;

}

exp1 เป็นกำรก้ำหนดคำเรมิ่ ( i=1 ) 8
exp2 เป็นกำรทดสอบเงื่อนไข (i<=12) ซึง่ มผี ล 2 แบบ
ถำ TRUE จะท้ำ statement
ถำ FALSE จะจบค้ำส่ัง for
exp3 เป็นกำรก้ำหนดคำเพ่ิม (++) หรือลด (--)

ค้ำส่ัง for

• ตวั อยำง for ( i=5 ; i>=1 ; i--)
{ printf(“%d”,i);
for ( i=1 ; i<=5 ; i++)
printf(“%d\n”,i); printf(“\n”);

}

9


Click to View FlipBook Version