Skip to content
Home » [Update] วิธีคิดแบบนักวิทยาการคอมพิวเตอร์/Conditionals and recursion | zero conditional คือ – NATAVIGUIDES

[Update] วิธีคิดแบบนักวิทยาการคอมพิวเตอร์/Conditionals and recursion | zero conditional คือ – NATAVIGUIDES

zero conditional คือ: คุณกำลังดูกระทู้

บทที่ 4 Conditionals and recursion

[

แก้ไข

]

4.1 The modulus operator

[

แก้ไข

]

เป็นสัญลักษณ์ซึ่งทำหน้าที่แทนการคำนวณ ซึ่งทำงานร่วมกับ integer (จำนวนเต็ม) และ integer expressions ใน Python สัญลักษณ์ตัวนี้คือ เครื่องหมาย % โครงสร้างของประโยคก็เหมือนกับ operator ตัวอื่นๆ เช่น

>>> quotient = 7 / 3

>>> print quotient

2

>>> remainder = 7 % 3

>>> print remainder

1

7 เมื่อหารด้วย 3 จะเท่ากับ 2 และเหลือเศษ 1

modulus operator ให้ผลลัพธ์ที่มีประโยชน์อย่างน่าประหลาดใจ ตัวอย่างเช่นคุณสามารถที่ตะตรวจสอบตัวเลขว่าตัวเลขหนึ่งๆหารลงตัวด้วยตัวเลขอื่นหรือไม่ – ถ้า x % y แล้วเท่ากับ 0 ดังนั้น x หารด้วย y ลงตัว

4.2 Boolean expressions

[

แก้ไข

]

เป็น expression ที่ประกอบด้วย true หรือ false ในการเขียน expression นี้ ทำโดยการใช้ operator == สำหรับเปรีบยเทียบค่า2ค่า และสร้างค่าทางตรรกะ

>>> 5 == 5

True

>>> 5 == 6

False

ใน statement แรก ค่าทั้งสองมีค่าเท่ากัน ดังนั้นผลลัพธ์ที่ออกมาคือ True ใน statement ที่สอง 5 ไม่เท่ากับ 6 ดังนั้นเราจะได้ค่า False

เครื่องหมาย == เป็นหนึ่งในเครื่องหมายที่ใช้ในการเปรียบเทียบ และนอกจากนี้ได้แก่:

x != y # x ไม่เท่ากับ y

x > y # x มากกว่า y

x < y # x น้อยกว่า y

x >= y # x มากกว่าหรือเท่ากับ y

x <= y # x น้อยกว่าหรือเท่ากับ y

สัญลักษณ์ใน Python นั้นจะแตกต่างกัยสัญลักษณ์ทางคณิตศาสตร์ ข้อผิดพลาดที่เกิดขึ้นโดยทั่วไปคือการใช้ = แทน == ให้จำไว้ว่า = เป็นเครื่องหมายที่สำหรับใช้ในการกำหนดค่าให้กับตัวแปร และ == เป็นเครื่องหมายที่ใช้สำหรับการเปรียบเทียบค่า

4.3 Logical operators

[

แก้ไข

]

มีด้วยกันอยู่ 3 ตัวคือ and or and not ความหมายของ operator เหล่านี้มีความหมายคล้ายคลึงกับความหมายของตัวมันเองในภาษาอังกฤษ ตัวอย่างเช่น x > 0 and x < 10 เป็นจริงในกรณีเดียวเมื่อ x มากกว่า 0 และน้อยกว่า 10

n%2 == 0 or n%3 == 0 เป็นจริงถ้า ตัวใดตัวหนึ่งของเงื่อนไขเป็นจริง นั่นก็คือ เมื่อตัวเลขใดๆหารด้วย 2 หรือ 3 ได้ลงตัว

สุดท้าย not ปฏิเสธ boolean expression ดังนั้น not(x > y) เป็น True ถ้า (x > y) เป็น False นั่นคือ ถ้า x น้อยกว่าหรือเท่ากับ y

พูดอย่างเคร่งครัด ค่าที่ใช้ร่วมกับตัว operator ของ logical operator ควรจะเป็น boolean expressions( expression ที่ประกอบด้วย True และ False ) แต่ใน Python ไม่ได้ยึดติดกับตรงนี้ ตัวเลขใดก็ได้ที่ไม่ใช่ 0 จะถูกแปลความให้เป็น “true”

>>> x = 5

>>> x and 1

1

>>> y = 0

>>> y and 1

0

โดยทั่วไป รูปแบบชนิดนี้ไม่ได้เป็นรูปแบบที่ดี ถ้าคุณต้องการที่จะเปรียบเทียบค่ากับ 0 คุณควรที่จะทำให้เห็นอย่างชัดเจน

4.4 Conditional execution

[

แก้ไข

]

เพื่อทีจะเขียนโปรแกรมให้ดีนั้น เราต้องการความสามารถที่จะตรวจเช็คเงื่อนไขและเปลี่ยนแปลงการทำงานของโปรแกรมอย่างสอดคล้องอยู่เสมอ Conditional statements ให้ความสามารถนี้กับเรา รูปแบบอย่างง่ายที่สุดของ if statement:

if x > 0:

print “x is positive”

boolean expression หลังจาก if statement เรียกว่า condition(เงื่อนไข) ถ้าหากว่ามันเป็น True หลังจากนั้น statement ที่ย่อหน้าก็จะถูกดำเนินการ แต่ถ้าไม่ก็จะไม่มีอะไรเกิดขึ้น

อย่าง compound statement อื่นๆ if statement สร้างขึ้นด้วย header และ block ของstatement:

HEADER:

FIRST STATEMENT

LAST STATEMENT

