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 09:12] admin old revision restored (2018/12/26 19:36) |
tutorial:conditionalstatement [2019/01/07 13:00] (current) admin [Break, Continue, Exit and Goto] |
||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ^ Statements ^ Syntax ^ | ^ Statements ^ Syntax ^ | ||
| |If..Then|<sxh delphi;> | |If..Then|<sxh delphi;> | ||
| - | //---Single Statement--- | ||
| if {Condition} then {Statement}; | if {Condition} then {Statement}; | ||
| + | </sxh> | | ||
| + | |If..Then..Else|<sxh delphi;> | ||
| + | if {Condition} then | ||
| + | {Statement1} | ||
| + | else | ||
| + | {Statement2}; | ||
| + | </sxh> | | ||
| + | |If..Then/ Else If..Then/ Else|<sxh delphi;> | ||
| + | if {Condition1} then | ||
| + | {Statement1} | ||
| + | else if {Condition2} then | ||
| + | {Statement2} | ||
| + | else | ||
| + | {Statement3}; | ||
| + | </sxh> | | ||
| + | |||
| + | จากตารางข้างบน เป็นรูปแบบ If ที่มี statement แค่บรรทัดเดียวต่อหนึ่ง condition เท่านั้น อย่างไรก็ตามหากมี statement หลายบรรทัด ต้องเติม begin .. end ด้วยทุกครั้ง ดังนี้ | ||
| - | //---Multiple Statements--- | + | <hidden If-Statement: Multiple Statements> |
| + | |If..Then|<sxh delphi;> | ||
| if {Condition} then | if {Condition} then | ||
| begin | begin | ||
| Line 15: | Line 32: | ||
| </sxh> | | </sxh> | | ||
| |If..Then..Else|<sxh delphi;> | |If..Then..Else|<sxh delphi;> | ||
| - | //---Single Statement--- | ||
| - | if {Condition} then | ||
| - | {Statement1} | ||
| - | else | ||
| - | {Statement2}; | ||
| - | |||
| - | //---Multiple Statements--- | ||
| if {Condition} then | if {Condition} then | ||
| begin | begin | ||
| Line 35: | Line 45: | ||
| end; | end; | ||
| </sxh> | | </sxh> | | ||
| - | \\ | + | |If..Then/ Else If..Then/ Else|<sxh delphi;> |
| + | //---Single Statement--- | ||
| + | if {Condition1} then | ||
| + | begin | ||
| + | {Statement1}; | ||
| + | {Statement2}; | ||
| + | {...}; | ||
| + | end | ||
| + | else if {Condition2} then | ||
| + | begin | ||
| + | {Statement3}; | ||
| + | {Statement4}; | ||
| + | {...}; | ||
| + | end | ||
| + | else | ||
| + | begin | ||
| + | {Statement5}; | ||
| + | {Statement6}; | ||
| + | {...}; | ||
| + | end; | ||
| + | |||
| + | </sxh> | | ||
| + | </hidden> \\ | ||
| + | |||
| Line 47: | Line 81: | ||
| 2: {Statement2}; | 2: {Statement2}; | ||
| 3: {Statement3}; | 3: {Statement3}; | ||
| + | else {Statement4}; | ||
| + | end; | ||
| + | </sxh> | | ||
| + | |Range|<sxh delphi;> | ||
| + | case {Var} of | ||
| + | 1..10: {Statement1}; | ||
| + | 11..20: {Statement2}; | ||
| + | 21..30: {Statement3}; | ||
| else {Statement4}; | else {Statement4}; | ||
| end; | end; | ||
| - | //---Multiple Statements--- | + | </sxh> | |
| + | |||
| + | จากตารางข้างบน เป็นรูปแบบ Case ที่มี statement แค่บรรทัดเดียวต่อหนึ่ง condition เท่านั้น อย่างไรก็ตามหากมี statement หลายบรรทัด ต้องเติม begin .. end ด้วยทุกครั้ง ดังนี้ | ||
| + | |||
| + | |||
| + | <hidden Case-Statements: Multiple Statements> | ||
| + | ^ Statements ^ Syntax ^ | ||
| + | |Exact value|<sxh delphi;> | ||
| case {Var} of | case {Var} of | ||
| 1: begin | 1: begin | ||
| Line 68: | Line 117: | ||
| </sxh> | | </sxh> | | ||
| |Range|<sxh delphi;> | |Range|<sxh delphi;> | ||
| - | //---Single Statement--- | ||
| case {Var} of | case {Var} of | ||
| - | 1..10: {Statement1}; | + | 1..10: begin |
| - | 11..20: {Statement2}; | + | {Statement1}; |
| - | 21..30: {Statement3}; | + | {Statement2}; |
| - | else {Statement4}; | + | ...; |
| + | end; | ||
| + | 11..20: begin | ||
| + | {Statement3}; | ||
| + | {Statement4}; | ||
| + | ...; | ||
| + | end; | ||
| + | 21..30: begin | ||
| + | {Statement5}; | ||
| + | {Statement6}; | ||
| + | ...; | ||
| + | end; | ||
| + | else begin | ||
| + | {Statement7}; | ||
| + | {Statement8}; | ||
| + | ...; | ||
| + | end; | ||
| end; | end; | ||
| </sxh> | | </sxh> | | ||
| - | \\ | + | </hidden> \\ |
| - | ===== Break, Continue and Goto ===== | + | ===== Break, Continue, Exit and Goto ===== |
| + | คำสั่ง Break, Continue, Exit และ Goto สรุปได้ดังนี้ | ||
| + | |||
| + | ^ Procedures ^ Meaning ^ Format ^ | ||
| + | | Break | ออกจาก Loop ปัจจุบัน (For/While/Repeat) |<sxh delphi;> | ||
| + | for i:=1 to 10 do | ||
| + | if i=5 then break; | ||
| + | |||
| + | writeln('i = ',i); //i = 5 | ||
| + | readln; | ||
| + | </sxh> | | ||
| + | |Continue| สิ้นสุด Loop ปัจจุบันทันที เพื่อเริ่ม Loop ปัจจุบันใหม่ในรอบถัดไป (For/While/Repeat) |<sxh delphi;> | ||
| + | for i:=1 to 5 do | ||
| + | begin | ||
| + | if i=4 then continue; | ||
| + | writeln('i = ',i); //i = 4 won't be showed | ||
| + | end; | ||
| + | </sxh> | | ||
| + | |Exit| ออกจาก Sub-Program ปัจจุบัน (Procedure/Function) |<sxh delphi;> | ||
| + | procedure MyProc; | ||
| + | begin | ||
| + | exit; | ||
| + | writeln('After Exit'); //This won't be shown | ||
| + | end; | ||
| + | </sxh> | | ||
| + | |Goto| ไปยัง Label ที่ระบุ |<sxh delphi;> | ||
| + | label PointA,PointB; //declare labels | ||
| + | BEGIN | ||
| + | Goto PointB; | ||
| + | PointA: | ||
| + | wrtieln('Passing PointA'); | ||
| + | exit; //Exit the program | ||
| + | PointB: | ||
| + | wrtieln('Passing PointB'); | ||
| + | Goto PointA; | ||
| + | END. | ||
| + | </sxh> | | ||