บทที่ 4 (ส่วนที่ 1)

101
204101 Introduction to Computer 1 บบบบบ 4 (บบบบบบบ 1) บบบบบบบบบบ (Pascal Language) บบบบบบบบบบบบบบบบบบบบบ บบบบบบ (Sequence)

Upload: kennan-ferguson

Post on 02-Jan-2016

42 views

Category:

Documents


0 download

DESCRIPTION

บทที่ 4 (ส่วนที่ 1). ภาษาปาสคาล (Pascal Language) และการเขียนโปรแกรมแบบลำดับ ( Sequence ). ภาษาปาสคาล ( Pascal). เป็นภาษาระดับสูง เช่นเดียวกับ C, Java, Ada, BASIC, Perl ถูกสร้างโดย Prof. Niklaus Wirth ในปี ค.ศ.1970 ชื่อของภาษาถูกตั้งขึ้นเพื่อเป็นเกียรติแก่ Blaise Pascal - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 1

บทท�� 4 (ส่�วนท�� 1)

ภาษาปาส่คาล (Pascal Language)

และการเขี�ยนโปรแกรมแบบล�าดั�บ (Sequence)

Page 2: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 2

ภาษาปาส่คาล (Pascal) เป�นภาษาระดั�บส่�ง เช่�นเดั�ยวก�บ C, Java, Ada,

BASIC, Perlถู�กส่ร!างโดัย Prof. Niklaus Wirth ในป# ค.ศ.1970

ช่%�อขีองภาษาถู�กตั้�(งขี)(นเพื่%�อเป�นเก�ยรตั้+แก� Blaise Pascal

ม�ล�กษณะเหม%อนภาษาอ�งกฤษท�าให!ง�ายตั้�อการโปรแกรมผู้�!เขี�ยนโปรแกรมไม�จำ�าเป�นตั้!องม�ความร�!ดั!านฮาร3ดัแวร3

ไม�ขี)(นอย��ก�บเคร%�อง(Machine Independent) จำะน�าไปใช่!ก�บเคร%�องใดัก4ไดั!

ตั้!องม�ตั้�วแปลภาษาเพื่%�อแปลงจำากภาษาระดั�บส่�งเป�นภาษาเคร%�องปาส่คาลคอมไพื่เลอร3 (Pascal Compiler)

Page 3: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 3

การท�างานขีองปาส่คาลคอมไพื่เลอร3Source Program : ไฟล3โปรแกรมขีองช่6ดัค�า

ส่��งตั้�างๆ ท��พื่)�งเขี�ยนเส่ร4จำดั!วยภาษาโปรแกรมคอมพื่+วเตั้อร3

Object Program :ไฟล3โปรแกรมท��ผู้�านการแปล (Compile) จำากคอมไพื่เลอร3แล!ว

Executed Program: โปรแกรมท��ผู้�านการรวม (Link) เขี!าก�บไลบราล� (Libraly) ขีองระบบแล!ว

Source Program“ซอร3ส่โปรแกรม” Object Program

Compile

LIBRALY

Executed Program“เอ4กซ3ซ�ค+วท3โปรแกรม”

Link

xxx.pas xxx.obj xxx.exe

Page 4: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 4

การท�างานขีองปาส่คาลคอมไพื่เลอร3

1 ค�าส่��ง จำะถู�กแปลเป�นภาษาเคร%�องไดั!มากกว�า 1 ค�าส่��ง