header จะเริ่มบนบรรทัดใหม่และจบด้วยเครื่องหมาย (:) ย่อหน้าที่ตามมาจะเรียกว่า block และ statement ที่อยู่ใน compound statement จะเรียกว่า body ของstatement

statement ใน body ของ if statement นั้นสามารถที่จะมีเท่าไหร่ก็ได้ไม่จำกัด แต่ต้องมีอย่างน้อย 1 statement ในบางครั้ง body ที่ไม่มี statementนั้นก็มีประโยชน์ กล่าวคือ เสมือนกับการจองพื้นที่สำหรับโค้ดที่เรายังไม่ได้เขียนลงไป ในกรณีนี้ คุณสามารถที่จะใช้ pass statement ซึ่งจะไม่ทำอะไรเลย

4.5 Alternative execution

[

แก้ไข

]

รูปแบบที่สองของ if statement คือ ทางเลือกของการดำเนินการ ที่ซึ่งมีความเป็นไปได้อยู่ 2 และตัดสินใจเลือกเงื่อนไขที่จะทำการดำเนินการ ไวยกรณ์มีลักษณะเช่นนี้:

if x%2 == 0:

print x, “is even”

else:

print x, “is odd”

ถ้าส่วนที่เหลืออย เมื่อ x ถูกหารด้วย 2 ลงตัว เราก็จะรู้ว่า x เป็นจำนวนคู่ แล้วโปรแกรมก็จะทำการแสดงข้อความของผลนั้น ถ้าเงื่อนไขไม่ถูกต้อง statement อีกชุดหนึ่งก็จะถูกดำเนินการแทนตั้งแต่ที่เงื่อนไขจะต้องเป็น True และ False แน่นอนว่าจะต้องมี 1ทางเลือกที่จะต้องถูกดำเนินการ ทางเลือกดังกล่าวนั้นถูกเรียกว่า branches เพราะว่าทางเลือกเหล่านั้นเป็นแขนงหรือทางแยกในการไหลของการดำเนินการ

เช่นด้านล่าง ถ้าคุณต้องการเช็ค parity ของตัวเลขบ่อยครั้ง คุณควรตัด code นี้ไปเป็น function:

def printParity(x):

if x%2 == 0:

print x, “is even”

else:

print x, “is odd”

สำหรับทุกๆค่าของ x printParityแสดงข้อความที่ได้เตรียมไว้ เมื่อคุณทำการเรียก คุณสามารถให้ตัวเลขใดๆก็ได้แทน argument

>>> printParity(17)

17 is odd

>>> printParity(y+1)

18 is even

4.6 Chained conditionals

[

แก้ไข

]

บางครั้งมีความเป็นไปได้ที่มากกว่า 2 และเราก็ต้องการทางแยกที่มากกว่า 2 เช่นกัน ทางเดียวที่จะแสดงการคำนวณเช่นนั้นคือ chained conditional:

if x < y:

print x, “is less than”, y

elif x > y:

print x, “is greater than”, y

else:

print x, “and” , y, “are equal”

elif คือ การย่อ ของ “else if” Again แน่นอนว่าเพียงทางเลือกเดียวเท่านั้นที่จะถูกดำเนินการ สำหรับ elif statements นั้นสามารถจะมีได้เท่าไหรก็ได้ไม่จำกัด แต่ทางเลือกสุดท้ายจะต้องเป็น else statements:

if choice == ‘A’ :

functionA()

elif choice == ‘B’ :

functionB()

elif choice == ‘C’ :

functionC()

else:

print “Invalid choice”

แต่ละเงื่อนไขจะถูกตรวจสอบในคำสั่ง ถ้าเงื่อนไขแรกเป็น false เงื่อนไขต่อไปก็จะถูกตรวจสอบ เป็นต้น ถ้าหนึ่งในเงื่อนไขนั้นๆเป็น true ทางเลือกนั้นๆก็จะถูกดำเนินการ และก็จะจบ statement แม้ว่าจะมีมากกว่า 1 เงื่อนไขที่เป็น true เงื่อนไขแรกที่เป็น true เท่านั้นที่จะถูกดำเนินการ

4.7 Nested conditionals

[

แก้ไข

]

เงื่อนไขหนึ่งๆสามารถที่จะใส่อยู่ในเงื่อนไขอื่นๆได้เช่นเดียวกัน เราสามารถที่จะเขียนตัวอย่างของการแบ่งแยกออกเป็นสามส่วนได้ตามตัวอย่างต่อไปนี้:

if x == y:

print x, “and”, y, “are equal”

else:

if x < y:

print x, “is less than”, y

else:

print x, “is greater than”, y

เงื่อนไขที่อยู่ข้างนอกประกอบด้วยสองทางเลือก ทางเลือกแรกมี statement ซึ่งเป็นผลลัพธ์ง่ายๆ ทางเลือกที่สองประกอบด้วย if statement ซึ่งมีทางเลือกอีก 2 ที่เป็นของตัวมันเอง

ถึงแม้ว่าการย่อหน้าของ statement จะทำให้เกิดเป็นโครงร่างเกิดขึ้น nested conditionals ก็ยังคงยากต่อการอ่านโดยเร็ว โดยทั่วไปมันเป็นความคิดที่ดีที่จะหลีกเลี่ยงมันเมื่อคุณทำได้

Logical operators เป็นทางหนึ่งที่จะช่วยทำให้ nested conditional statements ดูง่ายขึ้น สำหรับตัวอย่าง เราจะทำการเขียนโค๊ดต่อไปนี้ใหม่โดยใช้เพียงเงื่อนไขเดียว

if 0 < x:

x < 10:

print “x is a positive single digit”

print statement จะถูกดำเนินการเพียงกรณีเดียวถ้าเราทำให้เงื่อนไขทั้งสองผ่าน ดังนั้นเราสามรถใช้ and operator เข้ามาช่วยได้

