Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Similar presentations


Presentation on theme: "© 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach."— Presentation transcript:

1 © 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Pre-test Loops condition True False Steps to repeat if True condition False True Steps to repeat if False Do While condition Steps to repeat if condition is true Loop Do Until condition Steps to repeat if condition is false Loop

2 © 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Post-test Loops condition False True Steps to repeat if False condition True False Steps to repeat if True Do Steps to repeat if condition is true Loop While condition Do Steps to repeat if condition is false Loop Until condition

3 © 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach When are conditional loops used? n Execute processing steps repeatedly i One or more times: post-conditional loopsDo Loop body Loop While conditionLoop Until condition i Zero or more times: pre-conditional loops Do While conditionDo Until condition Loop body Loop n Data Validation i Prevent user from moving on until valid value has been entered

4 © 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Pre-test Loop to Find Factorial 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 What is the minimum number of times the loop will repeat? What happens if txtFactNbr contains zero?

5 © 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Post-test Loop to Play a Game 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 <> “” 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 © 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach False True Steps to process after loop ends Steps to process repeatedly Count > EndVal? Set Count = StartVal Count = Count + IncrVal Check if done counting Update counter Initialize counter Counting: Pre-Test Conditional Loop

7 © 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For/Next Loops Three Characteristics of a Counting Loop i Initialize counter i Increment counter in each loop iteration i Test counter before next iteration against final value For Count = StartVal To EndVal Step IncrVal steps to process repeatedly Next Count steps to process after loop ends False True Steps to process after loop ends Steps to process repeatedly Count > EndVal? Set Count = StartVal Count = Count + IncrVal

8 © 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For Next Loop Example FactNbr = CInt(txtFactNbr.Text) Factorial = 1 For Multiplier = FactNbr To 1 Step -1 Factorial = Factorial * Multiplier Next Multiplier


Download ppt "© 1999, by Que Education and Training, Chapter 7, pages 347-366 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach."

Similar presentations


Ads by Google