ค ำสั่งวนซ ้ำ (Loop)

25
Computer Science, CMU คำสั่งวนซ้ำ (Loop) 204101 Introduction to computer 1 While , For

Transcript of ค ำสั่งวนซ ้ำ (Loop)

Page 1: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ค ำสัง่วนซ ำ้ (Loop)

204101 Introduction to computer 1

While , For

Page 2: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

งำนท่ีต้องท ำซ ำ้เป็นจ ำนวนรอบ ใช้ได้ทัง้กำรวนรอบท่ีมีจ ำนวนรอบท่ีแน่นอนและไม่แน่นอน

มีเง่ือนไขในกำรหยดุกำรท ำงำน

ตรวจเง่ือนไขก่อนกำรท ำงำนทกุครัง้ ถ้ำใช่ตำมท่ีเง่ือนไขต้องกำร จะท ำงำนซ ำ้ต่อไป

อำจไม่ได้ท ำเลยแม้แต่ครัง้เดียว

1. Loop : While

204101 Introduction to computer 2

Flow diagram

Page 3: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ค ำสัง่ while จะทดสอบเง่ือนไขก่อนท ำงำน จะวนรอบท ำงำนเม่ือเง่ือนไขเป็นจริง

รปูแบบ

while condition :

statement(s)

อธิบำยCondition คือ เง่ือนไขในกำรตดัสินใจว่ำต้องกำรให้ท ำงำนหรือไม่ ถ้ำเง่ือนไขเป็นจริง (True) จึงจะท ำงำนstatement คือค ำสัง่ท่ีต้องกำรให้ท ำงำนในกรณีท่ี เง่ือนไขเป็นจริง

1. Loop : While

204101 Introduction to computer 3

Flow diagram

Page 4: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ให้รบัคะแนนของนักศึกษำกลุ่มหน่ึงแล้วท ำกำรแสดงผล

โดยก ำหนดให้กำรป้อนคะแนนเป็น -1 คือส้ินสดุกำรรบัคะแนน

(ให้ใช้ค ำสัง่ while)

วิเครำะหโ์จทย์

ผลลพัธ์ คือ แสดงคะแนนของนักศึกษำกลุ่มหน่ึง

ข้อมลูเข้ำ คือ คะแนนของนักศึกษำกลุ่มหน่ึง (score)

กำรประมวลผล จ ำนวนรอบไม่แน่นอน

ผูใ้ส่ข้อมลู -1 ส้ินสดุกำรรบั เป็นเง่ือนไขในกำรหยดุ

ตัวอย่าง 1 รับและแสดงคะแนน

204101 Introduction to computer 4

Page 5: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ตัวอย่าง 1 รับและแสดงคะแนน(ต่อ)

204101 Introduction to Computer 5

รบัค่ำคะแนนจำกผูใ้ช้

ถ้ำคะแนนท่ีรบัมำไม่เท่ำกบั-1 ให้แสดงผลคะแนนนัน้แล้วเร่ิมต้นรบัคะแนนใหม่

เร่ิมต้นผงังำน

ส้ินสดุผงังำน

input score

print score

score != -1False

True

input score

start

stop

Page 6: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

เขียนโปรแกรมภำษำไพทอน score.py ได้ดงัน้ี

inp = input("score :")

score = float(inp)

while score != -1 :

print(score)

inp = input("score :")

score = float(inp)

ตัวอย่าง 1 รับและแสดงคะแนน(ต่อ)

204101 Introduction to computer 6

ตวัอย่ำงกำรท ำงำนและผลลพัธท่ี์ได้

Page 7: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ให้รบัค่ำเลขจ ำนวนจริงค่ำหน่ึงแล้วท ำกำรแสดงผลของค่ำก ำลงัสองของเลขนัน้ โดยก ำหนดให้ถ้ำมีกำรป้อนค่ำลบให้ส้ินสดุกำรรบัข้อมลู(ก ำหนดให้ใช้ ค ำสัง่ while )

วิเครำะหโ์จทย์ผลลพัธ์ คือ ค่ำก ำลงัสองของตวัเลขท่ีรบัเข้ำมำข้อมลูเข้ำ คือ เลขจ ำนวนจริง (num)กำรประมวลผล จ ำนวนรอบไม่แน่นอน