if 0 < x and x < 10:

print “x is a positive single digit”

เงื่อนไขเหล่านี้เป็นชนิดที่ธรรมดา ดังนั้น Python จึงมีการจัดเตรียมตัวเลือกไวยกรณ์ที่เหมือนกับเครื่องหมายการคำนวณทางคณิตศาสตร:

if 0 < x < 10:

print “x is a positive single digit”

เงื่อนไขนี้เป็นความหมายเดียวกับ compound boolean expression และ nested conditional

4.8 The return statement

[

แก้ไข

]

เป็น statement ที่ยินยอมให้เรายุติการดำเนินการของ function ก่อนที่จะจบ เหตุผลหนึ่งที่ใช้มันก็คือถ้าคุณตรวจเจอเงื่อนไขที่ผิดพลาด

import math

def printLogarithm(x):

if x <= 0:

print “Positive numbers only, please”

return

result = mathlog(x)

print “The log of x is”, result

function printLogarithm มีพารามิเตอร์หรือตัวแปรที่ชื่อ x อย่างแรกที่ทำก็คือ ตรวจสอบว่าไม่ว่า x จะน้อยกว่าหรือเท่ากับ 0 กรณีนี้มันจะแสดงข้อความ error และใช้ return เพื่อออกจาก function นั้น

ให้จำไว้ว่าถ้าจะมีการใช้ฟังก์ชั่น math จะต้องมีการ import มันก่อนเสมอ

เราก็ได้กล่าวถึงกันไปแล้วสำหรับ function หนึ่ง เรียกไปยัง function อื่น และคุณก็ได้เห็นตัวอย่างเหล่านั้นไปพอสมควร เราจะไม่จะกล่าวถึงว่ามันถูกต้องสำหรับ function ที่เรียกตัวมันเอง มันอาจไม่แน่ชัดว่าทำไมมันถึงเป็นสิ่งที่ดี แต่มันกลับกลายมาเป็นสิ่งหนึ่งที่มหัศจรรย์และน่าสนใจที่โปรแกรมสามารถทำได้ ตัวอย่าง ดูจาก function ต่อไปนี้:

def countdown(n):

if n == 0:

print “Blastoff!”

else:

print n

countdown(n-1)

countdown คาดหวังกับตัวแปร n ว่าจะต้องเป็นจำนวนเต็มบวก ถ้า n เป็น 0 ผลลัพธ์ที่จะออกมาคือ “Blastoff!” มิฉะนั้น ก็จะแสดงค่า n ออกมาและทำการเรียก function ที่ชื่อ countdown ตัวมันเอง โดยมี (n-1)เป็นตัวแปรที่ใช้รับค่าเข้าฟังก์ชั่น

จะเกิดอะไรขึ้นหากเราเรียก function เช่นนี้:

>>> countdown(3)

การดำเนินการของ countdown จะเริ่มด้วย n=3 และเมื่อ n ไม่เท่ากับ 0 มันจะออกผลลัพธ์เป็นค่าเท่ากับ 3, และเรียกฟังก์ชั่นตัวมันเอง

การดำเนินการของ countdown เริ่มด้วย n=2 และเมื่อ n ไม่เท่ากับ 0 มันจะออกผลลัพธ์เป็นค่าเท่ากับ 2, และเรียกฟังก์ชั่นตัวมันเอง

การดำเนินการของ countdown เริ่มด้วย n=1 และเมื่อ n ไม่เท่ากับ 0 มันจะออกผลลัพธ์เป็นค่าเท่ากับ 1, และเรียกฟังก์ชั่นตัวมันเอง

การดำเนินการของ countdown เริ่มด้วย n=0 และเมื่อ n เป็น 0 มันจะออกผลลัพธ์เป็นคำว่า, “Blastoff!” จากนั้นก็จะ return

countdown ทำการคืนค่า n=1

countdown ทำการคืนค่า n=2

countdown ทำการคืนค่า n=3

ซึ่งผลทั้งหมดจะออกมาในลักษณะนี้:

3

2

1

Blastoff!

ตัวอย่างถัดมา ให้ดูเช่นเดิมที่ function newLine และ threeLines

def newLine():

print

def threeLines():

newLine():

newLine():

newLine():

แม้ว่ามันจะสามารถใช้งานได้มันก็ไม่ได้ช่วยอะไรได้มากถ้าเราต้องการผลลัพธ์เป็น 2บรรทัดใหม่ หรือจะ 106 ทางเลือกที่ดีกว่าน่าจะเป็นเช่นนี้:
def nLines(n)

if n > 0:

print

nLines(n-1)

โปรแกรมนี้เหมือนกับ countdown; ตราบเท่าที่ n มากกว่า 0 มันจะออกผลลัพธ์เป็นบรรทัดใหม่แล้วก็ทำการเรียกตัวเองซ้ำโดยผ่านตัวแปร(n-1) เพิ่มบรรทัดใหม่

กระบวนการที่ function เรียกตัวเองนั้นคือ recursion

4.10 Stack diagrams for recursive function

[

แก้ไข

]

ในหมวดที่ 3.11 เราคุ้นเคยกับ stack diagram ซึ่งอธิบายถึงสภาพของโปรแกรม ใรระหว่างที่มีการเรียกฟังก์ชั่น (function call: statement ที่ทำการดำเนินการฟังก์ชั่น ประกอบด้วยชื่อของฟังก์ชั่นและตามด้วย argument หรือตัวแปรที่ใช้รับค่าเข้าฟังก์ชั่นที่อยู่ภายในวงเล็บ)

