180 likes | 435 Views
บทที่ 8 ตัวดำเนินการ และ นิพจน์. OUTLINE 1. ตัวดำเนินการ (Operator) 2. นิพจน์(Expression) 3. คำสั่งเชิงเดียว และ คำสั่งเชิงกลุ่ม. ตัวดำเนินการ (Operator). ตัวดำเนินการ (Operator) เทียบได้กับคำสั่งให้ดำเนินกรรมวิธีกับข้อมูล ทางคณิตศาสตร์ บูลีน การเปรียบเทียบ และ อื่นๆ.
E N D
บทที่ 8ตัวดำเนินการ และ นิพจน์ • OUTLINE 1. ตัวดำเนินการ (Operator) 2. นิพจน์(Expression) 3. คำสั่งเชิงเดียว และ คำสั่งเชิงกลุ่ม
ตัวดำเนินการ (Operator) • ตัวดำเนินการ (Operator) เทียบได้กับคำสั่งให้ดำเนินกรรมวิธีกับข้อมูล ทางคณิตศาสตร์ บูลีน การเปรียบเทียบ และ อื่นๆ
ตัวดำเนินการ (Operator) • ตัวดำเนินการ (Operator) ในปาสคาลแบ่งออกเป็น • ตัวดำเนินการทางคณิตศาสตร์ (arithmetic operator) • มี +, -, *, /, div, mod • ตัวดำเนินการเปรียบเทียบ (relational operators) • ตัวดำเนินการบูลีน (boolean operator) • ตัวดำเนินการสตริง (string operator)
โปรแกรมการใช้ Operator PROGRAM operator1; USES Wincrt; begin writeln('+: ',25+50); writeln('/: ',25/50:5:2); writeln('MOD: ',50 mod 3); writeln('DIV: ',50 div 3); end.
ตัวดำเนินการเปรียบเทียบตัวดำเนินการเปรียบเทียบ • ตัวดำเนินการเปรียบเทียบ มีค่าที่ต้องการเปรียบเทียบอยู่ระหว่างตัวดำเนินการ ผลลัพธ์เป็นบูลีน คือ false , true • >, <, >=, <=, <>, in • ‘E’ in [‘a’,’e’,’i’,’o’,’u’] • 2 < 3
ตัวดำเนินการบูลีน • ตัวดำเนินการบูลีน มี 4 ตัว and, or, xor, not มีการทำงานเหมือนระดับบิต ต่างตรงที่ สองข้างของตัวดำเนินการเป็นบูลีน เช่น (a>13) and (b < 5) ผลลัพธ์มี จริงกับเท็จ
ตัวดำเนินการบูลีน Aมีค่า B มีค่า A and B A or B A xor B not A T T T T F F T F F T T F F T F T T T F F F F F T
ลำดับการทำงานของตัวดำเนินการลำดับการทำงานของตัวดำเนินการ 1. @, not 2. *, /, div, mod, and 3. +, -, or, xor 4. =, <> , < , > , <= , >= , IN ถ้ามีฟังก์ชัน หรือ วงเล็บให้ทำก่อน
นิพจน์คณิตศาสตร์ • นิพจน์ อาจเป็น ตัวแปร ค่าคงที่ หรือ ฟังก์ชัน • นิพจน์ทางคณิตศาสตร์ คือ นิพจน์ที่ใช้ตัวดำเนินการทางคณิตศาสตร์ +, - , * , / , div , mod โดยข้อมูลต้องเป็นตัวเลข • หากมีการผสมระหว่างเลขจำนวนเต็ม และ จำนวนจริง เลขจำนวนเต็มจะถูกเปลี่ยนเป็นจำนวนจริงโดยอัตโนมัต • หากต้องการเก็บค่าของเลขจำนวนจริงไว้ในเลขจำนวนเต็มต้องใช้ ฟังก์ชัน trunc(R) round( R) ก่อน
การสร้างนิพจน์ทางคณิตศาสตร์การสร้างนิพจน์ทางคณิตศาสตร์ • 5(num+total ) ===> 5 * (num + total) • (x2 +y2)2 ====> SQR(x*x + y*y) หรือ SQR(SQR(x)+SQR (y)) • -b+ b2- 4ac ===> (-b+SQRT(SQR(b)-4*a*c))/ (2*a) 2a
ตัวอย่างนิพจน์ทางคณิตศาสตร์ตัวอย่างนิพจน์ทางคณิตศาสตร์ PROGRAM expression1; USES Wincrt; VAR a,b,tem1,tem2 :integer; c,d,tem3 : real; BEGIN clrscr; a:= 27; b:=4; c:= 56.2; d:= 7.0;
ตัวอย่างนิพจน์ทางคณิตศาสตร์ตัวอย่างนิพจน์ทางคณิตศาสตร์ Writeln ('int,int: ', A+B:15, a-b:15, a*b:15, a/b:25); Writeln('int,rel: ',a+c:18, a-c:18, a*c:18, a/c:18); Writeln('rel,int: ',d+b:18,d-b:18,d*b:18,d/c:18); Writeln('rel,rel: ',c+d:18,c-d:18,c*d:18,c/d:18); tem1 := a div b; tem2 := a mod b; tem3 := a div b; Writeln (tem1:20,tem2:20,tem3:20) End.
นิพจน์แบบบูลีน มีค่า จริงกับเท็จเท่านั้น เพราะฉะนั้นต้องประกาศตัวแปลงแบบบูลีน ตัวอย่าง: VAR count,total :integer; length,height : real; done : boolean: Count < total (count=total) and(length>height) and done (count mod total =0) or(count <= 100) นิพจน์แบบบูลีน
PROGRAM operator1; USES Wincrt; VAR a, b : real; flag : boolean; begin write('Enter a:'); Readln (a); write('Enter b:'); Readln (b); flag := a < b; Writeln(flag); end. นิพจน์แบบบูลีน
คำสั่งเชิงเดียว และ คำสั่งเชิงกลุ่ม • คำสั่งเชิงเดียว คือ ประโยคคำสั่ง 1 คำสั่ง • คำสั่งเชิงกลุ่ม (compound statement) เมื่อต้องการให้มีคำสั่งหลายประโยคหรือ เป็นกลุ่มคำสั่งให้จัดอยู่ในคอมเปานด์ มีรูปแบบดังนี้ begin statement 1; : statement N end