ป้อนค่ำข้อมลูเป็นเลขลบ เงื่อนไขในกำรหยดุวนท ำซ ำ้

ตัวอย่าง 2

7204101 Introduction to computer

Page 8: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ตัวอย่าง 2 (ต่อ)

8

input num

num >= 0False

True

input num

start

stop

print num**2

inp=input('Input float number : ')

num = float(inp)

while num >= 0 :

print('Power of num=', num**2)

inp=input('Input float number : ')

num = float(inp)

ผลลพัธท่ี์ได้

เขียนโปรแกรมภำษำไพทอน power.py

204101 Introduction to computer

Page 9: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

Infinite Loop

var = 1

while var == 1 : # This constructs an infinite loop

num = input("Enter a number :")

print ("You entered: ", num)

print ("Good bye!“)

9204101 Introduction to computer

ข้อควรระวงั!! เรำอำจเขียนโปรแกรมแล้วเกิด วนซ ำ้ไม่จบส้ิน (infinite loop) ดงัตวัอย่ำง

จำกตวัอย่ำงข้ำงต้น โปรแกรมจะวนท ำงำนไม่จบส้ิน เรำต้อง กด CTRL+C เพ่ือจบกำรท ำงำนของโปรแกรม : Keyboard Interrupt

เมื่อโปรแกรมท ำงำน สมมติุเรำใส่ข้อมลูดงัตวัอย่ำงEnter a number :20

You entered: 20

Enter a number :29

You entered: 29

Enter a number :3

You entered: 3

Page 10: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ปกตภิาษาทีเ่ราคุน้เคย เมือ่ท างาน Loop ดว้ยค าสัง่ For มกัจะใชต้วัเลขเป็นตวันบัการวนซ ้า

ค าสัง่ For ในภาษาไพทอน จะใช ้ล าดบั(sequence) เป็นตวัวนซ ้า

ตวัอยา่ง เป็นการเปรยีบเทยีบการเขยีนโปรแกรมเพือ่ loop โดยใช้

ค าสัง่ while และค าสัง่ for

2. Loop : For

204101 Introduction to computer 10

counter = 0

while counter <= 10 :

print (counter)

counter +=1

for counter in range (10):

print (counter)

ผลลพัธ์

0

1

2

3

4

5

6

7

8

9

10

ผลลัพธ์

0

1

2

3

4

5

6

7

8

9

Page 11: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ค ำสัง่ for ในภำษำไพทอนจะวนซ ำ้เพ่ือท ำงำน โดยใช้ล ำดบั (sequence) เช่น ล ำดบัท่ีเรำระบโุดยตรง , ลิสต์ , สตริง ,range()

รปูแบบ for… in … :

for iterating_var in sequence :

statements(s)

อธบิาย interating_var คอื ตวัแปรทีใ่ชว้นซ ้า

sequence คอื ล าดบั

statement(s) คอืค าสัง่ในขอบเขตของค าสัง่ for จะวนรอบท าซ ้า(True) ส าหรบั next item from sequence และออกจากการวนรอบ(False) เมือ่ no more item in sequence

2. Loop : For

204101 Introduction to computer 11

Flow diagram

Page 12: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

(1) ค ำสัง่ for วนซ ำ้ท ำงำน ล ำดบัท่ีใช้คือ : ก ำหนดล ำดบัโดยตรง

ตวัอย่ำง

for i in 1,2,3,4,5 :print(i)

for i in 'one','two','three':print(i)

2. Loop : For

204101 Introduction to computer 12

ผลลัพธ์

1

2

3

4

5

ผลลัพธ์

one

two

three

ใชล้ ำดับ (sequence) : ล ำดับทีเ่รำระบโุดยตรง , ลสิต์ , สตรงิ ,range()

Page 13: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

(2) ค ำสัง่ for วนซ ำ้ท ำงำน ล ำดบัท่ีใช้คือ : สตริงหรือข้อควำม

for letter in 'Python' :

print (letter)

(3) ค ำสัง่ for วนซ ำ้ท ำงำน ล ำดบัท่ีใช้คือ : ลิสต์

fruits = ['banana', 'apple', 'mango']

for word in fruits :

print(word)

2. Loop : For

204101 Introduction to computer 13

a = ['cat', 'window', 'defenestrate']

for x in a :

print (x)

ผลลพัธ์Python

ผลลพัธ์cat

window

defenestrate

ผลลพัธ์banana

apple

mango

ใชล้ ำดับ (sequence) : ล ำดับทีเ่รำระบโุดยตรง , ลสิต์ , สตรงิ ,range()

Page 14: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

(4) ค ำสัง่ for วนซ ำ้ท ำงำน ล ำดบัท่ีใช้คือ : range()

for i in range(10) :

print (i)

กำรวนซ ำ้ ค ำสัง่ for ดงัตวัอย่ำงข้ำงต้น

มีกำรเรียกใช้ ฟังกช์นั range() อธิบำยฟังกช์นั range() ได้ดงัน้ี

2. Loop : For

204101 Introduction to computer 14

ผลลัพธ์

0

1

2

3

4

5

6

7

8

9

ใชล้ ำดับ (sequence) : ล ำดับทีเ่รำระบโุดยตรง , ลสิต์ , สตรงิ ,range()

Page 15: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

รปูแบบ range([start,] stop [,step])

ฟังกช์นั range() มีกำรส่งหรือ pass parameter ให้ฟังกช์นั range ได้ 3 argument

- pass 1 arg. called stop ในกรณีน้ีฟังกช์นั range จะ return sequence ในขอบเขต ตัง้แต่ 0 – (end-1)

- pass >=2 arg. ตวัแรกเรียก start ตวัถดัมำเรียก stop ตวัสดุท้ำยเรียก step

ล ำดบัจะถกูสร้ำง ด้วยกำรเพ่ิมค่ำจำก start ไป end โดยเพ่ิมขึน้ด้วยค่ำของ step(ถ้ำ step เป็นบวก ค่ำสดุท้ำยใน sequence คือ largest multiple less than end)

ตวัอย่ำง

range(10) range(0,10) range(0,10,1) ได้ผลลพัธเ์หมือนกนัคือตวัเลขตัง้แต่ 0 ถึง 9

2. Loop : For

204101 Introduction to computer 15

argument เป็นเลขจ ำนวนเตม็

สรุป รูปแบบการใช้ function range() มี 3 รูปแบบ คือ

1 arg range(10)

2 arg range(0,10)

3 arg range(0,10,1)

ใชล้ ำดับ (sequence) : ล ำดับทีเ่รำระบโุดยตรง , ลสิต์ , สตรงิ ,range()

Page 16: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ตวัอย่ำง ค ำสัง่ for และกำรเรียกใช้ ฟังกช์นั range()

for counter in range (1,101) :

# vary control variable from 1 to 100 increments of 1

for counter in range (100, 0 ,-1) :

# vary control variable from 100 to 1 increments of -1

for counter in range (7,78,7) :

# vary control variable from 7 to 77 in steps of 7

for counter in range (2,21,3) :

# vary control variable over following sequence of value 2 5 8 11 14 17 20

for counter in range (1,10,4) :

# vary control variable over following sequence of value 1,5,9

for counter in range (99,-1,-11) :

# vary control variable over following sequence of value 99 88 77 66 55 44 33 22 11 0

2. Loop : For

204101 Introduction to computer 16

รูปแบบ range([start,] stop [,step])

Page 17: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ให้รบัคะแนนของนักศึกษำ 10 คนแล้วท ำกำรแสดงผล (ใช้ค ำสัง่ for)วิเครำะหโ์จทย์

10 คน จ ำนวนรอบแน่นอน

ตวัอย่ำง 1

204101 Introduction to Computer 17

- ก ำหนดขอบเขตกำรวนซ ้ำ

-ในแตล่ะรอบทีท่ ำงำนตวัแปร i จะเพิม่ขึน้ทลีะหนึง่

-รวมท ำงำน 10 รอบ

print score

input score

start

stop

for i in range(10)False

True

Page 18: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

เขยีนโปรแกรมภาษาไพทอน ScoreFor.py ไดด้งัน้ี

ตวัอย่ำง 1

204101 Introduction to computer 18

for i in range(10) :

inp=input("score :")

score=float(inp)

print(score)

print score

input score

start

stop

for i in range(10)False

True

Page 19: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ให้รบัคะแนนของนักศึกษำ n คนแล้วท ำกำรค ำนวณและ

แสดงผลคะแนนเฉล่ีย โดยท่ีค่ำ n ให้รบัจำกผูใ้ช้ (For)

วิเครำะหโ์จทย์

- n คน จ ำนวนรอบแน่นอน

- ก ำหนดตวัแปร sum ไว้ส ำหรบัเกบ็ค่ำผลรวม (ตวัแปรท่ีเกบ็ค่ำผลรวมก่อนใช้งำนต้องให้ค่ำเร่ิมต้นเป็น 0 ก่อน)

ตวัอย่ำง 2

204101 Introduction to Computer 19

Page 20: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

เขียนโปรแกรมภำษำไพทอน ScoreForAvg.py ได้ดงัน้ี

ตวัอย่ำง 2 (ต่อ)

204101 Introduction to Computer 20

print sum , avginput score

read n

avg = sum / n

sum = 0

sum = sum + score

start

stop

sum=0.0

inp=input( ' number student : ')

n=int(inp)

for i in range(n) :

inp=input('score : ')

score=float(inp)

sum += score

print(score)

print('Sum = ', sum , 'Average = ' ,sum/n)

True

Falsefor i in range(n)

5 55 42 75 85 90

347 69.4

Page 21: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

โจทย ์คุณ อะเลก็ซสิ ฝากเงนิในบญัชสีะสมทรพัย ์ $1000 ไดร้บัอตัราดอกเบีย้ 5%ต่อปี และดอกเบีย้ทีไ่ดใ้นแต่ละปีกส็ะสมเขา้ในบญัชดีงักล่าว เมื่อฝากครบ 10 ปี จงเขยีนผงังานและโปรแกรมแสดงเงนิในบญัชตีัง้แต่ปีที ่1 จนถงึปีที ่10

ก าหนดสตูรดงัน้ี

a = p(1+r)n

p = จ านวนเงนิเริม่ตน้

r = อตัราดอกเบีย้

n = จ านวนปีทีฝ่าก

a = จ านวนเงนิทัง้หมดเมือ่ครบ n ปี

ตวัอย่ำง 3

204101 Introduction to computer 21

Page 22: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

ก ำหนดตวัแปร

p=1000 จ ำนวนเงินเร่ิมต้น

r = 0.05 อตัรำดอกเบี้ย

year = 1 โปรแกรมวนรอบตำมจ ำนวนปีท่ีโจทยก์ ำหนดคือ 10 ปี

a = จ ำนวนเงินทัง้หมดท่ีได้ ตัง้แต่ปีท่ี 1 จนครบ 10 ปี

สตูร a=p*(1+r)**year

จำกโจทย ์ก ำหนดตวัแปรและเขียนผงังำนได้ดงัน้ี

204101 Introduction to Computer 22

Page 23: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

จากโจทย ์การค านวณเงนิฝาก เขยีนโปรแกรมภาษาไพทอนไดด้งันี้

ตวัอย่ำง 3 (ต่อ)

204101 Introduction to computer 23

จำกโปรแกรมได้ผลลพัธด์งัน้ี

#Calculate Amountp=1000 # start principler=0.05 # interest rateprint ("Year %21s " % "Amount on deposit")for year in range (1,11):

a = p*(1+r)**yearprint ("%4d %21.2f" %(year , a))

Page 24: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

The keyword argument end can be used to avoid the newline after the output, or end the output with a different string:

ตวัอย่ำงa = 0b = 1

while b < 1000 :

print(b, end=',')

a = b

b = a+b

ผลลพัธท่ี์ได้

ฟังกช์นั print():กำรแสดงผลแบบไม่ขึน้บรรทดัใหม่

204101 Introduction to Computer 24

Page 25: ค ำสั่งวนซ ้ำ (Loop)

Computer Science, CMU

1. จงเขียนผงังำนและเขียนโปรแกรมส ำหรบัในกำรบวกเลขจ ำนวนเตม็ nจ ำนวน แล้วแสดงผลลพัธอ์อกทำงจอภำพ

เช่น ระบเุลขจ ำนวนเตม็ท่ีจะป้อน 5 ค่ำ โดยค่ำท่ีป้อนได้แก่ 1 5 10 6 และ 9ผลลพัธท่ี์ได้คือ 31

แบบฝึกหดั

25204101 Introduction to computer