ซึ่ง diagram ชนิดเดียวกันนี้สามารถที่จะช่วยอธิบาย recursive function
ทุกครั้งที่มีการเรียกฟังก์ชั่น Python จะทำการสร้าง frame (โครงสร้างฟังก์ชั่นขึ้นมา), ซึ่งประกอบด้วย ตัวแปรของฟังก์ชั่นนั้นและ parameters (ชื่อซึ่งใช้ในฟังก์ชั่นเพื่ออ้างถึงค่าที่ผ่านด้วย argument หรือตัวแปรที่ใช้รับค่าเข้าฟังก์ชั่น)

สำหรับ recursive function จะมี frame มากกว่า 1 บน stack ในเวลาเดียวกัน
นี่เป็นรูปภาพของ stack diagram ของฟังก์ชั่น countdown ซึ่งถูกเรียกด้วยมีค่า n=3

โดยปรกติ บนสุดของ stack จะเป็น frame สำหรับ ฟังก์ชั่น main มันจะว่างเปล่าเนื่องจากไม่ได้มีการสร้างตัวแปรใดๆใน main หรือผ่านค่า parameters ใดๆไปที่มัน

frame ของ ฟังก์ชั่น countdown มีค่า parameter n ที่ต่างกัน ด้านล่างสุดของ stack ที่ซึ่ง n=0 เรียกว่า base case (ทางแยกของเงื่อนไขใน recursive function ซึ่งจะไม่เกิดผลในการเรียกซ้ำ)
มันจะไม่ทำการเรียกซ้ำ และจะไม่มี frame เพิ่มขึ้นอีก

4.11 Infinite recursion

[

แก้ไข

]

ถ้าหากการเรียกซ้ำไม่เคยไปถึง base case มันก็จะทำการเรียกซ้ำต่ออย่างต่อเนื่อง และโปรแกรมจะไม่มีการยุติ นี่เป็นที่รู้จักกันคือ infinite recursion และโดยทั่วไปมันไม่ได้ถูกพิจารณาว่าเป็นความคิดที่ดี

นี่เป็นตัวอย่างโปรแกรมอย่างเล็กที่สุดที่เป็น infinite recursion:

def recurse():

recure():

ในทั่วไปกระบวนการของการเขียนและทดสอบโปรแกรม โปรแกรมที่เป็น infinite recursion จะไม่สามารถรันอย่างไม่สิ้นสุดได้
Python จะรายงานข้อความ error เมื่อมีการเรียกซ้ำจนถึงที่สุด:

File “<stdin>”, line 2, in recurse

(98 repetitions omitted)

File “<stdin>”, line 2, in recurse

RuntimeError: Maximum recursion depth exceeded

traceback (list ของฟังก์ชั่นที่กำลังทำการดำเนินการ จะถูกปริ้นเมื่อมี runtime error เกิดขึ้น) นี้มีขนาดใหญ่กว่าเล็กน้อยกว่าตัวอย่างที่เราได้เห็นกันในบทที่แล้ว

เมื่อ error เกิดขึ้น ก็มีถึง 100 recurse frame บน stack!

4.12 Keyboard input

[

แก้ไข

]

โปรแกรมที่เราได้เขียนจนเดี๋ยวนี้นั้นมีความหยาบอยู่เล็กน้อยคือมันไม่ได้มีการรับค่ามาจากผู้ใช้งานเลย โปรแกรมเหล่านั้นแค่ทำงานเหมือนๆเดิมอยู่ตลอดเวลา

Python จึงได้มีการจัด built-in functions ที่สามารถทำาการรับค่าจากคีย์บอร์ด อย่างง่ายที่สุดเรียกว่า raw_input เมื่อฟังก์ชั่นนี้ถูกเรียกใช้งาน โปรแกรมก็จะหยุดและรอให้ผู้ใช้งาน พิมพ์บางอย่าง

เมื่อผู้ใช้งาน กด Return หรือ ปุ่ม Enter โปรแกรมก็จะดำเนินต่อไปและ raw_input จะทำการคืนค่าที่ผู้ใช้งานป้อนเข้ามาเป็น string:

>>> input = raw_input ()

What are you waiting for?

>>> print input

What are you waiting for?

ก่อนที่จะทำการเรียก raw_input เป็นความคิดที่ดีที่จะแสดงข้อความเพื่อบอกให้ผู้ใช้งานรู้ว่าต้องป้อนข้อมูลหรือพิมอะไร ข้อความนี้เรียกว่า Prompt

เราสามารถที่จะให้ prompt นั้นทำหน้าที่แทน argument:

>>> name = raw_input (“Whatis your name? “)

Whatis your name? Arthur, King of the Brintons!

>>> print name

Arthur, King of the Brintons!

หากเราคาดหวังที่จะต้องการให้เป็น integer เราสามารถที่จะใช้ input function:

prompt = “Whatis the airspeed velocity of an unladen swallow?\n”

speed = input(prompt)

ถ้าผู้ใช้พิมพ์อักษรที่เป็นตัวเลขเข้ามา มันจะถูกแปลงให้เป็น integer และทำการกำหนดค่าให้กับ speed โชคร้าย ถ้าหากผู้ใช้งานพิมพ์ตัวอักษรที่ไม่ได้เป็นตัวเลขเข้ามา โปรแกรมก็จะล้ม:

>>> speed = input (prompt)

Whatis the airspeed velocity of an unladen swallow?

What do you mean, an African or a European swallow?

SyntaxError: invalid syntax

เพื่อที่จะหลีกเลี่ยง error ชนิดนี้ เป็นความคิดที่ดีที่จะใช้ raw_input เพื่อรับค่าที่เป็น string หลังจากนั้นก็ใช้ฟังก์ชั่นสำหรับการแปลง เพื่อแปลงค่าที่รับเป็นชนิดอื่น

modulus operator: An operator, denoted with a percent sign (%), that works on integers and yields the remainder when one number is divided by another.

