User Tools

Site Tools


tutorial:conditionalstatement

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorial:conditionalstatement [2019/01/04 12:51]
admin [If - Statements]
tutorial:conditionalstatement [2019/01/07 13:00] (current)
admin [Break, Continue, Exit and Goto]
Line 71: Line 71:
  
  
-<hidden Example-1: If Statements (If..Else If..Else)>​ 
-**ตัวอย่าง** โปรแกรมตัดคะแนน ให้กลายเป็นเกรด A-F\\ 
-<sxh delphi;​highlight:​ [9-21]> 
-program Nested_If_Then_Else;​ 
  
-var Score:real; 
  
-begin +===== Case - Statements ===== 
-  ​write('​Please insert your score(0-100)')+ Statements ​ ^  Syntax ​ ^  
-  ​readln(Score);+|Exact value|<​sxh delphi;>​ 
 +//---Single Statement--- 
 +case {Var} of  
 +  1{Statement1}
 +  ​2: {Statement2};​ 
 +  3: {Statement3};​ 
 +  else {Statement4};​ 
 +end;  
 +</​sxh>​ | 
 +|Range|<​sxh delphi;>​ 
 +case {Var} of  
 +  1..10: {Statement1};​ 
 +  11..20: {Statement2};​ 
 +  21..30: {Statement3};​ 
 +  else {Statement4};​ 
 +end
  
-  if (Score<​50)and(Score>​0) then +</​sxh> ​|
-     ​writeln('​Your Grade is : F') +
-  else if Score<60 then +
-     ​writeln('​Your Grade is : D') +
-  else if Score<70 then +
-     ​writeln('​Your Grade is : C') +
-  else if Score<80 then +
-     ​writeln('​Your Grade is : B') +
-  else if Score<80 then +
-     ​writeln('​Excellent!,​ your gade is : A') +
-  else +
-     ​writeln('​Your score is not correct!'​);​ +
- +
-  readln(); +
-end.     +
-</​sxh> ​ +
-   +
-</​hidden>​  +
- +
-<hidden Example-2: If Statements (Nested If)> +
-**ตัวอย่าง** โปรแกรมตัดคะแนน ให้กลายเป็นเกรด A-F\\ +
-__**ข้อสังเกต**__ ​ มีการใช้ If ซ้อน If (Nested If)  +
-นอกจากนี้ให้ลองสังเกต If..Then..Else if..Else ในบรรทัดที่ 12-21  +
-จะเห็นว่าไม่มีการใช้ Begin ... End +
-เพราะถือเป็น Blog เดียวกัน\\ +
-<sxh delphi;​highlight:​ [9-21]>​ +
-program Nested_If_Then_Else;​ +
- +
-var Score:​real;​ +
- +
-begin +
-  write('​Please insert your score(0-100):​ '); +
-  readln(Score);​+
  
-  if (Score<​0)or(Score>​100) then +จากตารางข้างบน เป็นรูปแบบ Case ที่มี statement แค่บรรทัดเดียวต่อหนึ่ง condition เท่านั้น อย่างไรก็ตามหากมี statement หลายบรรทัด ต้องเติม begin .. end ด้วยทุกครั้ง ดังนี้
-    writeln('​Your score is not correct!!'​) +
-  else +
-    if Score<50 then +
-       ​writeln('​Your Grade is : F') +
-    else if Score<60 then +
-       ​writeln('​Your Grade is : D') +
-    else if Score<70 then +
-       ​writeln('​Your Grade is : C') +
-    else if Score<80 then +
-       ​writeln('​Your Grade is : B') +
-    else +
-       ​writeln('​Excellent!,​ your Grade is : A');+
  
-  readln(); 
-end.    ​ 
-</​sxh> ​ 
-  ​ 
-</​hidden>​ \\ \\ 
  
 <hidden Case-Statements:​ Multiple Statements>​ <hidden Case-Statements:​ Multiple Statements>​
