Bài giảng Cấu trúc điều khiển - Lê Đình Ngân

pdf 28 trang hapham 2180
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Cấu trúc điều khiển - Lê Đình Ngân", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfbai_giang_cau_truc_dieu_khien_le_dinh_ngan.pdf

Nội dung text: Bài giảng Cấu trúc điều khiển - Lê Đình Ngân

  1. TRTR NGNG II HH CC BCBC MARKETINGMARKETING KHOAKHOA TINTIN HHCC QUQU NN LíLí 306 Nguy n Tr ng Tuy n – Q.TB – TP.HCM BBÀÀII GIGI NGNG GV: Lờ ỡnh Ngõn
  2. CC UU TRTR ÚÚCC II UU KHIKHI NN I/ Cu tr ỳc tu n t . II/ Cu tr ỳc ch n la (r nh ỏnh ). 2.1/ Cu tr ỳc If 2.1.1/ Cu tr ỳc If EndIf 2.2.2/ Cu tr ỳc If Else EndIf 2.2/ Cu tr ỳc Do Case . 2.3/ So sỏnh cu tr ỳc If EndIf và Do Case EndCase III/ Cu tr ỳc lp . 3.1/ Cu tr ỳc For EndFor 3.2/ Cu tr ỳc Do While 3.2.1/ Cu tr ỳc Do While .T. 3.2.2/ Cu tr ỳc Do While 3.2.3/ Cu tr ỳc Do While ! Eof () 3.3/ Cu tr ỳc Scan EndScan
  3. CCuu trtr ỳỳcc ČiČi uu khikhi nn LL pp trtr ỡỡnhnh ccuu trtr ỳỳcc ss ddngng ccỏỏcc ccuu trtr ỳỳcc ČiČi uu khikhi nn sausau :: 1.1. CCuu trtr ỳỳcc tutu ##nn tt$$ ((NN&&ii titi ''pp)) 2.2. CCuu trtr ỳỳcc chch nn ll$$aa (R(R nhnh ỏỏnhnh )) 3.3. CCuu trtr ỳỳcc ll00pp
  4. I/I/ CCuu trtr ỳỳcc tutu nn tt ((nnii titi pp)) CCỏỏcc ll11nhnh Č2Č2 33cc thithi hhàànhnh tutu ##nn tt$$ ,, ll11nhnh sausau nn&&ii titi ''pp ll11nhnh tr2tr2 66cc ThTh 88 tt$$ ccỏỏcc cõucõu ll11nhnh Č2Č2 33cc thth $$cc hihi 11nn theotheo ČČỳỳngng trtr ỡỡnhnh tt$$ vivi ''tt trongtrong ch2 >tt ch2<ngch2<ng trtr ỡỡnhnh concon S<S< ČČ@@ ttAAngng ququ ỏỏtt
  5. SS   TTNGNG QUQU ấấTT CC %%AA CC ''UU TRTR CC TUTU ))NN TT L,nh 1 L,nh 2 v v . L,nh Cu trỳc Cu trỳc Ch2<ng trỡnh ch*n l$a l0p con
  6. II/II/ CCuu trtr ỳỳcc chch 11nn llaa ((rr44 nhnh ỏỏnhnh )) LLàà ccuu trtr ỳỳcc chocho phph ộộpp chch nn ll$$aa mm>>tt trongtrong nhinhi uu h2h2 66ngng ČiČi chocho ch2 >tt bibi uu thth 88cc lulu nn lðlð chocho kk''tt ququ DD ČČỳỳngng (True(True .T.).T.) hoho 00cc saisai (False(False .F.).F.) MMJJii nhnh ỏỏnhnh thth $$cc hihi 11nn mm>>tt cụngcụng vivi 11cc ((hoho 00cc khụngkhụng llààmm ggỡỡ ccDD)) llàà ttựựyy vvààoo thuthu tt toto ỏỏnn Č2Č2 33cc cc thth  bbNNngng cõucõu ll11nhnh vivi ''tt trongtrong ch2 >tt ČiČi mm vvààoo vvàà mm>>tt ČiČi mm rara
  7. FoxproFoxpro ss ddngng 22 ddXXngng ccuu trtr ỳỳcc rr nhnh ỏỏnhnh :: a)a) CCuu trtr ỳỳcc IfIf [Else][Else] EndIfEndIf b)b) CCuu trtr ỳỳcc CaseCase [Otherwise][Otherwise] EndCaseEndCase 2.1/2.1/ CCuu trtr ỳỳcc IfIf :: LLàà ccuu trtr ỳỳcc chch nn ll$$aa ,, luụnluụn luụnluụn phph DDii kk''tt thth ỳỳcc bbNNngng EndIfEndIf CCỏỏcc ccuu trtr ỳỳcc IfIf ccúú thth  ll@@ngng vvààoo nhaunhau theotheo nhinhi uu ccpp TrongTrong ccuu trtr ỳỳcc IfIf ccúú thth  ccúú ElseElse hoho 00cc khụngkhụng DoDo ČČúú ,, FoxproFoxpro chiachia llààmm 22 lolo XXii ccuu trtr ỳỳcc IfIf
  8. 2.1.1/ 1.1/ CCuu trtr ỳỳcc IfIf EndIfEndIf DD==ngng tt??ngng ququ ỏỏtt LAu ČB IF ]Biu th8c lu n lð> ENDIF False Ch2 cho giỏ tra là True True .  N'u cú nhiu cu trỳc If EndIf n&i ti'p nhau thỡ ch2<ng L1nh thi hành trỡnh s- duy1t qua tu#n t$ tbng nhỏnh If và s- thi hành l1nh Č&i v6i nhcng nhỏnh If thoD Čiu ki1n .
  9. 2.2.2/ 2.2/ CCuu trtr ỳỳcc IfIf ElseElse EndIfEndIf DD==ngng tt??ngng ququ ỏỏtt LAu ČB False If ]Biu th8c lu n lð 1> Else True If False . Else True . EndIf EndIf
  10. TrongTrong ccuu trtr ỳỳcc IfIf ElseElse EndIfEndIf ,, nn''uu nhinhi uu > ccựựngng thotho DD thth ỡỡ ll11nhnh dd nhnh ỏỏnhnh IfIf phph ớớaa trờntrờn ss 2u2u tiờntiờn Č2Č2 33cc thth $$cc hihi 11nn vvàà bbff quaqua ccỏỏcc nhnh ỏỏnhnh IfIf cũncũn llXXii LL11nhnh dd nhnh ỏỏnhnh ElseElse cucu &&ii ccựựngng ss Č2Č2 33cc thth $$cc hihi 11nn nn''uu ccỏỏcc > dd trờntrờn ČČuu chocho trtr aa lulu nn lðlð saisai (False)(False) CCúú baobao nhiờunhiờu IfIf thth ỡỡ phph DDii Č2Č2 33cc ČČúúngng llXXii bbddii bbyy nhiờunhiờu EndIfEndIf CCuu trtr ỳỳcc nnààyy cũncũn Č2Č2 33cc gg ii llàà IfIf ll@@ngng ((ccỏỏcc nhnh ỏỏnhnh IfIf ll@@ngng vvààoo nhaunhau )) CCỏỏcc nhnh ỏỏnhnh IfIf ccúú thth  ll@@ngng vvààoo nhaunhau nhinhi uu ccpp nh2ngnh2ng tt&&ii 2u2u khụngkhụng baobao gigi hh ququ ỏỏ 33 ccpp
  11. 2.2/2.2/ CCuu trtr ỳỳcc DoDo CaseCase DD==ngng tt??ngng ququ ỏỏtt Do Case Case ]Biu th8c lu n lð 1> Case . Otherwise EndCase
  12. LAu ČB v v
  13. CCuu trtr ỳỳcc DoDo CaseCase LLàà mm>>tt ccuu trtr ỳỳcc rr nhnh ỏỏnhnh ((chch nn ll$$aa)) ,, chocho phph ộộpp chch nn mm>>tt trongtrong ccỏỏcc khkh DD ningning ccúú thth  xxDDyy rara CCuu trtr ỳỳcc DoDo Case Case phph DDii Č2Č2 33cc chch mm dd88tt bbNNngng EndCaseEndCase TrongTrong ccuu trtr ỳỳcc DoDo CaseCase ,, nn''uu ccúú nhinhi uu ČiČi uu kiki 11nn ((ExpLExpL )) ccựựngng thth ffaa thth ỡỡ chch `` nhnh ỏỏnhnh CaseCase nnààoo ccúú vvaa trtr ớớ dd trờntrờn ss Č2Č2 33cc 2u2u tiờntiờn thth $$cc hihi 11nn ((vvàà bbff quaqua tttt ccDD ccỏỏcc nhnh ỏỏnhnh CaseCase cũncũn llXXii)) . . TrongTrong ccuu trtr ỳỳcc DoDo CaseCase , , thụngthụng th2th2 hhngng ccúú ČiČi uu khokho DDnn OtherwiseOtherwise Č2Č2 33cc vivi ''tt ČČ thth $$cc hihi 11nn ll11nhnh cucu &&ii ccựựngng ((nn''uu ccỏỏcc nhnh ỏỏnhnh CaseCase tr2tr2 66cc ČČúú ČČuu khụngkhụng thth ffaa)) CCúú thth  ll@@ngng ccỏỏcc ccuu trtr ỳỳcc khkh ỏỏcc vvààoo ttbbngng nhnh ỏỏnhnh CaseCase
  14. 2.3/ 3/ SoSo ssỏỏnhnh ccuu trtr ỳỳcc IfIf EndIfEndIf vvàà DoDo CaseCase EndCaseEndCase GiGi ngng nhaunhau :: IfIf EndIfEndIf vvàà DoDo CaseCase EndCaseEndCase ČČuu llàà ccuu trtr ỳỳcc rr nhnh ỏỏnhnh ,, chocho phph ộộpp tata ČiČi uu khikhi nn h2h2 66ngng lulu nn lðlð ccjjaa ch2 >tt trongtrong ccỏỏcc khkh DD ningning ccúú thth  xxDDyy rara ((NN''uu ccúú 22 hoho 00cc 33 nhnh ỏỏnhnh thth ỡỡ ss ddngng ccuu trtr ỳỳcc IfIf EndIfEndIf ccúú hihi 11uu ququ DD h<nh<n ).). KhKh ỏỏcc nhaunhau :: CCuu trtr ỳỳcc IfIf EndIfEndIf ccúú thth  chiachia thth àànhnh ddXXngng IfIf nn&&ii titi ''pp vvàà IfIf ll@@ngng ,, trongtrong khikhi DoDo CaseCase chch `` ccúú thth  ČiČi uu khikhi nn h2h2 66ngng lulu nn lðlð ccjjaa ch2<ngch2<ng trtr ỡỡnhnh t2<ngt2<ng Č2<ngČ2<ng ccuu trtr ỳỳcc IfIf ll@@ngng
  15. SS ddngng ccuu trtr ỳỳcc IfIf EndIfEndIf vvàà DoDo CaseCase EndCaseEndCase trongtrong tr2tr2 hhngng hh33pp cc##nn chch nn mm>>tt trongtrong nhnh ccngng khkh DD ningning ccúú thth  xxDDyy rara ((xxỏỏcc ČČaanhnh h2h2 66ngng ČiČi chocho ch2 >tt trongtrong ccỏỏcc khkh DD ningning ccúú thth  xxDDyy rara thth ỡỡ chch nn IfIf ElseElse EndIfEndIf hoho 00cc DoDo CaseCase EndCaseEndCase .(.( NN''uu ss&& nhnh ỏỏnhnh <=3<=3 thth ỡỡ chch nn ccuu trtr ỳỳcc IfIf vvàà ng2ng2 33cc llXXii).).
  16. III/III/ CCuu trtr ỳỳcc llMMpp LLàà mm>>tt ccuu trtr ỳỳcc chocho phph ộộpp tata rara ll11nhnh thithi hhàànhnh nhinhi uu ll##nn mm>>tt nhnh úúmm ll11nhnh ghighi trongtrong phph ##nn thõnthõn ccjjaa ccuu trtr ỳỳcc FoxproFoxpro ccúú ccỏỏcc ddXXngng ccuu trtr ỳỳcc ll00pp sausau ::  ForFor EndForEndFor ỗỗ DoDo WhileWhile EndDoEndDo ‡‡ DoDo WhileWhile .T T. ‡‡ DoDo WhileWhile > ‡‡ DoDo WhileWhile !! EofEof ()() // DoDo WhileWhile BofBof ()() ộộ ScanScan EndScanEndScan
  17. 3.1/I.1/ CCuu trtr ỳỳcc ForFor EndForEndFor DD==ngng tt??ngng ququ ỏỏtt Biu thPc Biu thPc quy ČSnh giTi BATc lMp khQi t=o h=n kt thỳc vũng lMp For To [Step +/- ] . . [Loop] [Exit] EndFor
  18. LLAuAu ČČBB False True Command
  19. VũngVgng ll00pp ForFor EndForEndFor chocho phph ộộpp ll00pp llXXii mm>>tt nhnh úúmm ll11nhnh trongtrong thõnthõn ccuu trtr ỳỳcc vv66ii ss&& ll##nn ll00pp Č2Č2 33cc bibi ''tt tr2tr2 66cc ((quyquy ČČaanhnh bbddii ExpN2>).ExpN2>). CCuu trtr ỳỳcc ForFor phph DDii Č2Č2 33cc kk''tt thth ỳỳcc bbddii EndForEndFor [Step[Step +/ +/ ccúú thth  ccúú hoho 00cc khụngkhụng trongtrong vũngvũng ll00pp ForFor (( gigi ỏỏ trtr aa mm00cc nhiờnnhiờn llàà +1).+1). TrongTrong thõnthõn vũngvũng ll00pp ForFor ccúú thth  ccúú ll11nhnh LoopLoop ((chocho phph ộộpp quayquay trtr dd llXXii vũngvũng ll00pp)) hoho 00cc ExitExit ((chocho phph ộộpp thotho ỏỏtt khkh ffii vũngvũng ll00pp).).
  20. 3.2/3.2/ CCuu trtr ỳỳcc DoDo WhileWhile EndDoEndDo 3.2.1/ Cu trỳc Do While .T. D=ng t?ng quỏt LAu ČB False Do While .T. .T. . True If Loop Command Else Exit EndIf EndDo
  21.  cc ii mm ccaa DoDo WhileWhile .T T. 1) LLàà mmtt ccuu trtr ỳỳcc chocho phph ộộpp thithi hhàànhnh nhinhi uu llnn mmtt nhnh úúmm llnhnh vv ii ss vũngvũng llpp khụngkhụng bibi tt trtr cc 2) CCuu trtr ỳỳcc DoDo WhileWhile .T T. phph ii   cc kk tt thth ỳỳcc bbii llnhnh EndDoEndDo 3) DoDo WhileWhile .T T. khụngkhụng kiki mm tratra ii uu kiki nn  vvààoo vũngvũng llpp ,, do do úú ss llnn llpp phph ii   cc thth cc hihi nn ớớtt nhnh tt llàà 11 llnn 4) TrongTrong ccuu trtr ỳỳcc DoDo While While .T. .T. ccúú thth  ccúú llnhnh LoopLoop  gigi ỳỳpp quayquay trtr  llii vũngvũng llpp 5) TrongTrong ccuu trtr ỳỳcc DoDo WhileWhile .T T. ,, bbtt bubu cc phph ii ccúú mmtt bibi uu thth cc lulu nn lýlý ((ExpLExpL ))  thithi hhàànhnh llnhnh ExitExit thotho ỏỏtt khkh ii vũngvũng llpp
  22. 3.2.2/3.2.2/ CCuu trtr ỳỳcc DoDo WhileWhile > Dng tng quỏt Lu  False Do While =.T. True [Loop] [Exit] EndDo
  23. MMcc ČiČi mm ccWWaa ccuu trtr ỳỳcc DoDo WhileWhile > 1. . ChoCho phph ộộpp thithi hhàànhnh nhinhi uu ll##nn mm>>tt nhnh úúmm ll11nhnh trongtrong thõnthõn vũngvũng ll00pp ((KhiKhi > vvppnn cũncũn chocho gigi ỏỏ trtr aa llàà TrueTrue .T.).T.) 2.2. CCuu trtr ỳỳcc DoDo WhileWhile > luụnluụn kiki mm tratra ČiČi uu kiki 11nn ČČ vvààoo vũngvũng ll00pp 3.3. DoDo WhileWhile > luụnluụn phph DDii Č2Č2 33cc kk''tt thth ỳỳcc bbddii EndDoEndDo 4.4. TrongTrong thõnthõn vũngvũng ll00pp ccúú thth  ccúú ll11nhnh LoopLoop hoho 00cc ExitExit
  24. 3.2.3/3.2.3/ CCuu trtr ỳỳcc DoDo WhileWhile !! EofEof ()() Dng tng quỏt Lu  Use Use . Do While !Eof() False !EOF() True Skip Command EndDo Use Skip
  25. MMcc ČiČi mm ccWWaa ccuu trtr ỳỳcc DoDo WhileWhile !! EofEof ()() ( CCuu trtr ỳỳcc DoDo While While ! ! EofEof ()() th2th2 hhngng Č2Č2 33cc ss ddngng ČČ duyduy 11tt quaqua ttbbngng mmppuu tintin (Record) (Record) trờntrờn tt pp CSDLCSDL ČangČang Č2Č2 33cc mmdd trongtrong khukhu vv$$cc llààmm vivi 11cc hihi 11nn hhàànhnh 2) KhiKhi concon trtr ff mmppuu tintin (Record (Record Pointer) Pointer) v2v2 33tt ququ ỏỏ mmppuu tintin cucu &&ii thth ỡỡ EofEof ()() ss chocho gigi ỏỏ trtr aa llàà FalseFalse .F F. vvàà thotho ỏỏtt khkh ffii vũngvũng ll00pp 3) TrongTrong thõnthõn vũngvũng ll00pp DoDo While While ! ! EofEof ()() bbsstt bubu >>cc phph DDii ccúú cõucõu ll11nhnh SkipSkip ČČ chuychuy nn sangsang mmppuu tintin kk'' ((sausau khikhi xx lðlð xongxong mmppuu tintin hihi 11nn hhàànhnh )) 4) TrongTrong thõnthõn vũngvũng ll00pp ccúú thth  ccúú ll11nhnh Loop/ExitLoop/Exit
  26. 3.3/3.3/ CCuu trtr ỳỳcc ScanScan EndScanEndScan D=ng t?ng quỏt LAu ČB Use False . Scan [For ] True EndScan Use
  27. MMcc ČiČi mm ccWWaa ccuu trtr ỳỳcc ScanScan EndScanEndScan (( LL4à ccuu trtr ỳỳcc ddựựngng ČČ duyduy 11tt quaqua ccỏỏcc mmppuu tintin trờntrờn tt pp CSDLCSDL ČangČang Č2Č2 33cc mmdd rara trongtrong khukhu vv$$cc llààmm vivi 11cc hihi 11nn hhàànhnh 2)2) ScanScan luụnluụn Č2Č2 33cc kk''tt thth ỳỳcc bbddii EndScanEndScan 3)3) TrongTrong ccuu trtr ỳỳcc ScanScan EndScanEndScan khụngkhụng ccúú cõucõu ll11nhnh SkipSkip (( FoxproFoxpro ss tt$$ ČČ>>ngng chuychuy nn sangsang mmppuu tintin kk'' sausau khikhi xx lðlð mmppuu tintin hihi 11nn hhàànhnh )) 4)4) TrongTrong ccuu trtr ỳỳcc ScanScan EndScanEndScan ccúú thth  ccúú ll11nhnh Loop/ExitLoop/Exit
  28. t(t( o o DD ningning xx lðlð trongtrong ccuu trtr ỳỳcc ScanScan EndScanEndScan nhanhnhanh h > ČČ quyquy ČČaanhnh chch `` xx lðlð nhnh ccngng mmppuu tintin thotho DD ČiČi uu kiki 11nn ((TT88cc llàà ).T.>) LZT THC CH\NG C 'U TRC K