boolean expression: An expression that is either true or false.

comparison operator: One of the operators that compares two values: ==, !=, >, <, >=, and <=.

logical operator: One of the operators that combines boolean expressions: and, or, and not.

conditional statement: A statement that controls the flow of execution depending on some condition.

condition: The boolean expression in a conditional statement that determines which branch is executed.

compound statement: A statement that consists of a header and a body. The header ends with a colon (:). The body is indented relative to the header.

block: A group of consecutive statements with the same indentation.

body: The block in a compound statement that follows the header.

nesting: One program structure within another, such as a conditional statement inside a branch of another conditional statement.

recursion: The process of calling the function that is currently executing.
base case: A branch of the conditional statement in a recursive function that does not result in a recursive call.

infinite recursion: A function that calls itself recursively without ever reaching the base case. Eventually, an infinite recursion causes a runtime error.

prompt: A visual cue that tells the user to input data.

[NEW] ประโยคเงื่อนไข (Conditional Sentences) | zero conditional คือ – NATAVIGUIDES

ประโยคเงื่อนไข (Conditional Sentences) เป็นลักษณะประโยคที่บอกการกระทำของกิริยาอีกลักษณะหนึ่งที่มีความสำคัญพอๆ กับเรื่อง “mood” เพราะเป็นเรื่องที่เกี่ยวข้องสัมพันธ์กัน ข้อความของประโยคในรูปแบบต่างๆ จะแสดงความหมายไม่เหมือนกัน ข้อความบางอันเป็นจริง แต่ข้อความบางอันไม่เป็นจริง เป็นแต่การสมมุติเท่านั้น

ฉะนั้น จำเป็นที่จะต้องศึกษารูปแบบของประโยคเงื่อนไขต่างๆ ให้ดี จึงจะเข้าใจข้อความได้ถูกต้องขึ้น ประโยคเงื่อนไขนั้น ความจริงก็คือ Adverb clause ชนิดหนึ่งที่แสดงเงื่อนไข (Condition) ซึ่งมีตัว Relative เช่น “if, unless, provided (that), suppose (that), on condition that” แต่ส่วนใหญ่มักจะใช้ “if’ บางตำรา จึงเรียกประโยคเงื่อนไขว่า “IF-Clause” ก็มี

ชนิดของประโยคเงื่อนไข
ประโยคเงื่อนไข (Conditional Sentences) แบ่งออกเป็น 2 ชนิด คือ
1. เงื่อนไขที่เป็นจริง หรือเงื่อนไขที่เป็นไปได้ในอนาคต (Real Conditions or Open Conditions) มีรูปแบบโครงสร้างดังนี้

Subordinate Clause            Main Clause
IF + Present Simple            Future Simple

ตัวอย่าง
If John works hard, he will pass his examination.
If the rain stops I shall go for a walk.
Unless the rain stops I shall not go for a walk.

ตำแหน่งการวาง Clause ทั้ง 2 อาจสลับกันได้ คือ เอา Main Clause ขึ้น แล้วตามด้วย Subordinate Clause (แต่อย่างไรก็ดี ถ้าขึ้นต้น ประโยคด้วย Subordinate Clause หรือ if Clause ก่อนจะเป็นการเน้นยิ่งขึ้น) เช่น
I will help him if he asks me.
I won’t help him unless he asks me.
He will do the work if (provided that/on condition that) he has the time.

ประโยคเงื่อนไขแท้จริง (Real Conditions) นอกจากจะใช้โครงสร้างประโยคดังกล่าวข้างต้น ซึ่งเป็นโครงสร้างที่ใช้มากที่สุดแล้ว ยังมีโครงสร้างอีกแบบหนึ่งซึ่งใช้มากเช่นกัน และค่อนข้างจะมีความหมายเน้น (Emphasis) กว่าโครงสร้างข้างบน คือ

Subordinate Clause                Main Clause
If + Present Simple                 Present Simple

ตัวอย่าง
If you are right, I am wrong.
If he comes, I tell you.
If you boil water, it changes into steam.

นอกจากโครงสร้างหลัก 2 แบบ ดังกล่าวแล้ว ประโยคเงื่อนไขแท้จริง อาจใช้ในแบบอื่นๆ อีก เช่น ตัวอย่าง
1. If + Present Simple\Future Perfect Tense เช่น If I get this right, I shall have answered every question correctly.
2. IF + Present Simple, Past Simple Tense เช่น If what you say is right, then what I said was wrong.
3. If + Present Simple, Imperative Mood. เช่น If you meet Henry, tell him I want to see him. It the ground is very dry, don’t forget to water those plants.
4. If + should + Bare Infinitive, Imperative or Future in Question Forms เช่น
If you should meet Henry, tell him I want to see him.
If he should come, please give him this book.
If you should be passing, do come and see us.
If the train should be late, what will you do?

หมายเหตุ โครงสร้างตามรูปแบบที่ 4 นี้ จะมีความหมายความเป็นไปได้ที่น้อยลงกว่าโครงสร้างปกติ (Remote Possibility)
5. If + Past Simple, Present Simple Tense เช่น If I said that, I apologise.
6. If + Past Simple, Past Simple Tense เช่น If I said that, I was mistaken.
7. If + Past Simple, Future Simple Tense เช่น If I made a mistake, I will try to remedy it.
8. If + Present Perfect, Future Simple Tense เช่น If I have made a mistake, I will try to remedy it.
9. IF + Present Perfect, Present Simple Tense เช่น If you have done your work, you may go to the cinema.

ข้อสังเกต
1. ใน IF-Clause จะไม่ตามด้วย Future Tense เลย แม้ว่าความหมายจะเป็นอนาคตก็ตาม เช่น
I shall go for a walk if the rain will stop. (ผิด)
I shall go for a walk if the rain stops. (ถูก)

