This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorial:loopstatement [2019/01/07 14:54] admin created |
tutorial:loopstatement [2019/01/30 09:10] (current) admin |
||
---|---|---|---|
Line 1: | Line 1: | ||
======Loop Statements====== | ======Loop Statements====== | ||
ประกอบไปด้วย For..Do, While..Do และ Repeat..Until ดังนี้ | ประกอบไปด้วย For..Do, While..Do และ Repeat..Until ดังนี้ | ||
- | ^ Statements ^ Syntax ^ | + | ^ Statements ^ Syntax ^ Example ^ |
|For..Do|<sxh delphi;> | |For..Do|<sxh delphi;> | ||
+ | For i:={Start} to {End} do | ||
+ | {Statement}; | ||
+ | </sxh> <sxh delphi;> | ||
+ | For i:={Start} downto {End} do | ||
+ | {Statement}; | ||
+ | </sxh> |<sxh delphi;> | ||
For i:=1 to 10 do | For i:=1 to 10 do | ||
- | {Statement1}; | + | writeln('i = ',i); |
- | </sxh> | | + | </sxh><sxh delphi;> |
+ | For i:=10 downto 1 do | ||
+ | writeln('i = ',i); | ||
+ | </sxh>| | ||
|While..Do|<sxh delphi;> | |While..Do|<sxh delphi;> | ||
- | i:=0; | + | While {Condition} do |
+ | {Statement}; | ||
+ | </sxh> |<sxh delphi;> | ||
+ | i:=0 | ||
While i<10 do | While i<10 do | ||
i:=i+1; | i:=i+1; | ||
</sxh> | | </sxh> | | ||
|Repeat..Until|<sxh delphi;> | |Repeat..Until|<sxh delphi;> | ||
+ | Repeat {Statement} until | ||
+ | {Condition}; | ||
+ | </sxh> |<sxh delphi;> | ||
i:=0; | i:=0; | ||
Repeat i:=i+1 until | Repeat i:=i+1 until | ||
- | (i<10); | + | i<10; |
+ | </sxh> | | ||
+ | |||
+ | จากตารางข้างบน เป็นรูปแบบ Loop ที่มี statement แค่บรรทัดเดียวต่อหนึ่ง condition เท่านั้น อย่างไรก็ตามหากมี statement หลายบรรทัด ต้องเติม begin .. end ด้วยทุกครั้ง __**ยกเว้น**__ Repeat..Until Loop ดังนี้ | ||
+ | |||
+ | <hidden Loops: Multiple Statements> | ||
+ | ^ Statements ^ Syntax ^ Example ^ | ||
+ | |For..Do|<sxh delphi;> | ||
+ | For i:={Start} to {End} do | ||
+ | begin | ||
+ | {Statement1}; | ||
+ | {Statement2}; | ||
+ | ... | ||
+ | end; | ||
+ | </sxh> |<sxh delphi;> | ||
+ | For i:=1 to 10 do | ||
+ | begin | ||
+ | j:=i*2+5; | ||
+ | writeln('i = ',i); | ||
+ | end; | ||
+ | </sxh> | | ||
+ | |While..Do|<sxh delphi;> | ||
+ | While {Condition} do | ||
+ | begin | ||
+ | {Statement1}; | ||
+ | {Statement2}; | ||
+ | ... | ||
+ | end; | ||
+ | </sxh> |<sxh delphi;> | ||
+ | i:=0 | ||
+ | While i<10 do | ||
+ | begin | ||
+ | writeln('i = ',i); | ||
+ | i:=i+1; | ||
+ | end; | ||
+ | </sxh> | | ||
+ | |Repeat..Until|<sxh delphi;> | ||
+ | Repeat | ||
+ | {Statement1}; | ||
+ | {Statement2}; | ||
+ | ... | ||
+ | until | ||
+ | {Condition}; | ||
+ | </sxh> |<sxh delphi;> | ||
+ | i:=0; | ||
+ | Repeat | ||
+ | i:=i+1; | ||
+ | writeln('i = ',i){;} | ||
+ | until | ||
+ | i<10; | ||
</sxh> | | </sxh> | | ||
+ | </hidden> | ||
+ | |||
+ | __**หมายเหตุ**__ - คำสั่งสุดท้ายของ Repeat ก่อนจะเข้าสู่ Until จะใส่เครื่องหมาย ";" ปิดท้ายหรือไม่ก็ได้ | ||
+ | |||
+ | \\ |