1 / 15

บทที่ 8 ตัวดำเนินการ และ นิพจน์

บทที่ 8 ตัวดำเนินการ และ นิพจน์. OUTLINE 1. ตัวดำเนินการ (Operator) 2. นิพจน์(Expression) 3. คำสั่งเชิงเดียว และ คำสั่งเชิงกลุ่ม. ตัวดำเนินการ (Operator). ตัวดำเนินการ (Operator) เทียบได้กับคำสั่งให้ดำเนินกรรมวิธีกับข้อมูล ทางคณิตศาสตร์ บูลีน การเปรียบเทียบ และ อื่นๆ.

Download Presentation

บทที่ 8 ตัวดำเนินการ และ นิพจน์

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. บทที่ 8ตัวดำเนินการ และ นิพจน์ • OUTLINE 1. ตัวดำเนินการ (Operator) 2. นิพจน์(Expression) 3. คำสั่งเชิงเดียว และ คำสั่งเชิงกลุ่ม

  2. ตัวดำเนินการ (Operator) • ตัวดำเนินการ (Operator) เทียบได้กับคำสั่งให้ดำเนินกรรมวิธีกับข้อมูล ทางคณิตศาสตร์ บูลีน การเปรียบเทียบ และ อื่นๆ

  3. ตัวดำเนินการ (Operator) • ตัวดำเนินการ (Operator) ในปาสคาลแบ่งออกเป็น • ตัวดำเนินการทางคณิตศาสตร์ (arithmetic operator) • มี +, -, *, /, div, mod • ตัวดำเนินการเปรียบเทียบ (relational operators) • ตัวดำเนินการบูลีน (boolean operator) • ตัวดำเนินการสตริง (string operator)

  4. โปรแกรมการใช้ 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.

  5. ตัวดำเนินการเปรียบเทียบตัวดำเนินการเปรียบเทียบ • ตัวดำเนินการเปรียบเทียบ มีค่าที่ต้องการเปรียบเทียบอยู่ระหว่างตัวดำเนินการ ผลลัพธ์เป็นบูลีน คือ false , true • >, <, >=, <=, <>, in • ‘E’ in [‘a’,’e’,’i’,’o’,’u’] • 2 < 3

  6. ตัวดำเนินการบูลีน • ตัวดำเนินการบูลีน มี 4 ตัว and, or, xor, not มีการทำงานเหมือนระดับบิต ต่างตรงที่ สองข้างของตัวดำเนินการเป็นบูลีน เช่น (a>13) and (b < 5) ผลลัพธ์มี จริงกับเท็จ

  7. ตัวดำเนินการบูลีน 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

  8. ลำดับการทำงานของตัวดำเนินการลำดับการทำงานของตัวดำเนินการ 1. @, not 2. *, /, div, mod, and 3. +, -, or, xor 4. =, <> , < , > , <= , >= , IN ถ้ามีฟังก์ชัน หรือ วงเล็บให้ทำก่อน

  9. นิพจน์คณิตศาสตร์ • นิพจน์ อาจเป็น ตัวแปร ค่าคงที่ หรือ ฟังก์ชัน • นิพจน์ทางคณิตศาสตร์ คือ นิพจน์ที่ใช้ตัวดำเนินการทางคณิตศาสตร์ +, - , * , / , div , mod โดยข้อมูลต้องเป็นตัวเลข • หากมีการผสมระหว่างเลขจำนวนเต็ม และ จำนวนจริง เลขจำนวนเต็มจะถูกเปลี่ยนเป็นจำนวนจริงโดยอัตโนมัต • หากต้องการเก็บค่าของเลขจำนวนจริงไว้ในเลขจำนวนเต็มต้องใช้ ฟังก์ชัน trunc(R) round( R) ก่อน

  10. การสร้างนิพจน์ทางคณิตศาสตร์การสร้างนิพจน์ทางคณิตศาสตร์ • 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

  11. ตัวอย่างนิพจน์ทางคณิตศาสตร์ตัวอย่างนิพจน์ทางคณิตศาสตร์ 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;

  12. ตัวอย่างนิพจน์ทางคณิตศาสตร์ตัวอย่างนิพจน์ทางคณิตศาสตร์ 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.

  13. นิพจน์แบบบูลีน มีค่า จริงกับเท็จเท่านั้น เพราะฉะนั้นต้องประกาศตัวแปลงแบบบูลีน ตัวอย่าง: 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) นิพจน์แบบบูลีน

  14. 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. นิพจน์แบบบูลีน

  15. คำสั่งเชิงเดียว และ คำสั่งเชิงกลุ่ม • คำสั่งเชิงเดียว คือ ประโยคคำสั่ง 1 คำสั่ง • คำสั่งเชิงกลุ่ม (compound statement) เมื่อต้องการให้มีคำสั่งหลายประโยคหรือ เป็นกลุ่มคำสั่งให้จัดอยู่ในคอมเปานด์ มีรูปแบบดังนี้ begin statement 1; : statement N end

More Related