การแปลท�(งโปรแกรมให!เป�นภาษาเคร%�องในคร�(งเดั�ยวไดั!เอ4กซ3ซ�ค+วท3โปรแกรมเป�นไฟล3ผู้ลล�พื่ธ์3ท��ส่ามารถูน�า

ไปใช่!งานไดั!ตั้ลอดั เช่�น โปรแกรมเคร%�องค+ดัเลขีการเปล��ยนแปลงหร%อแก!ไขีส่�วนใดัส่�วนหน)�งขีอง

โปรแกรม ตั้!องม�การคอมไพื่ล3โปรแกรมใหม�ตั้�(งแตั้�ตั้!น

ในระหว�างการคอมไพื่ล3 ถู!าม�ขี!อผู้+ดัพื่ลาดั คอมไพื่เลอร3ก4จำะแจำ!งให!ทราบเพื่%�อท�าการแก!ไขีตั้�อไป

Page 5: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 5

ส่�วนประกอบขีองภาษาปาส่คาล

1 .ส่�วนห�วขีองโปรแกรม (Header)ช่%�อโปรแกรม (Program Name)ประกาศตั้�วแปรก�าหนดัช่น+ดัขี!อม�ลใหม�2. ส่�วนตั้�วขีองโปรแกรม (Body)กล6�มขีองค�าส่��งท��อย��ระหว�าง begin และ end

program First;var number : integer;

beginwriteln('First

Program');

end.ตั้�วอย�างขีองภาษาปาส่คาล

Page 6: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 6

ตั้�วแปร (Variable)จำองพื่%(นท��ในหน�วยความจำ�าเพื่%�อเก4บขี!อม�ลท��ตั้!องการใช่!ในโปรแกรม ตั้�วแปรท6กตั้�วตั้!องม�การประกาศ (Declaration) ก�อนการใช่!งาน ค�าส่��งส่�าหร�บการประกาศตั้�วแปรในภาษาปาส่คาลค%อค�าส่��ง VAR

ร�ปแบบ VARName : Type;โดัยท�� Name ช่%�อตั้�วแปร

Type ช่น+ดัขี!อม�ลขีองตั้�วแปร

ตั้�วอย�าง VAR Salary : Integer;ตั้�วอย�างการประกาศตั้�วแปรช่%�อ Salary เพื่%�อจำองพื่%(นท��ส่�าหร�บ

เก4บขี!อม�ลช่น+ดัเลขีจำ�านวนเตั้4ม

Page 7: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 7

ขี!อก�าหนดัในการตั้�(งช่%�อตั้�วแปร

1 .อ�กษรตั้�วแรกตั้!องเป�นตั้�วอ�กษร (A-Z หร%อ a-z) หร%อ เคร%�องหมาย _ (Underscore) เท�าน�(น

2.อ�กษรตั้�วอ%�น ๆ ตั้!องเป�นตั้�วอ�กษร (A-Z หร%อ a-z) หร%อ ตั้�วเลขี - 09 หร%อเคร%�องหมาย _ เท�าน�(น

3.ห!ามตั้�(งช่%�อตั้�วแปรซ�(าก�บค�าส่งวน (Reserved Word)

ค�าส่งวนค%อค�าท��ถู�กก�าหนดัความหมายในการน�าไปใช่!งานไว!แล!ว เช่�น And, Begin, Do, End, For, Not, If, Mod, Not, Or, Program, Repeat, Then, To, Type, Until, และ Var

Page 8: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 8

ขี!อก�าหนดัในการตั้�(งช่%�อตั้�วแปร (ตั้�อ)

4. ห!ามม�ช่�องว�างภายในช่%�อ5. ไม�ควรตั้�(งช่%�อตั้�วแปรซ�(าก�บค�ามาตั้รฐาน

(Standard Identifier)ค�ามาตั้รฐานค%อค�าท��ถู�กก�าหนดัความหมายใน

การน�าไปใช่!งานไว!แล!ว โดัยปกตั้+จำะเป�นค�าส่��งตั้�าง ๆ เช่�น Write, Writeln, Read และ Readln เป�นตั้!น

ช่%�อตั้�วแปร ช่%�อฟ<งก3ช่�น ช่%�อค�าคงท��6. ตั้�วอ�กษรตั้�วเล4กและตั้�วใหญ่�ถู%อว�าไม�ตั้�างก�น เช่�น

Num, NUM และ num ม�ความหมายเดั�ยวก�น

Page 9: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 9

ตั้�วอย�างการตั้�(งช่%�อตั้�วแปรRoom_number ถู�กตั้!อง123ABC ผู้+ดั ห!ามขี)(นตั้!น

ดั!วยตั้�วเลขี_cAt ถู�กตั้!องme@CMUผู้+ดั ห!ามใช่!เคร%�องหมาย @

My Book ผู้+ดั ห!ามเว!นวรรค

Page 10: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 10

ช่น+ดัขี!อม�ล (Data Types)

Integer จำ�านวนเตั้4มReal จำ�านวนจำร+งหร%อทศน+ยมChar ตั้�วอ�กขีระStringขี!อความBoolean ค�าความจำร+ง ค%อจำร+ง (True) หร%อ เท4จำ (False)

Page 11: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 11

Data Types : Integer and Real

เก4บตั้�วเลขีจำ�านวนเตั้4ม (Integer) ม�ค�าอย��ในช่�วง -32,768 ถู)ง 32,767

ใช่!พื่%(นท��ในการเก4บขี!อม�ล 2 ไบตั้3 ( 16 บ+ตั้)ตั้�วอย�างการประกาศตั้�วแปร var It : integer;เก4บตั้�วเลขีจำ�านวนจำร+ง (real) หร%อ

ทศน+ยมท��ม�ค�าอย��ในช่�วง2.9e-39 ถู)ง 1.7e38 29( . x 10-

39 ถู)ง 17 x 1038)ใช่!พื่%(นท��ในการเก4บขี!อม�ล 6 ไบตั้3 ( 48

บ+ตั้)ตั้�วอย�างการประกาศตั้�วแปร var Rl : real;

Page 12: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 12

Data Types : Char (ตั้�วอ�กขีระ)ใช่!เก4บตั้�วอ�กขีระ 1 ตั้�ว ในระบบ ASCII

ใช่!พื่%(นท��ในการเก4บขี!อม�ล 1 ไบตั้3 ( 8 บ+ตั้)ตั้�วอย�างการประกาศตั้�วแปร var Ch : char;

Page 13: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 13

Data Types : String (ขี!อความ)ใช่!เก4บขี!อความ 1 ขี!อความ (กล6�มขีองตั้�ว

อ�กขีระ)ร�ปแบบการประกาศตั้�วแปรช่น+ดัขี!อความ

var Name : string[size];size ค%อจำ�านวนตั้�วอ�กขีระส่�งส่6ดัท��ส่ามารถูเก4บไดั!

หากไม�ก�าหนดั size คอมไพื่เลอร3จำะก�าหนดัค�าให!โดัยอ�ตั้โนม�ตั้+เป�น 255

ตั้�วอย�างการประกาศตั้�วแปรvar St1 : string[10];var St2 : string;

Page 14: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 14

Data Types : Boolean (ค�าความจำร+ง)

ใช่!เก4บค�าความจำร+ง ไดั!แก� จำร+ง (True) และ เท4จำ (False)

ใช่!พื่%(นท��ในการเก4บขี!อม�ล 1 ไบตั้3 ( 8บ+ตั้)

ตั้�วอย�างการประกาศตั้�วแปรvar Bl : boolean;

Page 15: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 15

การให!ค�าตั้�วแปรใช่!เคร%�องหมาย := (Assignment Operator)การให!ค�าตั้�วแปรม�ร�ปแบบ Variable := Value;

program My_Program;

var In : integer;var Rl : real;var Ch : char;var St : string[30];var Bl : boolean;

beginIt := -27;Rl := 821.459;Ch := 'F';St := 'Pascal Language';Bl := true;

end.กรณ�ตั้�วแปรช่น+ดั char หร%อตั้�วแปรช่น+ดั string ค�าขี!อม�ลท��จำะก�าหนดัให!ก�บตั้�วแปร 2 ประเภทน�(ตั้!องอย��ภายใตั้!เคร%�องหมายฟ<นหน� ' ' (single quote)

Page 16: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 16

ตั้�วดั�าเน+นการและน+พื่จำน3 (Operators and Expressions) ตั้�วดั�าเน+นการ

ตั้�วดั�าเน+นการทางคณ+ตั้ศาส่ตั้ร3 (Arithmetic Operators)

ตั้�วดั�าเน+นการเปร�ยบเท�ยบ (Comparison Operators)

ตั้�วดั�าเน+นการทางตั้รรกศาส่ตั้ร3 (Logical Operators)

น+พื่จำน3Now +++

Page 17: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 17

ตั้�วดั�าเน+นการทางคณ+ตั้ศาส่ตั้ร3 (Arithmetic Operators)

+ Addition บวก- Subtraction ลบ* Multiplication ค�ณ/ Division หารdiv Integer Division

หารจำ�านวนเตั้4มmod Modulo หารเอา

เศษ

Page 18: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 18

ตั้�วอย�างการใช่!ตั้�วดั�าเน+นการทางคณ+ตั้ศาส่ตั้ร3

PROGRAM My_Program;var x, y, z : integer;

BEGINx := 7;y := 4;z := x + y; { z = 11 }z := x - y; { z = 3 }z := x * y; { z = 28 }z := x / y; { z = 1.75 }z := x div y; { z = 1 }z := x mod y; { z = 3 }

END.

หมายเหตั้6 : ประกาศหลายตั้�วแปร แตั้�ม�ช่น+ดัขีองตั้�วแปรเป�นประเภทเดั�ยวก�นหมดัให! ค��นแตั้�ละช่%�อตั้�วแปรดั!วยคอมมา

ภายในส่�ญ่ล�กษณ3 { } ค%อ ค�า

อธ์+บาย(Comment) ไม�ม�ผู้ลตั้�อการท�างาน

ขีองโปรแกรม

Page 19: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 19

ตั้�วดั�าเน+นการเปร�ยบเท�ยบ (Comparison Operators)

Operator Example Meaning

= x = y ให!ค�า True ถู!าค�า x เท�าก�บ ค�า y<> x <> y ให!ค�า True ถู!าค�า x ไม�เท�าก�บ ค�า y> x > y ให!ค�า True ถู!าค�า x มากกว�า ค�า y< x < y ให!ค�า True ถู!าค�า x น!อยกว�า ค�า y>= x >= y ให!ค�า True ถู!าค�า x มากกว�าหร%อเท�าก�บ ค�า y<= x <= y ให!ค�า True ถู!าค�า x น!อยกว�าหร%อเท�าก�บ ค�า y

Page 20: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 20

ตั้�วดั�าเน+นการทางตั้รรกศาส่ตั้ร3

x y x and y x or ynot(y)

T T T T FT F F T TF T F TF F F F

And , or และ not

Page 21: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 21

ตั้�วอย�างการใช่!ตั้�วดั�าเน+นการทางตั้รรกศาส่ตั้ร3

PROGRAM My_Program;

var x, y : integer;

var z : boolean;

BEGIN

x := 7;

y := 4;

z := x <> y; {z= True }

z := (x > y) and (x = y); {z=True and False so z=False}

z := not(x <= y); {z= not(F) z = True}

END.

ค�าอธ์+บาย(Comment) ไม�ม�ผู้ลตั้�อการท�างานขีองโปรแกรม

Page 22: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 22

น+พื่จำน3 (Expressions) ค�าคงท��หร%อตั้�วแปรท��ม�การใช่!ตั้�วดั�าเน+น

การน+พื่จำน3ทางคณ+ตั้ศาส่ตั้ร3ตั้ามปกตั้+

x2 + 4y + 58y2 - 8z = 0

น+พื่จำน3ทางคณ+ตั้ศาส่ตั้ร3ในภาษาปาส่คาลx*x + 4*y + 58*y*y – 8*z = 0

Page 23: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 23

ล�าดั�บความส่�าค�ญ่ขีองตั้�วดั�าเน+นการ (Precedence)

ล�าดั�บ เคร%�องหมาย1 ( )2 - หมายถู)งเคร%�องหมายลบ เช่�น -93 not4 * / div mod and5 - + or6 = < <= > >= <>

Page 24: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 24

ตั้�วอย�างล�าดั�บความส่�าค�ญ่ขีองตั้�วดั�าเน+นการ (ตั้�อ)

ส่มมตั้+ว�าม�การก�าหนดัค�าตั้�วแปรแตั้�ละตั้�วม�การก�าหนดัค�าดั�งน�(x := 16;

y := 4;

c := 2.5;

d := 0.25;

ตั้�วอย�าง 1c * d - x / y

2.5 * 0.25 - 16 / 4

0.625 - 4

-3.375

โดัย x และ y เป�น Integer

c และ d เป�น Real

Page 25: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 25

ตั้�วอย�างล�าดั�บความส่�าค�ญ่ขีองตั้�วดั�าเน+นการ (ตั้�อ)

ตั้�วอย�าง 2-(-5) * (x mod y – x div (y+1))

-(-5) * (16 mod 4 – 16 div (4+1))

-(-5) * (16 mod 4 – 16 div (5))

5 * (16 mod 4 – 16 div 5)

5 * (0 – 3)

5 * (– 3)

-15

ส่มมตั้+ว�า ม�การก�าหนดัค�าตั้�วแปรแตั้�ละตั้�วม�การก�าหนดัค�าดั�งน�(x := 16;

y := 4;

c := 2.5;

d := 0.25;

โดัย x และ y เป�น Integer

c และ d เป�น Real

Page 26: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 26

ตั้�วอย�างล�าดั�บความส่�าค�ญ่ขีองตั้�วดั�าเน+นการ (ตั้�อ)

ตั้�วอย�าง 3c / d + y mod x <> 2*c – d

2.5 / 0.25 + 4 mod 16 <> 2*2.5 – 0.25

10 + 4 <> 5 – 0.25

14 <> 4.75

True

ส่มมตั้+ว�า ม�การก�าหนดัค�าตั้�วแปรแตั้�ละตั้�วม�การก�าหนดัค�าดั�งน�(x := 16;

y := 4;

c := 2.5;

d := 0.25;

โดัย x,y เป�น Integer

c,d เป�น Real

Page 27: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 27

ค�าส่��งร�บและแส่ดังผู้ลWrite และ WritelnRead และ Readlnการใช่!งาน 4 ค�าส่��งขี!างตั้!น ตั้!องม�การ

เพื่+�มค�าส่��งuses WinCrt; ไว!ในส่�วน Header

ค�าส่��ง uses ค%อค�าส่��งในการขีอใช่! Unit หร%อ Libraly ขีองภาษาปาส่คาล (กล6�มขีองค�าส่��งตั้�าง ๆ)

Page 28: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 28

Write & WritelnWrite(Output, Values); { แส่ดังผู้ลปกตั้+ }Writeln(Output, Values); { แส่ดังผู้ลแล!วขี)(น

บรรท�ดัใหม� }Output ปลายทางท��ตั้!องการแส่ดังผู้ล เช่�น หน!าจำอ ไฟล3

หร%อ เคร%�องพื่+มพื่3 ถู!าไม�ระบ6จำะถู�กตั้�(งค�าให!เป�นหน!าจำอ

Values ขี!อความหร%อตั้�วแปร ถู!าม�ขี!อม�ลมากกว�า 1ตั้!อง

ค��นดั!วยเคร%�องหมาย , (Comma) และขี!อความตั้!องอย��ใน

เคร%�องหมาย ' ' (single quote)

Page 29: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 29

ตั้�วอย�างการใช่! Write & Writelnprogram My_Program;

uses WinCrt;var It : integer; Rl : real; Ch : char; St : string[30]; Bl : boolean;begin

It := -27;Rl := 821.459;

Ch := 'F';St := 'Pascal Language';

Bl := true;

หมายเหตั้6 :ประกาศหลายตั้�วแปรส่ามารถูใช่!ค�าว�า var เพื่�ยงคร�(งเดั�ยวไดั!

Page 30: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 30

ตั้�วอย�างการใช่! Write & Writeln (ตั้�อ)

ผู้ลขีองค�าส่��ง Write ท��หน!าจำอภาพื่(ม�บรรท�ดัเดั�ยว)ขีองโปรแกรมน�(ค%อ

WRITEInteger:-27Real: 8.2145900000E+02Char:FString:Pascal LanguageBoolean:TRUE

Write('WRITE');

write('Integer:', It);

Write('Real:', Rl);

Write('Char:', Ch);

write('String:', St);

Write('Boolean:', Bl);

END.

หมายเหตั้6 : อย�าล%มว�าค�าส่��งในภาษาปาส่คาล เป�น แบบ case insensitive หมายความว�าจำะเขี�ยนค�าส่��งเป�นอ�กษรตั้�วพื่+มพื่3เล4กหร%อใหญ่�ก4ไดั!

Page 31: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 31

ตั้�วอย�างการใช่! Write & Writeln (ตั้�อ) จำากตั้�วอย�างขี!างตั้!น

ถู!าเปล��ยนเป�นใช่!ค�าส่��ง Writeln ดั�งน�(

Writeln('WRITELN');

Writeln ('Integer:', It);

Writeln ('Real:', Rl);

Writeln('Char:', Ch);

Writeln('String:', St);

Writeln('Boolean:', Bl);

end.

ผู้ลท��แส่ดังบนหน!าจำอภาพื่จำะม� 5 บรรท�ดัดั�งน�(

WRITELN

Integer:-27

Real: 8.2145900000E+02

Char:F

String:Pascal Language

Boolean:TRUE

Page 32: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 32

ร�ปแบบการแส่ดังผู้ลขีองจำ�านวนทศน+ยม

Write(Values:a:b);

Values จำ�านวนทศน+ยมa จำ�านวนตั้�าแหน�งท�(งหมดัท��จำองไว!ส่�าหร�บการแส่ดังผู้ล

โดัยจำะแส่ดังตั้�วเลขีแบบช่+ดัขีวา ** หากขี!อม�ลท��ตั้!องการแส่ดังม�มากกว�าตั้�าแหน�งท��จำองไว!

ส่ามารถูน�าขี!อม�ลแส่ดังผู้ลเก+นตั้�าแหน�งท��จำองไดั! **

b จำ�านวนตั้�าแหน�งหล�งจำ6ดัทศน+ยม

Page 33: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 33

ตั้�วอย�างร�ปแบบการแส่ดังผู้ลขีองจำ�านวนทศน+ยม

ตั้�วอย�างขีองค�าส่��ง Writeln

Writeln('Real1:', Rl);

Writeln('Real2:', Rl:10);

Writeln('Real3:', Rl:10:3);

Writeln('Real4:', Rl:10:2);

Writeln('Real5:', Rl:1:2);

การแส่ดังผู้ลท��หน!าจำอภาพื่

Real1:^8.2145900000E+02

Real2:^8.215E+02

Real3:^^^821.459

Real4:^^^^821.46

Real5:821.46

หมายเหตั้6 ^ แทนช่�องว�าง 1 ตั้�าแหน�ง

** เป�นร�ปแบบท��ตั้!องการให!ตั้�วเลขีท��แส่ดังผู้ลม�ทศน+ยม 2 ตั้�าแหน�ง โดัยส่�วนท��ระบ6จำ�านวนทศน+ยมเป�นส่�วน : ท��เป�นล�าดั�บท�� 2 จำ)งตั้!องใส่�ขี!อม�ลให!ถู�กตั้!องร�ปแบบและตั้รงตั้�าแหน�งท��ตั้!องการก�าหนดั จำ)งก�าหนดัส่�วน : ล�าดั�บท�� 1 ม�ค�าเป�น 1 **

Page 34: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 34

Read & Readln

Read(Input, Variables); { ร�บขี!อม�ลท�(งหมดั }

Readln(Input, Variables); { ร�บขี!อม�ลพื่อดั� }

Input ตั้!นทางท��ตั้!องการร�บขี!อม�ลเขี!ามา เช่�น แป>นพื่+มพื่3 หร%อ ไฟล3 ถู!าไม�ระบ6จำะถู�กตั้�(งค�าให!เป�น แป>นพื่+มพื่3

Variables ตั้�วแปรส่�าหร�บเก4บขี!อม�ลท��ร�บเขี!ามา

Page 35: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 35

ตั้�วอย�างการใช่! Readprogram My_Program;uses WinCrt;var num1, num2 : integer;begin

write('Input number 1: '); read(num1);write('Input number 2: ');read(num2);writeln('number 1 = ', num1);writeln('number 2 = ', num2);

end.

ผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม : ตั้�วอย�าง 1

Input number 1: 25 Input number 2: -59 number 1 = 25number 2 = -59

หมายเหตั้6 แทนความหมายว�าผู้�!ใช่!กดัป6?ม Enter บนค�ย3บอร3ดัเม%�อผู้�!ใช่!ใส่�ขี!อม�ลเส่ร4จำ

Page 36: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 36

ตั้�วอย�างการใช่! Read (ตั้�อ)ผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม : ตั้�วอย�าง 2

Input number 1: 25 -59 Input number 2: number 1 = 25

number 2 = -59

หมายเหตั้6 : ถู!าใช่!ค�าส่��ง Read ในการร�บขี!อม�ล ถู!าม�การร�บขี!อม�ลเก+น

ขี!อม�ลตั้�วท��เก+นจำะถู�กเก4บไว! เม%�อม�การร�บขี!อม�ลคร�(งตั้�อไป ขี!อม�ลตั้�วท�เก+นจำะถู�กน�ามาใช่!ท�นท�

Page 37: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 37

ตั้�วอย�างการใช่! Readlnbegin

write('Input number 1: ');readln(num1);write('Input number 2: ');readln(num2);writeln('number 1 = ', num1);writeln('number 2 = ', num2);

end.

ผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม : ตั้�วอย�าง 1

Input number 1: 25 Input number 2: -59 number 1 = 25number 2 = -59

ตั้�วอย�าง 2

Input number 1: 25 -48 Input number 2: -59 16 number 1 = 25number 2 = -59

หมายเหตั้6 : กรณ�ใช่!ค�าส่��ง Readln ถู!าม�การร�บขี!อม�ลเก+น ขี!อม�ลตั้�วท��เก+นจำะถู�กก�าจำ�ดัท�นท�

Page 38: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 38

ตั้�วอย�าง 1จำงเขี�ยนโปรแกรมในการหาพื่%(นท��ขีองร�ปส่ามเหล��ยมใดั ๆโดัยก�าหนดัให!ร�บค�าฐานและส่�งจำากผู้�!ใช่!

ว+เคราะห3 ผู้ลล�พื่ธ์3ค%อ

พื่%(นท�� ใช่!ตั้�วแปรเป�น Area (ประเภท Real)

ขี!อม�ลเขี!าค%อ ฐาน และ ส่�ง ใช่!ตั้�วแปรเป�น Base

และ Height ตั้ามล�าดั�บ (ประเภท

Real)การประมวลผู้ลค%อ พื่%(นท�� = ½ * ฐาน *

ส่�ง

Read Base

Read Height

Display Area

Area = 1/2*Base*Height

Start

Stop

Page 39: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 39

ตั้�วอย�าง 1 (ตั้�อ)program Triangle;uses WinCrt;var Base, Height, Area : real;begin write('Input Base : ');

readln(Base); write('Input Height : ');

readln(Height); Area := 0.5 * Base * Height; writeln('Area = ', Area:1:2);end. ตั้�วอย�างผู้ลล�พื่ธ์3การท�างาน

ขีองโปรแกรมน�(Input Base: 7.26 Input Height: 2.68 Area = 9.73

หมายเหตั้6 : พื่%(นท��จำากการค�านวณเท�าก�บ 9.7284

Page 40: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 40

ตั้�วอย�าง 2ให!ร�บค�า x และ y ท��เป�นจำ�านวนเตั้4มจำากผู้�!ใช่! แล!วหาค�า z จำากส่มการz = x2 - 2y (ก�าหนดัให!แส่ดังผู้ลค�า z โดัยม�ทศน+ยม 4

ตั้�าแหน�ง) 7xy3

program Equation;uses WinCrt;var x, y : integer;var z : real;begin write('Input x : ');

readln(x); write('Input y : ');

readln(y); z := (x * x - 2*y) /

(7*x*y*y*y); writeln('z = ', z:1:4);end.

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างาน

ขีองโปรแกรมน�(Input x : 7 Input y : -5 z = -0.0096

Page 41: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 41

ตั้�วอย�าง 3จำงเขี�ยนโปรแกรมในการร�บรายการ

อาหาร, จำ�านวน และราคา แล!วแส่ดังผู้ลส่+�งท��ร�บมาท�(งหมดั

รวมท�(งราคารวม ก�าหนดัให!ขี!อม�ล ตั้�วแปร

ประเภทขี!อม�ลรายการอาหาร Food

Stringจำ�านวน Amount

Integerราคา Price Realราคารวม Total Real

Page 42: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 42

ตั้�วอย�าง 3 (ตั้�อ)program Restaurant;uses WinCrt;var Food : string[30];var Amount : integer;var Price, Total : real;begin Write('Input Food: '); Readln(Food); Write('Input Price: '); Readln(Price);

Write('Input Amount: '); Readln(Amount); Total := Price * Amount; Writeln('Food:', Food); Writeln('Price: ', Price:1:2); Writeln('Amount: ', Amount); Writeln('Total: ', Total:1:2);end.

Input Food: Coke Input Price: 13.25

Input Amount: 7 Food: CokePrice: 13.25Amount: 7Total: 92.75

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรมน�(

Page 43: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 43

แบบฝึAกห�ดั 1: เขี�ยนโปรแกรมภาษาปาส่คาลจำากผู้�งงานตั้�อไปน�(

Stop

Print Y

Read T

Start

A=2.3 , B=0.04578, C=-0.0000088

Y = A + B*T +C*T2

T = T + 273

1

Page 44: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 44

แบบฝึAกห�ดั 2: เขี�ยนโปรแกรมภาษาปาส่คาลจำากผู้�งงานตั้�อไปน�(

Read X

Read Y

Display Z

Z =(X+Y)2

Start

Stop

2

Page 45: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 45

แบบฝึAกห�ดั 33 . จำงเขี�ยนโปรแกรมภาษาปาส่คาลร�บค�าเลขีจำ�านวนเตั้4ม x

และ y เพื่%�อค�านวณและแส่ดังค�า z เม%�อ z = 4x3 +1.5y+10 ก�าหนดัให!แส่ดังผู้ลค�า z โดัยม�ทศน+ยม 4 ตั้�าแหน�ง

4. จำงเขี�ยนโปรแกรมภาษาปาส่คาล ในการค�านวณหาพื่%(นท��ขีองร�ปส่��เหล��ยมในส่�วนท��แรเงาดั�งในร�ป ซ)�งผู้�!ใช่!จำะป>อนร�ศม�ขีองวงกลมเขี!ามา โดัยแส่ดังผู้ลล�พื่ธ์3ออกทางจำอภาพื่ (ให!น�กศ)กษาก�าหนดั ตั้�วแปรและช่น+ดัขีองตั้�วแปรให!เหมาะส่ม)

Page 46: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 46

บทท�� 4 (ส่�วนท�� 2)

การเขี�ยนโปรแกรมภาษาปาส่คาลโดัยใช่!ค�าส่��งควบค6ม

(Control Statements) แบบเล%อก (Selection) : if-

else, caseแบบท�าซ�(า (Loop) : while ,

repeat, for

Page 47: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 47

if-then (ตั้�อ)

if condition then begin {compound st.}

statement1; statement2;

statement3; …end;

conditionNo Yes

statement(s)

if condition then statement;

condition เง%�อนไขีในการตั้�ดัส่+นใจำว�าตั้!องการให!ท�างานหร%อไม� ถู!าเง%�อนไขีเป�นจำร+ง (True) จำ)งจำะท�างาน

statement ค�าส่��งท��ตั้!องการให!ท�างานในกรณ�ท��เง%�อนไขีเป�นจำร+งถู!า statement ม�หลายค�าส่��ง ตั้!องอย��ภายใน begin และ end;

Page 48: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 48

ตั้�วอย�าง 11จำงเขี�ยนโปรแกรมในการร�บตั้�วอ�กษร 1 ตั้�ว จำากผู้�!ใช่!

ถู!าตั้�วอ�กษรท��ร�บมาเป�น C ให!แส่ดังขี!อความว�า Cat

A =‘C’No Yes

Read A

START

STOP

write 'Cat'

program Cat;uses WinCrt;var A : char;begin write('Input character : '); readln(A); if A = ‘C' then

writeln('Cat');end.Input Character: C

Cat-

Page 49: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 49

ตั้�วอย�าง 11. (ตั้�อ)

program Cat;uses WinCrt;var A : char;begin write('Input character : '); readln(A); if A = ‘C' then writeln(‘I love cat.');end.

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม

Input Character: c

-

Input Character: C

I love cat.-

Input Character: G

-

Page 50: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 50

ตั้�วอย�าง 12

program Cat2;uses WinCrt;var A : char;begin write('Input character: '); readln(A);

if A = ‘C' then begin

writeln('Cat');writeln('I love

cat.');end;

end.

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม

Input Character: c

_

Input Character: C

CatI love cat.Input Character: G

ส่�งเกตั้ว�าม�หลายค�าส่��งตั้!องใส่� begin end;

จำงเขี�ยนโปรแกรมในการร�บตั้�วอ�กษร 1 ตั้�ว จำากผู้�!ใช่!ถู!าตั้�วอ�กษรท��ร�บมาเป�น C ให!แส่ดังขี!อความว�า Cat และ I love cat. โดัยแส่ดังขี!อความละ 1บรรท�ดั

Page 51: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 51

ตั้�วอย�าง 13.

program Cat3;uses WinCrt;var A : char;begin write('Input character: '); readln(A);

if (A = 'c') or (A = 'C') then begin

writeln('Cat');writeln('I love cat.');

end;end.

Input Character: c

CatI love cat.

Input Character: C

CatI love cat.Input Character: G

-

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม

จำงเขี�ยนโปรแกรมในการร�บตั้�วอ�กษร 1 ตั้�ว จำากผู้�!ใช่!ถู!าตั้�วอ�กษรท��ร�บมาเป�น c หร%อ C ให!แส่ดัง

ขี!อความว�า Cat และ I love cat โดัยแส่ดังขี!อความละ 1บรรท�ดั

Page 52: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 52

if-then-elsecondition

No Yes

statementA(s)statementB(s)

if condition then begin {A}

statement1;statement2;…

endelse Begin {B}

statement1;statement2;…

end;

if condition then statementelse statement;

ม�หลายค�าส่��งตั้!องอย��ภายใน begin และ end;ระว�ง! ก�อนหน!า else ไม�ตั้!องใส่�เคร%�องหมาย ;

Page 53: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 53

ตั้�วอย�าง 14 (ตั้�อ)

program Cat4;uses WinCrt;var A : char;begin write('Input character: ');readln(A); if (A = 'c') or (A = 'C') then begin

writeln('Cat');writeln('I love cat.');

endelse

writeln('Pet');end.

Input Character: c

CatI love cat.

Input Character: C

CatI love cat.

Input Character: G

Pet

ส่�งเกตั้ว�าก�อนหน!า else ไม�ตั้!องใส่�เคร%�องหมาย ;

จำงเขี�ยนโปรแกรมในการร�บตั้�วอ�กษร 1 ตั้�ว จำากผู้�!ใช่!ถู!าตั้�วอ�กษรท��ร�บมาเป�น c หร%อ C ให!แส่ดังขี!อความว�า Cat และ I love cat โดัยแส่ดังขี!อความละ 1บรรท�ดั แตั้�ถู!าตั้�วอ�กษรท��ร�บมาไม�ใช่� c หร%อ C ให!แส่ดังขี!อความว�า Pet

Page 54: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 54

ตั้�วอย�าง 2จำงเขี�ยนโปรแกรมในการร�บค�าตั้�วแปรส่องตั้�วจำากผู้�!ใช่!แล!วแส่ดังค�าตั้�วแปรท��ม�ค�ามากส่6ดัว+เคราะห3 ผู้ลล�พื่ธ์3 ค%อ ตั้�วแปรท��ม�ค�ามากส่6ดั ก�าหนดัให!เป�น Max

ขี!อม�ลเขี!า ค%อ ตั้�วแปรส่องตั้�ว ก�าหนดัให!เป�น A และ B

การประมวลผู้ลค%อการเปร�ยบเท�ยบค�าตั้�วแปรหาค�ามากส่6ดั

Page 55: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 55

ตั้�วอย�าง 2 (ตั้�อ)program Maximum;uses WinCrt;var A, B, Max : real;begin write('Input A: ');readln(A);write('Input B: ');readln(B);if A>B then Max := Aelse Max := B;writeln('Max = ', Max:1:5);end.

A > BNo Yes

Read A,B

START

STOP

write Max

Max = AMax = B

ส่�งเกตั้ว�าก�อนหน!า else

ไม�ตั้!องใส่�เคร%�องหมาย ;

ตั้�วอย�างผู้ลล�พื่ธ์3Input A: -2 Input B: 6 Max =

6.00000

Page 56: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 56

if-then-else แบบ if_ซ!อน_ifif condition1 then

begin statement1;

statement2; endelse if condition2 then

statement3 else if condition3 then begin

statement4; statement5; end

else statement6;

กรณ�ท�� statement ม�หลายค�าส่��ง ตั้!องอย��ภายใตั้! begin และ end;และท6ก ๆ ค�าส่��งในน�(นตั้!องลงท!ายดั!วยเคร%�องหมาย ;

statement2

condition1no

yes

condition2yes

no

nocondition3

yes

statement6statement4

statement3

statement1

statement5

Page 57: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 57

ตั้�วอย�าง 3จำงเขี�ยนโปรแกรมในการร�บค�าอาย6ขีองผู้�!ป?วยแล!วให!แส่ดังขี!อความบอกถู)งจำ�านวนยาท��ตั้!องร�บประทาน ดั�งน�(อาย6มากกว�า 15 ป# ร�บประทานยา 2 เม4ดัอาย6มากกว�า 5 ป# ถู)ง 15 ป# ร�บประทานยา 1เม4ดัอาย6แรกเก+ดัถู)ง 5 ป# ห!ามร�บประทาน

READ Age

DISPLAY “Eat 2 tablets”

Age > 15

no

yes

Age > 5

DISPLAY “Eat 1 tablets”

yes

DISPLAY “Do not eat”

no

START

STOP

Page 58: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 58

ตั้�วอย�าง 3 (ตั้�อ) program Drug;uses WinCrt;var Age : integer;

begin write('Input Age: '); readln(Age); if Age > 15 then writeln('2 Tablets') else if Age > 5 then writeln('1 Tablet') else writeln('0 Tablet');end.ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม Input Age: 5

0 Tablet

Input Age: 8

1 Tablet

READ Age

DISPLAY “Eat 2 tablets”

Age > 15

no

yes

Age > 5

DISPLAY “Eat 1 tablets”

yes

DISPLAY “Do not eat”

no

START

STOP

Page 59: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 59

ตั้�วอย�าง 4จำงเขี�ยนโปรแกรมในการร�บเพื่ศและอาย6แล!วแส่ดัง

ขี!อความ ดั�งน�(เพื่ศช่าย อาย6 -115 ให!แส่ดังขี!อความว�า Boyเพื่ศช่าย อาย6 > 15 ให!แส่ดังขี!อความว�า Manเพื่ศหญ่+ง อาย6 -115 ให!แส่ดังขี!อความว�า Girlเพื่ศหญ่+ง อาย6 > 15 ให!แส่ดังขี!อความว�า Womanก�าหนดัการใส่�ขี!อม�ลเพื่ศจำากผู้�!ใช่!โดัย M แทน เพื่ศช่าย และ W

แทน เพื่ศหญ่+ง

M

W

1 - 15

> 15

1 - 15

> 15

Man

Boy

Girl

Woman

Page 60: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 60

ตั้�วอย�าง 4 (ตั้�อ)program Gender_and_Age;uses WinCrt;var Gender : char;var Age : integer;begin write('Input Gender : ');readln(Gender); write('Input Age : '); readln(Age); if Gender = 'M' then begin if Age > 15 then writeln('Man') else if (0<Age) and (Age<15) then writeln('Boy'); end else if Gender = 'W' then begin if Age > 15 then writeln('Woman') else if (0<Age) and (Age<15) then

writeln('Girl'); end;end.

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรมInput Gender : W Input Age : 6 Girl

Page 61: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 61

case --> condition เป�น operator ม�ค�า discretecase condition of

constant1 : statement;constant2 : statement;constant3 : statement;

… else statement;end; {end of case}

การท�างาน• ถู!า condition ม�ค�าตั้รงก�บ constant

ตั้�วใดั statement น�(นก4จำะท�างาน• ในกรณ�ท��ไม�ตั้รงก�บ constant ใดั ๆ เลย • statement หล�ง else จำะถู�กท�างาน โดัย

else จำะม�หร%อไม�ม�ก4ไดั!

condition ตั้�วแปรหร%อ operator ช่น+ดัไม�ตั้�อเน%�อง เช่�น ช่น+ดั

integer หร%อ charconstant ค�าคงท��ขีองตั้�วแปรหร%อขีอง operator ท��เก4บค�า

หร%อเป�นค�าไม�ตั้�อเน%�อง เช่�นช่น+ดั integer หร%อ char ถู!าเป�น char ค�า

คงท��ตั้!องอย��ภายในเคร%�องหมาย' 'statement ค�าส่��ง

Page 62: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 62

ตั้�วอย�าง 5จำงเขี�ยนโปรแกรมในการร�บเลขีจำ�านวนเตั้4ม 1

จำ�านวน จำากผู้�!ใช่!แล!วแส่ดังขี!อความดั�งน�(ถู!าร�บเลขี 1 แส่ดังขี!อความ Hello

Pascalถู!าร�บเลขี 4 หร%อ 5 แส่ดังขี!อความHello

Javaถู!าร�บเลขี 9 แส่ดังขี!อความHello C++ถู!าร�บเลขีนอกเหน%อจำากขี!างตั้!น จำะแส่ดังขี!อความ Hello World

Read T

T4,5

1

9

other

START

STOP

DISPLAY “Hello Pascal” DISPLAY

“Hello Java”DISPLAY

“Hello C++”

DISPLAY “Hello World”

Page 63: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 63

ตั้�วอย�าง 5 (ตั้�อ)program Hello;uses WinCrt;

var T : integer;begin write('Input Integer: '); readln(Number); case T of 1 : Writeln('Hello

Pascal'); 4,5 : Writeln('Hello

Java'); 9 : Writeln('Hello C++'); else Writeln('Hello World'); end;end.

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรมInput Integer: 1 Hello Pascal-

Read T

T4,5

1

9

other

START

STOP

DISPLAY “Hello Pascal”DISPLAY

“Hello Java”DISPLAY

“Hello C++”

DISPLAY “Hello World”

Page 64: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 64

จำงว+เคราะห3และเขี�ยนโปรแกรมเพื่%�อร�บค�าขี!อม�ลจำ6ดัหน)�งในระนาบ 2 ม+ตั้+เป�นเลขีจำ�านวนจำร+ง 2 จำ�านวน แล!วตั้รวจำส่อบว�าขี!อม�ลจำ6ดัท��ร�บเขี!ามาเป�นขี!อม�ลจำ6ดัท��อย��บน Quadrant ใดั หร%ออย��บนแกน x หร%อแกน y หร%อเป�นจำ6ดั origin บนระนาบ 2 ม+ตั้+

ตั้�วอย�างผู้ลการท�างาน

Page 65: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 65

ค�าส่��งควบค6มแบบท�าซ�(าwhile จำ�านวนรอบไม�แน�นอน

ตั้รวจำส่อบก�อนท�างานอาจำไม�ไดั!ท�างานเลยแม!แตั้�คร�(งเดั�ยว ใน

กรณ�เง%�อนไขีเป�นเท4จำแตั้�แรกrepeat จำ�านวนรอบไม�แน�นอน

ท�างานก�อนตั้รวจำส่อบไดั!ท�างานแน�นอนอย�างน!อย 1 คร�(ง แม!

เง%�อนไขีเป�นจำร+งแตั้�แรกfor จำ�านวนรอบแน�นอน

Page 66: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 66

while --> while condition is true do loopwhile condition

do beginstatement1;statement2;statement3;…

end;

condition

statement(s)

yes

no

condition เง%�อนไขีในการตั้�ดัส่+นใจำว�าตั้!องการให!ท�างานหร%อไม�

ถู!าเง%�อนไขีเป�นจำร+ง (True) จำ)งจำะท�างาน

statement ค�าส่��งท��ตั้!องการให!ท�างานในกรณ�ท��เง%�อนไขีเป�นจำร+ง

Page 67: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 67

ตั้�วอย�าง 6ให!ร�บค�าเลขีจำ�านวนจำร+งค�าหน)�งแล!วท�าการแส่ดัง

ผู้ลขีองค�าก�าล�งส่องขีองเลขีน�(น โดัยก�าหนดัให!ถู!าม�การป>อนค�าลบให!ส่+(นส่6ดัการร�บขี!อม�ล (ใช่!โครงส่ร!าง While Do )

ว+เคราะห3โจำทย3ผู้ลล�พื่ธ์3 ค%อ ค�าก�าล�งส่องขีองตั้�วเลขีท��ร�บเขี!ามาขี!อม�ลเขี!า ค%อ เลขีจำ�านวนจำร+งจำ�านวนรอบไม�แน�นอนป>อนค�าขี!อม�ลเป�นเลขีลบ เง%�อนไขีในการหย6ดั

วนท�าซ�(า

Page 68: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 68

ตั้�วอย�าง 6 (ตั้�อ)program Power;uses WinCrt;var num : real;begin write('Input num: '); readln(num); while num >= 0 do

begin writeln(‘Power of num=',num*num:10:5);

write('Input num: '); readln(num); end;end.

READ num

num 0

no

yes

READ num

START

STOP

Print num2

Page 69: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 69

program Power;uses WinCrt;var num : real;begin write('Input num : '); readln(num); while num >= 0 do begin writeln(‘Power of num=‘,num*num:10:5);

write('Input num : '); readln(num); end;end.

ตั้�วอย�าง 6 (ตั้�อ)

Input num : 2.2 Power of num=^^^4.84000Input num: 5.5 Power of num=^^30.25000Input num: -1

-

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม

Input num : 0.005Power of num=^^^0.00003Input num : -9 -

Page 70: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 70

repeat repeat loop until condition is true

repeatstatement1;statement2;statement3;…

until condition;condition

statement(s)

yes

no

condition ถู!าเง%�อนไขีเป�นจำร+ง (True) จำะหย6ดัการท�างาน

ถู!าเง%�อนไขีเป�นเท4จำ (False) จำะท�า loop รอบตั้�อไป

statement ค�าส่��งท��ตั้!องการให!ท�างานในกรณ�ท��เง%�อนไขีเป�นเท4จำ

ค�าส่��งหร%อช่6ดัค�าส่��งท��อย��ใน loop

Page 71: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 71

repeat (ตั้�อ)var Cont : char;…repeat

statement(s);…write('Continue (Y/N): ');readln(Cont); {‘Y’เพื่%�อท�าloop}

until Cont <> 'Y';

Cont <>'Y'

statement(s)

yesno

READ Cont

ขี!อส่�งเกตั้ การเขี�ยนดั!วยภาษาปาส่คาลกล6�มขีองค�าส่��งตั้�าง ๆ (statements) ท��อย��ภายใตั้!เง%�อนไขีการวนท�าซ�(า ไม�ตั้!องม�เคร%�องหมาย begin ... end; ซ)�งตั้�างก�บการเขี�ยนโปรแกรมแบบ pretest เช่�น while

Page 72: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 72

ตั้�วอย�าง 7ให!ร�บตั้�วเลขีจำากผู้�!ใช่! แล!วให!แส่ดังผู้ลว�า “Negative” หร%อ “Positive or Zero” เม%�อแส่ดังผู้ลแล!วให!ผู้�!ใช่!เล%อกไดั!ว�าตั้!องการท�างานตั้�อไปหร%อไม� ว+เคราะห3โจำทย3Input : ตั้�วเลขี 1 จำ�านวน (Num)Output : ขี!อความ “Negative” หร%อ “Positive or Zero”การท�างานจำะวนท�าซ�(า โดัยให!ผู้�!ใช่!ย%นย�นหล�งจำากท�างานเส่ร4จำแล!ว

READ Stop

Stop = 'Y'no

READ Num

Num >= 0

DISPLAY “Positive or Zero”

DISPLAY “Negative”

yes no

START

STOPyes

Page 73: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 73

ตั้�วอย�าง 7 (ตั้�อ)program Number;

uses WinCrt;

var Num : integer;

var Stop : char;

begin

repeat

write('Input Number: ');

readln(Num);

if Num>= 0 then writeln(‘Positive or Zero')

else writeln(‘Negative');

write('Stop program (Y/N): ');

readln(Stop);

until Stop = 'Y';

end.

Input Number: -7 NegativeStop program (Y/N): N

Input Number: 12 Positive or ZeroStop program (Y/N): Y

ตั้�วอย�างผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม

Page 74: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 74

ตั้�วอย�าง 8ให!แส่ดังค�าตั้�วเลขีท��เป�นเลขีค��ตั้�(งแตั้� 2 ถู)ง

10024, 246810 10024... , (Do

While และ Repeat Until )ว+เคราะห3โจำทย3Input : -Output :- แส่ดังตั้�วเลขีค�� 2 4 6 …, 10,024 โดัยไม�

ทราบจำ�านวนรอบ- แส่ดังตั้�วเลขี 10024, เป�นตั้�วเลขี

ส่6ดัท!ายซ)�งเป�น เง%�อนไขีในการหย6ดั

Page 75: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 75

ตั้�วอย�าง 8 แบบ WHILE (ตั้�อ)

DISPLAY N

N <= 10,024no

yes

N = 2

N = N + 2

START

STOP

program whileExample;uses WinCrt;var N : integer;begin N := 2; while N <= 10024 do begin write(N); N := N + 2; end;end.

Page 76: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 76

ตั้�วอย�าง 8 แบบ REPEAT (ตั้�อ)

DISPLAY N

N > 10,024no

yes

N = 2

N = N + 2

START

STOP

program repeatExample;uses WinCrt;var N : integer;begin N := 2; repeat write(N,' '); N := N + 2; until N > 10024;end.

Page 77: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 77

For : ค�าส่��งวนท�าซ�(ากรณ�ท��ร�!จำ�านวนรอบแน�นอน

var variable : integer/char;…for variable := start to/downto stop do begin

statement(s);end;

statement(s)

For i =start to stop

Page 78: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 78

ตั้�วอย�าง 9

Program Power;uses WinCrt;var i : integer; { *1* }var x : integer;begin; for i := 1 to 10 do {*2*} begin write('Input number x : '); readln(x); writeln(‘x^2 =', x*x); end;end.

display x2

read x

For i = 1 to 10

START

STOP

ให!ร�บค�าเลขีจำ�านวนเตั้4ม x แล!วแส่ดังค�า x2 โดัยให!โปรแกรมวนท�าซ�(า 10 คร�(ง

ผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม

Input Number x : 12 x^2 = 144Input Number x : 6 x^2 = 36…Input Number : 10 x^2 = 100

10 คร�(ง

Page 79: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 79

ตั้�วอย�าง 9 (ตั้�อ)ถู!าเปล��ยนบรรท�ดั { *1* } และ{ *2* }

เป�นดั�งน�( จำะให!ผู้ลล�พื่ธ์3เหม%อนเดั+ม{ *1* } var i : integer;

{ *2* } for i := 15 downto 6 do begin

{ *1* } var i : char;

{ *2* } for i := 'A' to 'J' do begin

{ *1* } var i : char;

{ *2* } for i :='z' downto 'q' do begin

1

2

3

Program Power;uses WinCrt;var i : integer;

{ *1* }var x : integer;begin; for i := 1 to 10 do

{*2*} begin write('Input number

x: '); readln(x); writeln(‘x^2 =',

x*x); end;end.

Page 80: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 80

ตั้�วอย�าง 10ให!ร�บคะแนนขีองน�กศ)กษา N คนแล!วท�าการ

ค�านวณและแส่ดังผู้ลคะแนนเฉล��ย โดัยท��ค�า N ให!ร�บจำากผู้�!ใช่!(ก�าหนดัให!ใช่!ค�าส่��ง For)ว+เคราะห3โจำทย3- Input :N ,คะแนนน.ศ . N คน ร�บขี!อม�ล

คะแนน จำ�านวน N รอบแน�นอน- Output: Sum ไว!ส่�าหร�บเก4บค�าผู้ลรวม - (ตั้�วแปรท��เก4บค�าผู้ลรวมก�อนใช่!งานตั้!องให!ค�า

เร+�มตั้!นเป�น 0 )

Page 81: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 81

program N_Students;uses WinCrt;var score, i, N, sum :

integer;var mean : real;begin sum := 0; write('Input number : '); readln(N); for i := 1 to N do begin write('Input Score : '); readln(score); writeln('Score = ',

score); sum := sum + score;

end;mean : = sum / N;write('Mean = ', mean:10:5);

end.

ตั้�วอย�าง 10 (ตั้�อ)

DISPLAY mean

READ score

READ N

mean = sum / N

sum = 0

sum = sum + score

For i = 1 to N

START

STOP

Page 82: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 82

program N_Students;uses WinCrt;var score, i, N, sum :

integer;var mean : real;begin write('Input number : '); readln(N); for i := 1 to N do begin write('Input Score : '); readln(score); writeln('Score = ',

score); sum := sum + score;

end;mean : = sum / N;write('Mean = ', mean:10:5);

end.

ตั้�วอย�าง 10 (ตั้�อ)Input number : 4 Input Score : 12 Score = 12Input Score : 8 Score = 8Input Score : 15 Score = 15Input Score : 10 Score = 10Mean =^^11.25000_

N= 4 คร�(ง

ผู้ลล�พื่ธ์3การท�างานขีองโปรแกรม

Page 83: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 83

ตั้�วอย�าง 11ให!แส่ดังผู้ลเลขีค��ตั้�(งแตั้� 97 ถู)ง 3ว+เคราะห3โจำทย3ล�าดั�บขีองตั้�วเลขีจำะแส่ดังผู้ลดั�งน�( 97,95,93, ...,7 ,5 ,3 ,

ตั้�วแรกค%อ 97 ตั้�วส่6ดัท!ายค%อ 3 ลดัลงท�ละ 2

จำะใช่! while loop (pre-test) ในการแก!ป<ญ่หา

(ใน flowchart เป�น โครงส่ร!าง for loop (pre-test))

Page 84: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 84

ตั้�วอย�าง 11 (ตั้�อ)

DISPLAY k

For k=97 downto 3 step -2

START

STOP

program Odd_Reverse;uses WinCrt;var k : integer;begin k:= 97; while k>=3 do begin write (k , ‘,‘);

k:= k- 2; end;end.

Page 85: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 85

แบบฝึ�กหั�ด 5. จำงเขี�ยนโปรแกรมเพื่%�อร�บเลขีจำ�านวนเตั้4ม 3 จำ�านวน

แล!วแส่ดังผู้ลล�พื่ธ์3ออกทางจำอภาพื่ เป�นค�าตั้�วเลขีท��ม�ค�ามากท��ส่6ดั และค�าตั้�วเลขีท��ม�ค�าน!อยท��ส่6ดัเช่�น ร�บเลขี 3 จำ�านวน ค%อ 10 8 และ 15

ผู้ลล�พื่ธ์3 ตั้�วเลขีท��มากท��ส่6ดั ค%อ 15 ตั้�วเลขีท��น!อยท��ส่6ดั ค%อ 8

6. จำงเขี�ยนโปรแกรมส่�าหร�บในการบวกเลขีจำ�านวนเตั้4ม N จำ�านวน แล!วแส่ดังผู้ลล�พื่ธ์3ออกทางจำอภาพื่ เช่�น ระบ6เลขีจำ�านวนเตั้4มท��จำะป>อน 5 ค�า โดัยค�าท��ป>อนไดั!แก� 1 5 10 6 และ 9 ผู้ลล�พื่ธ์3ท��ไดั!ค%อ 31

Page 86: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 86

บทท�� 4 (ส่�วนท�� 3)

การเขี�ยนโปรแกรมภาษาปาส่คาล

โดัยใช่!โครงส่ร!างหล�กท�(ง 3 โครงส่ร!าง ในโปรแกรม

Page 87: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 87

หมายเหตั้6 Newline ค%อให!ขี)(นบรรท�ดัใหม�

START

STOP

Print ‘*’

For col = 1 to k

For row = 1 to k

Print Newline

แบบฝึAกห�ดั 11 .ผู้ลล�พื่ธ์3ขีองโปรแกรมน�(ค%อ

อะไรk = 4

ทดัลองเขี�ยนโปรแกรมภาษา

ปาส่คาลจำากผู้�งงาน�(

Page 88: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 88

แบบฝึAกห�ดั 22 .จำงเขี�ยนโปรแกรมเพื่%�อร�บขี!อม�ลอ�กขีระ 1 ตั้�ว

และขี!อม�ลเลขีจำ�านวนเตั้4ม 2 จำ�านวน ( a และ b) แล!วค�านวณและแส่ดังค�า c ตั้ามเง%�อนไขีน�( ถู!าร�บขี!อม�ลอ�กขีระเป�น ‘P’ ให!ค�านวณ c = a +b และถู!าเป�น ‘M’ ให!ค�านวณ c = a*b โดัยให!โปรแกรมวนท�าซ�(าไดั! 5 รอบ

Page 89: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 89

ตั้�วอย�าง 12 จำงเขี�ยนโปรแกรมเพื่%�อค�านวณเง+นค�าจำ!างขีอง

พื่น�กงานจำ�านวน 20 คน โดัยก�าหนดัให!ร�บขี!อม�ลจำ�านวนช่��วโมงท�างานและขี!อม�ลประเภทขีองพื่น�กงาน ซ)�งพื่น�กงานแตั้�ละประเภทไดั!ร�บค�าจำ!างตั้�อช่��วโมงตั้�างก�น ดั�งน�(ประเภทขีองพื่น�กงาน ค�าจำ!างบาท / ช่��วโมง1 552 753 90

4 100

Page 90: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 90

ตั้�วอย�าง 1 2 (ตั้�อ)

Print P

Read H,T

T=1NoYes

No

P=H*55 T=2Yes

P=H*75 T=3

P=H*90

No

Yes

P=H*100

START

STOP

เม%�อค+ดัค�าจำ!างขีองล�กจำ!างคนเดั�ยว

Page 91: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 91

ตั้�วอย�าง 12 (ตั้�อ) แบบท�� 1 กรณ�ค+ดัค�าจำ!างขีองล�กจำ!าง 20

คน

ส่ามารถูใช่! repeat until หร%อ loop for ก4ไดั!

ทราบจำ�านวนรอบแน�นอน

Print P

Read H,T

T=1NoYes

No

P=H*55 T=2Yes

P=H*75 T=3

P=H*90

No

Yes

P=H*100

START

STOP

i<=20

i=1

i=i+1

Yes

No

เม%�อค+ดัค�าจำ!างขีองล�กจำ!างหลายคน

Page 92: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 92

ตั้�วอย�าง 1 2 (ตั้�อ)

Print P

Read H,T

T2

13

P=H*55P=H*75

4

P=H*90P=H*100

START

STOP

For i = 1 to N

Read N

แบบท�� 2 กรณ�ค+ดัค�าจำ!างขีองล�กจำ!าง N คน

เม%�อค+ดัค�าจำ!างขีองล�กจำ!างหลายคน

Page 93: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 93

ตั้�วอย�าง 1 (ตั้�อ)program Employee;uses WinCrt;var N, T , i : integer;

H,P : real;begin write('Input number of Employees : ');

readln(N); for i := 1 to N do begin write('Input Hours : '); readln(H);

write('Input Type : '); readln(T); case T of 1 : P := H*55; 2 : P := H*75; 3 : P := H*90;

4 : P := H*100; end; write('P = ' ,P:1:2); end;end.

โปรแกรมน�(เป�นค�าส่��งภาษาปาส่คาลขีองตั้�วอย�างท�� 12 ในกรณ�ใดั ?

Page 94: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 94

ตั้�วอย�าง 13จำงเขี�ยนโปรแกรมเพื่%�อร�บขี!อม�ลเลขีจำ�านวนเตั้4ม 10 จำ�านวน โดัยให!แส่ดังเฉพื่าะค�าขีองเลขีตั้��าส่6ดับนจำอภาพื่

min > numNo Yes

min = num

START

i = 1, min = 999

i=i+1

No

Yes

i <= 10

Read numPrint min

STOP หมายเหตั้6 โปรแกรมจำะท�างานถู�กตั้!องเม%�อขี!อม�ลเลขีจำ�านวนเตั้4มท6กตั้�วตั้��ากว�า 999

แบบท�� 1

Page 95: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 95

ตั้�วอย�าง 13 ( ตั้�อ )

จำงเขี�ยนโปรแกรมเพื่%�อร�บขี!อม�ลเลขีจำ�านวนเตั้4ม 10 จำ�านวน โดัยให!แส่ดังเฉพื่าะค�าขีองเลขีตั้��าส่6ดับนจำอภาพื่

min > numNo Yes

min = num

START

i = 1, min = num

i=i+1

No

Yes

i < 10

Read num

Read numPrint min

STOP

แบบท�� 2

ตั้�างจำากแบบท�� 1 ตั้รงไหน ?

Page 96: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 96

ตั้�วอย�าง 14จำงเขี�ยนโปรแกรมส่�าหร�บร�บคะแนนขีอง

น�กศ)กษาท��เร�ยนว+ช่า 204101 กล6�มหน)�ง แล!วหาค�าคะแนนขีองน�กศ)กษาท��ม�ค�ามากท��ส่6ดั (Max) และค�าคะแนนเฉล��ย(Average)ขีองน�กศ)กษากล6�มน�( แส่ดังผู้ลล�พื่ธ์3ออกทางจำอภาพื่

ก�าหนดัให! - ขี!อม�ลคะแนนขีองน�กเร�ยนท6กคนตั้!องไม�ตั้��ากว�า 0

- โปรแกรมหย6ดัร�บขี!อม�ลคะแนน เม%�อร�บค�าคะแนนเป�น -1

Page 97: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 97

ตั้�วอย�าง 14 (ตั้�อ) กรณ�ไม�ทราบจำ�านวนพื่น�กงาน

Print avg,max

Read score

max < scoreNo Yes

max = score

START

STOP

i=1, sum=0, max= -1

sum = sum + score

i=i+1

No

Yes

score != -1

Read score

i>1No Yes

avg=sum/(i-1)

Page 98: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 98

program Employee;uses WinCrt;var i : integer; score,max,sum,avg : real;begin i := 1; sum := 0; max := -1; write('Input score : '); readln(score); while score <> -1 do begin sum := sum + score; if (max < score ) then max := score; i := i + 1; write('Input score : '); readln(score); end; if (i > 1 ) then begin avg := sum/(i-1); writeln('maximum = ' , max:1:2); writeln('average = ' , avg:1:2); end;end.

Page 99: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 99หมายเหตั้6 Newline ค%อให!ขี)(นบรรท�ดัใหม�

row=1 or row = kNoYes

col>1 and col < kYes No

START

STOP

Print ‘*’

Print ‘ ’ Print ‘*’

For col = 1 to k

For row = 1 to k

Read k

Print Newline

แบบฝึAกห�ดั3โปรแกรมน�(ท�างานอะไร

Page 100: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 100

แบบฝึ�กหั�ด 4 .จำงเขี�ยนโปรแกรมส่�าหร�บวาดัร�ปส่ามเหล��ยม

ความส่�ง k ดั!วยส่�ญ่ล�กษณ3 *ตั้�วอย�างเช่�น ถู!าร�บ k เป�น 3 ร�ปท��แส่ดังค%อ 5. จำงเขี�ยนโปรแกรมเพื่%�อร�บขี!อม�ลเลขีจำ�านวนเตั้4ม

N จำ�านวน และน�บว�าม�เลขีจำ�านวนเตั้4มค�� และจำ�านวนเตั้4มค��อย�างละก��จำ�านวน (กรณ�เลขีศ�นย3ไม�น�บ)

***

* **

Page 101: บทที่ 4 (ส่วนที่ 1)

204101 Introduction to Computer 101

ทดสอบก�นหัน�อยจำงเขี�ยนโปรแกรมเพื่%�อร�บขี!อม�ลเลขีจำ�านวนเตั้4ม M และพื่+มพื่3ช่6ดัขี!อม�ล

เลขีจำ�านวนเตั้4ม โดัยเร+�มตั้!นท�� 1 ท�(งหมดั M จำ�านวน ดั�งตั้�วอย�างถู!า M = 5 ผู้ลล�พื่ธ์3ท��แส่ดังบนจำอภาพื่ค%อ 163125

ถู!า M = 12 ผู้ลล�พื่ธ์3ท��แส่ดังบนจำอภาพื่ค%อ 16312518724

9 301136

หมายเหตั้6 ในช่6ดัขี!อม�ล ค�าตั้�วเลขีขีองตั้�าแหน�งค��และเลขีค��จำะม�ความส่�มพื่�นธ์3ไม�เหม%อนก�น