2. “will” จะใช้ใน IF-Clause แต่มิได้แสคงความหมาย “อนาคตกาล” แต่แสดงถึง “ความเต็มใจหรือตั้งใจทำ (Willingness) เช่น
If you will sign this agreement, I will let you have the money at once.

แต่ข้อความข้างบนจะสุภาพยิ่งขึ้นถ้าใช้ “would” เช่น If you would (=would be so kind as to/would be kind enough to) sign this agreement, I will let you have the money at once.

2. เงื่อนไขที่ไม่จริงหรือเงื่อนไขสมมุติ (Unreal Conditions or Hypothetical Conditions, Suppositions) ซึ่งแบ่งออกเป็น 2 ลักษณะ คือ
2.1 เงื่อนไขที่ไม่จริง หรือเป็นไปไม่ได้ในอนาคต (Unreal Conditions in Future) มีรูปแบบดังนี้

Subordinate Clause            Main Clause
IF+Past Simple Tense        Future Simple in the Past

เช่น
If Henry were here, he would know the answer.
If I had the money, I would buy a new car.
If I were King, you should be Queen.

ข้อสังเกต
1. ใน IF-Clause ที่แสดงเงื่อนไขไม่จริงในอนาคตนี้ จะใช้ “were” กับทุกๆ ประธาน
2. รูป Future Simple in the Past นั้น ก็คือ รูปที่มาจาก Future Simple นั่นเอง แต่เปลี่ยน “will” เป็น “would” หรือเปลี่ยน “shall” เป็น “should”
3. ความหมายของทุกตัวอย่างข้างต้น จะแสดงการสมมุติทั้งสิ้น เช่น
“If Henry were here……” แสดงว่า “ผมรู้ว่าเฮนรีไม่มีโอกาสจะอยู่ที่นี่ได้เลย” หรือ “If I had the money………” หมายความว่า “ผมรู้ว่าผมไม่มีทางจะมีเงินจำนวนนั้นได้เลย’’ ตัวอย่างประโยคสุดท้ายยิ่งเน้นชัดว่า ข้อความนั้นไม่มีทางเป็นไปได้อย่างยิ่ง คือ “If I were King…………”

อนึ่ง รูปแบบเงื่อนไขไม่แท้จริง หรือเงื่อนไขเป็นไปไม่ได้ในอนาคตนี้ อาจแสดงได้อีกรูปแบบหนึ่ง ซึ่งจะมีความหมายเน้นขึ้น

IF+Past Simple Tense        Future Perfect in the Past
เช่น
If he were really interested in buying the property, he would have made an offer before now.

หมายเหตุ รูป Future Perfect in the Past นั้นคือรูป Future Perfect Tense นั่นเอง แต่เปลี่ยน “will” เป็น “would” “shall” เป็น “should” เช่นกัน

2.2 เงื่อนไขไม่จริงหรือเงื่อนไขสมมุติย้อนอดีต (Unreal Conditions in Past) มีรูปแบบดังนี้

Subordinate Clause                  Main Clause
IF + Past Perfect Tense            Future Perfect in the Past
เช่น
If John had worked hard, he would have passed the examination.
If you had asked me, I would have helped you.
If I had had the money, I would have bought a bigger house.
If the hat had suited me, I would have bought it.
I should never have done that work if you had not helped me.
If you hadn’t told me about it, I might never have gone to see it.

ข้อสังเกต
1. รูป Future Perfect in the Past ก็คือ รูปที่มาจาก Future Perfect Tense นั่นเอง แต่เปลี่ยน “will” เป็น “would” หรือ “shall” เป็น “should”

2. ความหมายของรูปเงื่อนไขไม่จริงย้อนอดีตนี้จะมีความหมายเหมือนกับโครงสร้าง “wish + Past Perfect” (ดูเรื่อง “Mood” ที่เกี่ยวกับโครงสร้าง “wish” ประกอบ) กล่าวคือ เป็นเงื่อนไขที่กล่าวถึงสิ่งที่เกิดขึ้นแล้วและได้กระทำลงไปตรงกันข้ามกับรูปประโยคเสมอ หรือเป็นเงื่อนไขที่ตรงกันข้ามกับความเป็นจริงนั่นเอง (Contrary to the Facts) เช่น
“If John had worked hard……..” (= ผู้พูดกล่าวพาดพิงถึงการเรียนที่แล้วมา และความเป็นจริงที่เกิดขึ้นแล้ว จอห์นไม่ได้ศึกษาอย่างขยันเลย เขาจึงสอบตก)
หรือ
“If you had asked me,………………”
(= ผู้พูดกล่าวพาดพิงถึงการเรียนที่แล้วมา และความจริงที่เกิดขึ้นคือคุณไม่ได้ขอร้องผม ผมก็เลยไม่ได้ช่วยคุณ)
หรือ
“If the hat had suited me, I would have bought it.”
(= ผู้พูดกล่าวพาดพิงเรื่องที่แล้วมาเช่นกัน ซึ่งมีความหมายว่า “(เมื่อวานนี้ตอนที่ผมไปดูหมวกใบหนึ่งในร้าน) ถ้ามันเหมาะกับผม ผมก็คงซื้อมันไปแล้ว” ซึ่งความเป็นจริงก็คือ ผมไม่ได้ซื้อหมวกใบนั้น  เพราะมันไม่เหมาะกับผม)

ลองทำความเข้าใจกับตัวอย่างอื่นๆ ต่อไป

อนึ่ง ยังมีรูปแบบเงื่อนไขไม่จริง หรือเงื่อนไขสมมุติย้อนอดีตอีกรูปหนึ่งที่ใช้กันในภาษาอังกฤษ คือ

IF + Past Perfect Tense        Future Perfect Tense

