This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
tutorial:conditionalstatement [2019/01/07 10:50] admin [Break, Continue, Exit and Goto] |
tutorial:conditionalstatement [2019/01/07 13:00] (current) admin [Break, Continue, Exit and Goto] |
||
|---|---|---|---|
| Line 146: | Line 146: | ||
| ^ Procedures ^ Meaning ^ Format ^ | ^ Procedures ^ Meaning ^ Format ^ | ||
| - | | Break | ออกจาก Loop ปัจจุบัน (ใช้กับ Loop พวก For, While, Repeat) |<sxh delphi;> | + | | Break | ออกจาก Loop ปัจจุบัน (For/While/Repeat) |<sxh delphi;> |
| - | procedure ProcedureName; | + | for i:=1 to 10 do |
| - | begin | + | if i=5 then break; |
| - | {Do something} | + | |
| - | end; | + | writeln('i = ',i); //i = 5 |
| + | readln; | ||
| </sxh> | | </sxh> | | ||
| - | |Continue| สิ้นสุด Loop ปัจจุบัน เพื่อไปเข้า Loop ถัดไป (ใช้กับ Loop พวก For, While, Repeat) |<sxh delphi;> | + | |Continue| สิ้นสุด Loop ปัจจุบันทันที เพื่อเริ่ม Loop ปัจจุบันใหม่ในรอบถัดไป (For/While/Repeat) |<sxh delphi;> |
| - | procedure ProcedureName; | + | for i:=1 to 5 do |
| - | begin | + | begin |
| - | {Do something} | + | if i=4 then continue; |
| - | end; | + | writeln('i = ',i); //i = 4 won't be showed |
| + | end; | ||
| </sxh> | | </sxh> | | ||
| - | |Exit| ออกจาก Sub-Program ปัจจุบัน |<sxh delphi;> | + | |Exit| ออกจาก Sub-Program ปัจจุบัน (Procedure/Function) |<sxh delphi;> |
| - | procedure ProcedureName; | + | procedure MyProc; |
| begin | begin | ||
| - | {Do something} | + | exit; |
| + | writeln('After Exit'); //This won't be shown | ||
| end; | end; | ||
| </sxh> | | </sxh> | | ||
| - | |Goto| ไปยัง Label ที่กำหนด |<sxh delphi;> | + | |Goto| ไปยัง Label ที่ระบุ |<sxh delphi;> |
| - | PROGRAM Test_Goto; | + | label PointA,PointB; //declare labels |
| - | var Answer:Char; | + | |
| - | label RightWay,LeftWay,WrongWay; | + | |
| BEGIN | BEGIN | ||
| - | write('Please select your way: R/L'); | + | Goto PointB; |
| - | readln(Answer); | + | PointA: |
| - | if Answer='R' then | + | wrtieln('Passing PointA'); |
| - | Goto RightWay | + | exit; //Exit the program |
| - | else if Answer='L' then | + | PointB: |
| - | Goto LeftWay | + | wrtieln('Passing PointB'); |
| - | else | + | Goto PointA; |
| - | Goto WrongWay; | + | |
| - | + | ||
| - | RighWay: | + | |
| - | writeln('you have selected Right Way'); | + | |
| - | exit; | + | |
| - | LeftWay: | + | |
| - | writeln('you have selected Left Way'); | + | |
| - | exit; | + | |
| - | WrongWay: | + | |
| - | writeln('you have selected wrong Way'); | + | |
| - | exit; | + | |
| END. | END. | ||
| </sxh> | | </sxh> | | ||