User Tools

Site Tools


Sidebar


Introduction


Basic Tutorials


Advance Tutorials


Useful Techniques


Examples

  • Simple Pipe Weight Calculator
  • Unit Convertor

Sidebar

tutorial:syntax

This is an old revision of the document!


Syntax

Syntax เบื้องต้น

สำหรับคนที่มีความรู้เกี่ยวกับภาษา Pascal บ้างแล้ว ผมขอสรุป Syntax ไว้ใน Hidden Blog ข้างล่างนี้ เพื่อความสะดวกในการเข้ามาดูครับ

Syntax Summary

Syntax Summary

Topic Format
Comments styles (3 forms)
//This is line comment;

{ This is block 
comment#1 }

(* This is block 
comment#2 *)
Type declaration
  TYPE
    //Enumerated Type
    TWeekDay = (Mon,Tue,Wed,Thu,Fri); 
                
    //Record type
    TMyRecord = record  
        Name:string;
        Age:integer;
      end;
      
    //Static Array
    TStArr1D = array[1..10] of integer;
    TStArr2D = array[1..3] of array[1..5] 
               of real;//Array (3 x 5)
    //Static Array with initial values           
    TStArr1D: array[1..3] of integer = (14,25,36);
               
    //DynamicArray 
    TDyArr1D = array of boolean;
    TDyArr2D = array of array of string;
    
    //Object
    TMyObject = object  
      private  
        length, width: integer; 
      public  
        procedure setlength(l: inteter);  
        function getlength: integer;  
      end;    
    
    //Class
    TMyClass = class
      private
        length, width: integer;
      public
        constructor create(l, w: integer);
        destructor destroy; override;
        procedure setlength(l: integer);
        function getlength: integer;
      end;    
Variable declaration
var MyInt:integer; MyReal:real; 
    x,y,z:boolean; //Multi-declaration
    MyDay:TWeekDay;
    Arr1:TStArr1D;
    Arr2:TStArr2D;
    MyObject:TMyObject;
    MyClass:TMyClass;
    MyInt:integer = 10; //initialize value
Variable assignment
  MyInt := 10; 
  MyReal := 2.521;
  x:=True; y:=false; z:=true;
  Arr1[1] := 10; //Array 1-D
  Arr2[1,3] :=22.85; //Array 2-D
Numeric operator
  z := x + y;  //Plus
  z := x - y;  //Minus
  z := x / y;  //Divide
  z := x div y; //Divide, result: integer type
  z := x * y;  //Multiply
  z := x % y;  //modulus
Relational operator
  A = B  //Equal
  A <> B  //Not Equal
  A > B  //Greater than
  A < B  //Less than
  A >= B  //Greater than or equal
  A <= B  //Less than or equal
Boolean operator
  X and Y //And
  X or Y  //Or
  not X  //Not



Program Structure

โครงสร้างของโปรแกรม Pascal โดยทั่วไป จะมีรูปแบบดังนี้

PROGRAM ProgramName ;

USES
  {include units (external .pas files)}

CONST
  {Constant declarations}
 
TYPE
  {Type declarations}
 
VAR
  {Variable declarations}

{Sub-programs declarations}

BEGIN
  {Executable statements (Main)}
END.

การเขียนโปรแกรมตามโครงสร้างดังกล่าว มีข้อสังเกตเพิ่มเติมดังนี้

  • ในส่วนไหนที่ไม่ได้ใช้งาน ให้ตัดออก เช่น ถ้าไม่ได้ใช้ส่วนของ TYPE ก็ให้ตัดออกไป (ดู Example-1)
  • โปรแกรมย่อย (Procedure/Function) โดยปกติจะประกาศหลังจากการประกาศตัวแปร VAR แต่ในความเป็นจริงแล้ว Function/Procedure สามารถประกาศได้ทุกที่ ที่อยู่หลังจากการประกาศ USES
  • ส่วนประกาศ (Declaration scope) สามารถนำมาใส่ในโปรแกรมย่อยจำพวก Procedure/Function ได้ (ยกเว้น uses) โดยส่วนประกาศเหล่านี้ จะกลายเป็น local Scope ของโปรแกรมย่อยนั้น (ดู Example-2)

Example-1: Program Structure

Example-1: Program Structure

ตัวอย่าง การเขียนโปรแกรมหาพื้นที่วงกลม
ข้อสังเกต: ไม่มีการใช้การประกาศ TYPE จึงตัดคำว่า TYPE นี้ออกจากโครงสร้าง

PROGRAM CircularAreaCalculator ;
 
CONST
  PI = 3.14159;
 
VAR
  Din:real;
  Dex:real;
  CArea:real;
  
BEGIN
  writeln('Please insert internal diameter');
  readln(Din);
  writeln('Please insert external area');
  readln(Dex);
  CArea:= PI*(Dex*Dex-Din*Din);
  writeln('Circular area is: ', CArea);  
END.

Example-2: Program Structure with Subprogram

Example-2: Program Structure with Subprogram

ตัวอย่าง การเขียนโปรแกรมหาพื้นที่วงกลม
ข้อสังเกต: มีการประกาศ subprogram ที่เป็น procedure showcalcarea ซึ่งจะเห็นว่าภายใน showcalcarea มีส่วนประกาศย่อยคือ const, type, var และ function squrevalue อยู่ภายใน

PROGRAM CircularAreaCalculator ;
  
VAR
  Din:real;
  Dex:real;
  CName:string;
  
procedure showcalcarea(nameCircle:string, Din,Dex:real);
  const PI = 3.14159;
  type TMyCircular = record
    name:string;
    din,dex:real;
    area_in,area_ex,area_tot:real;
    end;

  var Ain,Aex:real; MyCircular:TMyCircular; 
    function squrevalue(x:real):real;
    begin
      result:=x*x;
    end;
begin
    with MyCircular do 
    begin
      name:=nameCircle;
      din:=Din;
      dex:=Dex;
      area_in:=PI*squrevalue(Din)/4;
      area_ex:=PI*squrevalue(Dex)/4;
      area_tot:=area_ex-area_in;
    end;
    writeln('Total area is: ',MyCircular.area_tot);

end;
    
BEGIN
  writeln('Please insert name of circle');
  readln(CName);
  writeln('Please insert internal diameter');
  readln(Din);
  writeln('Please insert external area');
  readln(Dex);
  showcalcarea(CName,Din,Dex);
 
END.



Unit Structure

บางครั้งการแยกโค๊ดออกเป็นโมดูลย่อยแล้วค่อยเรียกใช้เข้ามาในโปรแกรมหลัก จะทำให้เราสะดวกในการค้นหาและแก้ไข อีกทั้งโค๊ดส่วนนี้ยังสามารถเอาไปใช้ในหลายๆโปรแกรมในภายหน้าได้อีก เราเรียกโมดูลที่เก็บโค๊ดเหล่านี้ว่า Unit
Unit คือไฟล์ .pas ที่มีส่วนประกาศหรือตัวแปรต่างๆ (uses, const, type, var) หรือโปรแกรมย่อย (functions/procedures) อยู่ภายใน ประกอบไปด้วยส่วนที่เป็น Public และ Private โดยโครงสร้าง Unit ของ Pascal จะมีรูปแบบดังนี้

unit unitname;
  //public scope -----
   
interface
  {uses, const, type, var, function, procedure ... declaration}
  
  //private scope -----
implementation
  {function and procedure details}
  
end.


ความรู้พื้นฐานก่อนเริ่มเขียน Pascal

Declaration and Implementation

การเขียนโปรแกรมนอกเหนือจากต้องรู้จักโครงสร้างของโปรแกรมแล้ว ยังต้องทำความเข้าใจกับสองคำนี้ คือ Declaration กับ Implementation ซึ่งนอกจากจะเกี่ยวข้องกับการเขียน Main Program แล้ว ยังเกี่ยวข้องกับ Sub-program ด้วยนั่นเอง (Sub-program คือ Procedure/Function หรือเรียกอีกอย่างว่า Method นั่นเอง เพื่อไม่ให้ดูซับซ้อนมากเกินไป ในหัวข้อนี้ จะขอเรียกพวกนี้สั้นๆว่า Sub-program แทน)

Declaration คือ การประกาศ ลองนึกภาพตามว่า หากเราจะหาว่าในโปรแกรมที่เราเขียนนี้ มี Method อะไรบ้าง เราต้องมาหาในนี้ก่อน เพราะมันเหมือนการสรุปว่า มี Method อะไรบ้าง

Implementation คือ ส่วนที่บ่งบอกว่า Program หรือ Sub-program นั้นทำอะไรบ้าง มีขั้นตอนอะไรบ้าง

ย้อนกลับไปดูที่โครงสร้างของ Program จะแบ่งแยกพื้นที่ได้ดังนี้

  • Declaration Scope คือ ส่วนแรก คือ บรรทัดที่ 1-15
  • Implementation Scope คือ ในส่วนของ BEGIN … END. หรือ บรรทัดที่ 17-19

PROGRAM ProgramName ;

USES
  {include units (external .pas files)}

CONST
  {Constant declarations}
 
TYPE
  {Type declarations}
 
VAR
  {Variable declarations}

{Sub-programs declarations}

BEGIN

  {Executable statements (Main)}
  
END.


unit unitname;
   
interface
  {uses, const, type, var, procedures/functions ... declaration}
  
implementation
  {Procedures/Functions implementation here}
end.

Built-in Functions ที่ควรทราบ

ก่อนการฝึกทำตัวอย่าง หรือเขียนโปรแกรมพื้นฐานง่ายๆ เราควรทำความรู้จักกับ Built-in Function ต่อไปนี้เสียก่อน

Functions Descriptions
writeln(string) หรือ writeln(string,value) แสดงข้อความและค่าของตัวแปร แล้วลงไปบรรทัดถัดไป
write(string) แสดงข้อความและค่าของตัวแปร แล้วอยู่บรรทัดเดิม
readln(variable) รับค่าใส่ Variable จากการพิมพ์ข้อมูลหน้าจอแล้วกด Enter บางครั้งเราใช้ readln() เฉยๆเพื่อเบรคโปรแกรมไว้ไม่ปิดตัวเอง

Example-3: การใช้งาน write, writeln และ readln

Example-3: การใช้งาน write, writeln และ readln

ตัวอย่าง โปรแกรมคำนวณ Body Mass Index (BMI)

program BMI_Calculation;

uses SysUtils; //for function: Format()

var Mass,Height,BMI:real;

begin

  write('Please insert your mass (kg): ');
  readln(Mass);   //Get input: Mass

  write('Please insert your Height (cm): ');
  readln(Height); //Get input: Height

  //Calculate
  BMI:=Mass/(Height*Height/1e4);
  writeln('----Result----');
  writeln('Your BMI is = ',Format('%.2f',[BMI]));

  readln();
end. 

Compiled Results:

Please insert your mass (kg): 60
Please insert your Height (cm): 170
----Result----
Your BMI is = 20.76  


tutorial/syntax.1546745162.txt.gz · Last modified: 2019/01/06 10:26 by admin