1 / 8

Pre-test Loops

Pre-test Loops. False. condition. Steps to repeat if False. True. True. condition. Steps to repeat if True. False. Do While condition Steps to repeat if condition is true Loop. Do Until condition Steps to repeat if condition is false Loop. Post-test Loops.

kasie
Download Presentation

Pre-test Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pre-test Loops False condition Steps to repeat if False True True condition Steps to repeat if True False • Do While condition • Steps to repeat if condition is true • Loop • Do Until condition • Steps to repeat if condition is false • Loop

  2. Post-test Loops Steps to repeat if False False condition True Steps to repeat if True True condition False • Do • Steps to repeat if condition is true • Loop While condition • Do • Steps to repeat if condition is false • Loop Until condition

  3. When are conditional loops used? • Execute processing steps repeatedly • One or more times: post-conditional loops Do Do Loop body Loop body Loop While condition Loop Until condition • Zero or more times: pre-conditional loops Do While condition Do Until condition Loop body Loop body Loop Loop • Data Validation • Prevent user from moving on until valid value has been entered

  4. FactNbr = CInt(txtFactNbr.Text) Factorial = 1 Multiplier = FactNbr Do While Multiplier > 0 Factorial = Factorial * Multiplier Multiplier = Multiplier - 1 Loop FactNbr = CInt(txtFactNbr.Text) Factorial = 1 Multiplier = FactNbr Do Until Multiplier <= 0 Factorial = Factorial * Multiplier Multiplier = Multiplier - 1 Loop Pre-test Loop to Find Factorial What is the minimum number of times the loop will repeat? What happens if txtFactNbr contains zero?

  5. Dealer = 0 User = 0 Do Call PlayGame(Dealer, User) Again = InputBox(“Play again?”) Loop Until Ucase(Again) = “NO”Or Again = “” Dealer = 0 User = 0 Do Call PlayGame(Dealer, User) Again = InputBox(“Play again?”) Loop While Ucase(Again) <> “NO”And Again <> “” Post-test Loop to Play a Game What is the minimum number of times the game will be played? What happens if the user says No the first time? What happens if the user says NO? What happens if the user clicks the Cancel button ?

  6. Counting: Pre-Test Conditional Loop Initialize counter Set Count = StartVal Count > EndVal? Check if done counting False True Steps to process repeatedly Steps to process after loop ends Count = Count + IncrVal Update counter

  7. For/Next Loops Set Count = StartVal False Count > EndVal? True Steps to process repeatedly Steps to process after loop ends Count = Count + IncrVal Three Characteristics of a Counting Loop • Initialize counter • Increment counter in each loop iteration • Test counter before next iteration against final value ForCount = StartValToEndValStepIncrVal steps to process repeatedly NextCount steps to process after loop ends

  8. For Next Loop Example FactNbr = CInt(txtFactNbr.Text) Factorial = 1 For Multiplier = FactNbr To 1 Step -1 Factorial = Factorial * Multiplier Next Multiplier

More Related