งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก"— ใบสำเนางานนำเสนอ:

1 Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก
ต้องการคำนวณ …+n (สมมุติให้ n เป็นเลขคี่) ตัวอย่างที่ผิด ตัวอย่างที่ถูก sum := 0; i ;= 1; while i<n do sum := sum + i; i := i + 2; writeln(‘sum = ‘,sum); sum := 0 i ;= 1; while i<n do begin sum := sum + i; i := i + 2; end; Writeln(‘sum = ‘,sum); ตามหลังคำว่า Do นักศึกษาชอบลืมใส่ begin กับ end ในเวลาที่เราต้องการให้ มีหลายๆคำสั่งใน statement หลัง do

2 Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก
ตัวอย่างที่ผิด ตัวอย่างที่ถูก If (nbook <= 100)and(nbook>0) then totalprice := nbook*price; writeln(‘No discount’); else if nbook <= 500 then totalprice := 0.95*nbook*price; writeln(‘Discount 5%’); else if nbook > 500 then totalprice := 0.9*nbook*price; writeln(‘Discount 10%’); If (nbook <= 100)and(nbook>0) then begin totalprice := nbook*price; writeln(‘No discount’); end else if nbook <= 500 then totalprice := 0.95*nbook*price; writeln(‘Discount 5%’); else if nbook > 500 then totalprice := 0.9*nbook*price; writeln(‘Discount 10%’); end; ตามหลัง then ถ้ามีหลายคำสั่ง จะต้อง ขึ้นต้นด้วย begin และจบด้วย end

3 Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก
ตัวอย่างที่ผิด ตัวอย่างที่ถูก If (nbook <= 100)and(nbook>0) then totalprice := nbook*price; else if nbook <= 500 then totalprice := 0.95*nbook*price; else if nbook > 500 then totalprice := 0.9*nbook*price; If (nbook <= 100)and(nbook>0) then totalprice := nbook*price else if nbook <= 500 then totalprice := 0.95*nbook*price else if nbook > 500 then totalprice := 0.9*nbook*price; สอง Program นี้คล้ายกันมาก แต่ Program ทางซ้ายมือมีเครื่องหมาย ; ในที่ที่ไม่ควรจะมี เพราะว่าคำสั่ง if – then – else if –then –else if – then ถือเป็นคำสั่งเดียวจะต้อง ใช้ ; ปิดตอนท้ายสุดเท่านั้น

4 Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก
ตัวอย่างที่ผิด ตัวอย่างที่ถูก If 0< n <= 100 then totalprice := n*price else if 100 < n <= 500 then totalprice := 0.95*n*price else if n > 500 then totalprice := 0.9*n*price; If (0<n) and (n <= 100) then totalprice := n*price; else if (100 < n) and (n <= 500) then totalprice := 0.95*n*price; else if n > 500 then totalprice := 0.9*n*price; นักศึกษาชอบเขียนเงื่อนไขเป็น 100 < n <= 500 ซึ่งในทางคณิศาสตร์ไม่ผิด แต่ในทางภาษา Pascal ผิดไวยากรณ์ ที่ถูกต้องแก้เป็น (100<n) and (n<=500) ตัวอย่างที่ผิด ตัวอย่างที่ถูก For 1 to n Do …. For i := 1 to n Do …. บางทีลืมใส่ตัวแปรในคำสั่ง for

5 Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก
ตัวอย่างที่ผิด Case n of n <= : totalprice := price*n; n > 100 and n <= 500 : totalprice := price*n*0.95; n > : totalprice := price*n*0.9; End; ตัวอย่างที่ถูก Case n of : totalprice := price*n; : totalprice := price*n*0.95; Else if n>500 then totalprice := price*n*0.9; End; เราไม่สามารถนำการเปรียบเทียบ เช่น n<=100 มาเป็นเงื่อนไข ของคำสั่ง case ได้ เงื่อนไขของคำสั่ง case จะต้องเป็น มีค่าเป็นเลข integer, นิพจน์ทางเลขที่ มีผลลัพธ์เป็นจำนวนเต็ม หรือ character

6 Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก
ตัวอย่างที่ถูก ตัวอย่างที่ไม่ดี If totalprice < 500 then totalprice := 500 else totalprice := totalprice; If totalprice < 500 then totalprice := 500; คำสั่งนี้ไม่ผิด แต่ไม่มีประโยชน์ If (n<= 100)and(n>0) then begin totalprice := n*price; writeln(‘Total price=‘,totalprice); end else if n <= 500 then totalprice := 0.95*n*price; If (n<= 100)and(n>0) then begin totalprice := n*price; end else if n <= 500 then totalprice := 0.95*n*price; End writeln(‘Total price=‘,totalprice); สองคำสั่งนี้เหมือนกันทุกประการสามารถยุบ เป็นคำสั่งเดียวได้โดยวางไว้นอกคำสั่ง if

7 Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก
ตัวอย่างที่ถูก ตัวอย่างที่ผิด Program MyFirstprogram; Var x,y:real; age:integer; Begin readln(‘x’, ‘y’); readln(‘age’); writeln(‘age = ’,age); End. Program MyFirstprogram; Var x,y:real; age:integer; Begin readln(x, y); readln(age); writeln(‘age = ‘,age); End. บางคนสับสนระหว่าง ชื่อตัวแปรกับ ข้อความ ชื่อตัวแปรไม่ต้องมี ‘ ’ ปิดล้อมไว้ ถ้ามี ‘ ’ ปิดล้อมไว้ จะหมายถึง ข้อความ คำสั่ง readln ใช้กับชื่อตัวแปร

8 Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก
ตัวอย่างที่ผิด Repeat write(‘Please enter number : ’); readln(n); …. write(‘Do you want to play again?); readln(ch); Until ch = ‘N’ or ‘n’; ตัวอย่างที่ถูก Repeat write(‘Please enter number : ’); readln(n); …. write(‘Do you want to play again?); readln(ch); Until ch = ‘N’ or ch = ‘n’; ระวัง เรื่องเงื่อนไขสองเงื่อนไข


ดาวน์โหลด ppt Top 10 ไวยากรณ์ที่นักศึกษาใช้ผิดบ่อยมาก

งานนำเสนอที่คล้ายกัน


Ads by Google