Line 181: Line 142:
 </​sxh>​ | </​sxh>​ |
 </​hidden>​ \\ </​hidden>​ \\
 +===== Break, Continue, Exit and Goto =====
 +คำสั่ง Break, Continue, Exit และ Goto สรุปได้ดังนี้ ​
  
-===== Case - Statements ===== +^  ​Procedures ​ ​^  ​Meaning ​ ^  Format ​ ^ 
-^  ​Statements ​ ​^  ​Syntax ​ ^  +Break | ออกจาก Loop ปัจจุบัน (For/​While/​Repeat) ​|<sxh delphi;>​ 
-|Exact value|<sxh delphi;> +  ​for i:=to 10 do 
-//---Single Statement--- +    if i=5 then break;
-case {Var} of  +
-  1: {Statement1};​ +
-  2: {Statement2};​ +
-  3: {Statement3};​ +
-  else {Statement4};​ +
-end+
  
-//---Multiple Statements--- +  writeln('​i = ',​i);  ​//i = 5 
-case {Var} of  +  ​readln
-  ​1: begin  +
-      {Statement1};​ +
-      {Statement2};​ +
-      ...; +
-      end; +
-  2: begin +
-      {Statement3};​ +
-      {Statement4};​ +
-      {Statement5};​ +
-      ... +
-      end; +
-  3: {Statement6};​ +
-  else {Statement7};​ +
-end+
 </​sxh>​ | </​sxh>​ |
-|Range|<sxh delphi;> +|Continue| สิ้นสุด Loop ปัจจุบันทันที เพื่อเริ่ม Loop ปัจจุบันใหม่ในรอบถัดไป (For/​While/​Repeat) ​|<sxh delphi;>​ 
-//---Single Statement--- +  ​for i:=to 5 do 
-case {Var} of  +  ​begin 
-  1..10: {Statement1};​ +    if i=4 then continue
-  ​11..20: {Statement2}+    ​writeln('​i = ',i)//i = 4 won't be showed 
-  ​21..30:​ {Statement3}+  end; 
-  ​else {Statement4};​ +
-end;  +
 </​sxh>​ | </​sxh>​ |
-\\ +|Exit| ​อกจก Sub-Program ​ปัจจุบัน (Procedure/​Function) |<sxh delphi;>​ 
-<hidden Example-3: Case Statements>​ +procedure MyProc;
-**ตัวย่ง** โรแกรมตดคะแน ให้กลายเป็นเกรด A-F\\ +
-<sxh delphi;highlight: [10-16]+
-program TestCaseStatement; +
- +
-var score:​integer;​ +
-  Grade:​char;​ +
 begin begin
-  ​write('Insert your score (0 to 100): '); +  ​exit; 
-  ​readln(score)+  writeln('After Exit'​); ​//This won't be shown 
-   +end
-  case score of +</​sxh>​ | 
-    0..50: Grade:​='​F'​+|Goto| ไปยัง Label ที่ระบุ |<sxh delphi;> 
-    ​51..60:​ Grade:​='​D'​+label PointA,​PointB//declare labels 
-    ​61..70:​ Grade:​='​C'​+BEGIN 
-    ​71..80Grade:​='​B';​ + Goto PointB
-  else Grade:='A'; + PointA
-  end+ wrtieln('Passing PointA')
-   + exit//Exit the program 
-  ​writeln('Your grade is: ',Grade); + PointB: 
-  ​readln()+ wrtieln('Passing PointB'); 
-end  + Goto PointA
-</​sxh> ​ +END
- +</​sxh> ​|
-Compiled Results: +
- +
-  Insert your score (0 to 100): 35 +
-  Your grade is: F +
-   +
-</​hidden>​ \\ \\ +
- +
-===== Break, Continue and Goto =====+
tutorial/conditionalstatement.1546581111.txt.gz · Last modified: 2019/01/04 12:51 by admin