บทที่ 6 > 6.4 การแก้ปัญหากับภาษาปาสคาล > 6.4.1 โครงสร้างภาษาปาสคาล 6/27  
     
        จากตัวอย่างในรูปด้านล่างจะเห็นว่า ภาษาปาสคาลแบ่งโปรแกรมออกเป็น 2 ส่วน ส่วนแรก คือบริเวณที่มีการแรเงาในรูป ส่วนนี้เรียกว่า ส่วนประกาศ เป็นส่วนที่มีการประกาศชื่อโปรแกรม ตัวแปรและข้อมูลต่างๆ ที่ใช้งานในโปรแกรมนี้

            สำหรับส่วนที่สองเรียกว่า ส่วนคำสั่ง ได้แก่ คำสั่งที่อยู่ภายในคำสั่ง begin และ end นอกจากนี้ในการเขียนโปรแกรมภาษาปาสคาล เมื่อผู้เขียนต้องการจบโปรแกรมต้องเติมเครื่องหมาย "." เพื่อจบการทำงานของโปรแกรมเสมอ
 
     
 
 

 

 

 

 

PROGRAM  Average_5;

Var    sum , N, data : integer;

         average : real;

(*  Declaration part*)

 

BEGIN

Write(The program will calculate the average of );

Writeln( five integers you enter through the keyboard …’);

Writeln;

sum := 0;

N := 1;

(*  main program *)

(* output statement *)

 

 

(* assignment statement *)

 

WHILE  N <= 5 DO

      Begin

               Write(Please enter the number : );

               Readln(data);

               sum := sum + data;

                N := N+1;

       End;

(* while loop *)

 

 

(* input statement *)

 

 

(* end of while loop *)

 

Average := sum / 5;

Write(The average of five numbers is , Average:6:2);

END.

END.

 

 

(* end of program *)

 

 
     
 
ก่อนหน้า   ถัดไป