ซึ่งมีความหมายแตกต่างจากรูปแบบหลักที่กล่าวถึงข้างต้นนี้ เช่น
If you had been there last night, you will have helped me.
จะมีความหมายว่า “ผู้พูดไม่ทราบว่าเมื่อคืนคุณอยู่ที่นั่นหรือเปล่า แต่แน่ใจว่าถ้าคุณอยู่คุณคงช่วยผมไปแล้ว” ซึ่งมีความหมายต่างจาก
“If you had been there last night, you would have helped me. หมายความว่า “ผู้พูดแน่ใจว่า คุณไม่ได้อยู่ที่นั่น คุณจึงไม่ได้ช่วยผม” สามารถกล่าวได้ว่า เงื่อนไขไม่จริงหรือเงื่อนไขสมมุติแบบ 2 นี้ ใช้แสดงความหมายได้ทั้ง 3 กาลเวลาด้วยกัน คือ
1. Present time (ปัจจุบันกาล)
If Henry were here, he would know the answer.
If I had the money, I should buy a new car.
If the grass needed cutting, I would cut it.
If the hat suited me, I would buy it.

แม้ว่ากิริยาในประโยคจะเป็นรูปอดีต แต่ความหมายเป็นเงื่อนไขปัจจุบัน คือ จะมีความหมายเท่ากับ
“If Henry were here Now………..” หรือ “If I had the money Now……….” หรือ “If the grass needed cutting Now…………….”
แต่ทุกอย่างที่กล่าวนั้นมีเหตุข้ดข้องที่จะเกิดขึ้นไม่ได้ (Impossible) ตามรูปแบบโครงสร้าง 2.1

2. Past time (อดีตกาล) เช่น
If John had worked hard last term, he would have passed the examination.
If you had asked me that time, I would have helped you.

ความหมายประโยคต่างๆ จะเป็นไปตามลักษณะโครงสร้าง 2.2 ตามที่กล่าวมาแล้ว

3. Future Time (อนาคตกาล) เช่น
รูปแบบของมันส่วนหนึ่งจะเป็นไปตามลักษณะโครงสร้าง 2.1 แต่บางครั้งอาจใช้ Adverb of Time ขยาย แสดงความเป็น
อนาคตอย่างชัดเจน หรือบางทีใช้โครงสร้าง “were + To-lnfinitive” เช่น
If Richard worked hard next term, he would pass the examination.
What would you say if I were to tell you that Mary is going to be married?
If our train were to arrive punctually, we should have time to visit your sister.

ความหมายของประโยคต่างๆ ก็เป็นไปตามลักษณะโครงสร้าง 2.1 เช่นกัน

ลักษณะการวาง IF-Clause
ได้กล่าวมาข้างต้นบ้างแล้วว่า การวาง If-Clause อาจจะวางสลับกันก็ได้ คือ
1. วาง IF Clause ก่อน และตามด้วย Main Clause ซึ่งจะมีความหมายที่เน้นมาก เช่น
If he should come here, please tell me.
If our train were to arrive punctually, we should have time to visit your sister.
If John had worked hard last term, he would have passed the examination.

ซึ่งส่วนมากมักจะวาง IF-Clause ในลักษณะเช่นนี้

2. วาง Main Clause แล้วตามด้วย IF-Clause ความหมายของรูป IF-Clause เช่นนี้จะไม่เน้น แต่เป็นการบอกกล่าวธรรมดา เช่น
Please tell me if he should come here.
We should have time to visit your sister if our train were to arrive punctually.
John would have passed the examination if he had worked hard.
I will go if it should be necessary.

3. วางในระบบ Inversion Form รูปแบบนี้ใช้ในภาษาเขียนเท่านั้น ซึ่งจะเขียนรูปแบบนี้ได้ก็ต่อเมื่อมีกิริยาช่วย (Helping Verbs) อยู่ในส่วน IF-Clause แล้วย้ายกิริยาช่วย (Helping Verbs) ตัวนั้นไว้หน้าประธาน ตัด “if” ทิ้ง เช่น
1. Should he come here, please tell me. (มาจาก If he should come here, please tell me.)
2. Were our train to arrive punctually, we should have time to visit your sister.
3. I will go, should it be necessary.
4. Had John worked hard last term, he would have passed the examination.

ที่มา:อาจารย์ชำนาญ  ศุภนิตย์, ดร.สัญญา  จัตตานนท์, อาจารย์สุทิน  พูลสวัสดิ์


English Language Grammar – Conditionals: The Zero Conditional


This video explains how to use the zero conditional form, which is used to talk about real conditions and results. Here’s an example: If I am sick, I stay home and rest. What do you do if you are sick? AmericanEnglish
To learn more English or to get additional resources for English language teaching, see:
http://www.facebook.com/AmericanEnglishatState
http://www.facebook.com/AmericanEnglishforEducators
http://americanenglish.state.gov

นอกจากการดูบทความนี้แล้ว คุณยังสามารถดูข้อมูลที่เป็นประโยชน์อื่นๆ อีกมากมายที่เราให้ไว้ที่นี่: ดูเพิ่มเติม

English Language Grammar - Conditionals: The Zero Conditional

How to use the zero and first conditional – 6 Minute Grammar


If you study English grammar every day, your English will get better! In this episode of 6 Minute Grammar, we take a look at the zero and first conditionals!
ZERO conditional: If you drop an apple, it falls to the ground.
FIRST conditional: If you drop that glass, it will break. Learn more in this video and visit our website: http://www.bbc.co.uk/learningenglish/english/course/intermediate/unit5/tab/grammar
0:00 Introduction
0:56 Zero conditional
2:10 First conditional
4:15 Quiz
If you enjoyed this video, you’ll also find these videos really useful:
How to Use The Second Conditional:
https://youtu.be/3OuqzHxlrHc
How to Use The Third Conditional:
https://youtu.be/P4_kqBsj2sE
Mixing Conditionals:
https://youtu.be/_Mv7fBqauvc
BBCLearningEnglish Grammar 6MinuteGrammar

