ความแตกต่างกันของ For Loop กับ While Loop
for loop นั้น เราจะมีการบอกถึง ค่าเริ่มต้น ค่าสิ้นสุด ของตัววิ่ง จำเป็นจะต้องมีตัวมากำหนดค่า ในที่นี้คือ i = 1
อยู่ด้วยเสมอ ซึ่งตัวนับนี้ จะเป็นตัวบอกเราว่า loop ของเรานั้นทำการวนซ้ำมาเป็นรอบที่เท่าไรแล้ว และในส่วนแรกนี้เราจำเป็นจะต้องมีการกำหนด ค่าเริ่มต้นให้กับตัวนับก่อน
เช่น
myView = av.GetActiveDoc
myTheme = myView.GetActiveTheme.Get(0)
theFtab = myTheme.GetFtab
myNameField = theFtab.FindField(“Name”)
i = 1
for each rec in theFtab
theName = theFtab.ReturnValue(myNameField,rec)
if ( i > 10 ) then
exit
else
Msgbox.info(theName.AsString,(”Name in 10 records”)
end
i = i + 1
end
แต่ while loop นั้นจะบอกแค่เงื่อนไขการจบเท่านั้นเป็นแบบ Boolean เมื่อมีการทดสอบเงื่อนไข ถ้าเป็นจริงก็จะทำต่อไปเรื่อยๆ จนกว่าเงื่อนไขจะเป็นเท็จ ก็จะจบการทำงานและ จะออกจากloop
เช่น
theProject = av.GetProject
theView = theProject.FindDoc(“NewView”)
theThemeList = theView.GetTheme
MsgBox.Info (theThemeList.Get(2).AsString,””)
i = theThemeList.count – 1
MsgBox.Info (i.AsString,””)
While (theThemeList .Count<>0)
theTheme = theThemeList .Get(1)
theView.DeleteTheme (theTheme)
i = i - 1
end
วันพฤหัสบดีที่ 9 กรกฎาคม พ.ศ. 2552
สมัครสมาชิก:
บทความ (Atom)