How to use the zero and first conditional - 6 Minute Grammar

When To Use ZERO + FIRST Conditional Sentences | Accurate English Grammar


Today, we’re focusing on accurate English grammar and lots of practice with Zero and First Conditional sentences! I’ll help you to study the difference between them and practice using them in different contexts so you know when to use the 0 and 1st conditional in English!
mmmMore Conditional Videos to watch:
All conditionals: http://bit.ly/mmmEnglishConditionals
Second Conditional: https://bit.ly/SecondConditionalSentences
\r
Read the full transcript of this lesson on my blog here:\r
https://www.mmmenglish.com/blog\r
\r
TAGS: mmmEnglish English Conditionals EnglishGrammar WhatsTheDifference FirstConditional ZeroConditional EnglishLesson YouTubeTeacher EnglishWithEmma \r
\r
\r
\r
More English Language Tools I recommend!\r
⭐️Improve your English pronunciation and expression by imitating a native English speaker: https://www.mmmenglish.com/imitation\r
⭐️Try Grammarly Grammar Checker it’s FREE! https://www.grammarly.com/mmmenglish\r
⭐️English Listening practice Try Audible for FREE! http://www.audibletrial.com/mmmEnglish \r
\r
\r
\r
Find mmmEnglish here: \r
mmmEnglish Website: http://bit.ly/mmmEnglish \r
On Facebook: http://bit.ly/mmmEnglishFB\r
On Instagram: http://bit.ly/mmmEnglishInsta \r
SUBSCRIBE ➡️ http://bit.ly/Subscribe2mmmEnglish (New lessons every week!)\r
\r
Emma also founded The Ladies’ Project: http://bit.ly/TheLadiesProject \r
A place for international women to build confidence and English speaking fluency! 💃\r
\r
\r
\r
TRANSLATE THIS VIDEO!\r
Do your friends a favour and help to translate this lesson into your native language! Contribute subtitles translations here: \r
http://www.youtube.com/timedtext_video?ref=share\u0026v=ZYdIhPgIDkg\r
Your name will be featured underneath the video 😝

When To Use ZERO + FIRST Conditional Sentences | Accurate English Grammar

zero conditional คืออะไร วิธีใช้ ใครจะสอบดูด่วน เรียนอังกฤษกับ ดร.พี่นุ้ย


ดร.พี่นุ้ย สมิตา หมวดทอง
โรงเรียนสอนภาษาอังกฤษ NuiEnglish
เกียรตินิยมอันดับ 1 อักษร จุฬาฯ
นักเรียนทุน ก.พ. และทุนรัฐบาลอังกฤษ
พขส.2 สถาบันพระปกเกล้า
FA 7 สถาบันพระปกเกล้า
พสบ.26 กองทัพบก
พคบ.10 กอ.รมน
กรรมการสงเคราะห์ สถานพินิจสมุทรสาคร
The Startup Ready Batch3
SIP 44 ธนาคารกรุงเทพ
http://www.nuienglish.com
http://www.facebook.com/nuienglish

zero conditional คืออะไร วิธีใช้ ใครจะสอบดูด่วน เรียนอังกฤษกับ ดร.พี่นุ้ย

CONDITIONALS | Learn all the conditionals | English grammar


We hope you enjoyed this video! If you have any questions please ask in the comments.
Please like subscribe and share your comments with us!
⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎⬇︎
Ultimate parts of speech guide: https://www.youtube.com/watch?v=dAP7q…
⇢ ✅ Follow us on Instagram: https://www.instagram.com/ilearneasye…
⇢ ✅ AMAZON 30DAY FREE TRIAL: https://amzn.to/39Bo067
FREE GRAMMAR CHECKER:
⇢ ✍🏼 📚 https://grammarly.go2cloud.org/SH2QH
RECOMMENDED BOOKS: 📚👩🏻‍💼
⇢ Fantasic Mr Fox book: https://amzn.to/31b3Pb5
⇢ Fantastic Mr Fox audiobook: https://amzn.to/2XfwBX0
⇢ Harry Potter book: https://amzn.to/316UVvw
⇢ Harry Potter audiobook: https://amzn.to/31at6lQ
GRAMMAR BOOKS 📚
⇢ 📕 Beginner level: https://amzn.to/3fhEOjF
⇢ 📘 Intermediate level: https://amzn.to/2Pdgl4i
⇢ 📗 Advanced level: https://amzn.to/2BNSg10
English shows to watch: 🖥
⇢ Fantastic Mr Fox: https://amzn.to/3k8cTqf
⇢ Harry Potter: https://amzn.to/30gzskw
⇢ Sherlock Holmes: https://amzn.to/2EwpLpx
⇢ 🎙Microphone I use: https://amzn.to/3jTSyED
Links marked with an are affiliate links.
Time stamps:
0:00 Intro
0:17 What is a conditional?
1:00 Zero conditional
1:45 First conditional
2:20 Second conditional
3:05 Third conditional
3:40 Mixed conditional
Music: Royalty Free music https://www.bensound.com/

CONDITIONALS | Learn all the conditionals | English grammar

นอกจากการดูบทความนี้แล้ว คุณยังสามารถดูข้อมูลที่เป็นประโยชน์อื่นๆ อีกมากมายที่เราให้ไว้ที่นี่: ดูวิธีอื่นๆLEARN FOREIGN LANGUAGE

ขอบคุณมากสำหรับการดูหัวข้อโพสต์ zero conditional คือ

Leave a Reply

Your email address will not be published. Required fields are marked *