Post Test Loop Do statements Loop Until condition

  • Slides: 9
Download presentation
Post Test Loop Do statement(s) Loop Until condition Code inside “Do Loop Until” executes

Post Test Loop Do statement(s) Loop Until condition Code inside “Do Loop Until” executes at least one time. If the evaluated condition is False, the Loop end and program continue to run. Loop is executed once and then the condition is tested. If it is False, the loop is run again. If it is True, the statements following the Loop statement are executed. Chapter 6 1

Post Test Loop Characteristics • For “Post Test” Loop, Code inside the Loop will

Post Test Loop Characteristics • For “Post Test” Loop, Code inside the Loop will executes at least once no matter the testing Criteria is True or False. Chapter 6 2

Example: Repeat Request Until Proper Response is Given The following Code demonstrate a segment

Example: Repeat Request Until Proper Response is Given The following Code demonstrate a segment of Password Checking Program, which requires user to input the correct password before he can proceed to the rest of the program. Do pass. Word = Input. Box("What is the password? ") pass. Word = pass. Word. To. Upper Loop Until pass. Word = "SHAZAM" Chapter 6 3

Pseudocode and Flowchart for a Post-Test Loop Chapter 6 4

Pseudocode and Flowchart for a Post-Test Loop Chapter 6 4

Comparison Between “Do While Loop” and “Do Loop Until” Do While XXX XXX Loop

Comparison Between “Do While Loop” and “Do Loop Until” Do While XXX XXX Loop Do XXX XXX Loop Until Chapter 6 5

Lab Sheet 6. 2: The Millionaire Calculator txt. Amount btn. Calculate txt. When First,

Lab Sheet 6. 2: The Millionaire Calculator txt. Amount btn. Calculate txt. When First, build the Form with the above layout Chapter 6 6

How the Code Run ? balance = CDbl(txt. Amount. Text) Do balance += 0.

How the Code Run ? balance = CDbl(txt. Amount. Text) Do balance += 0. 06 * balance num. Years += 1 Loop until balance < 1000000 Balance will increase by 6% each time when the loop Program run. This Line of Code prevent the program to become an infinite loop. Chapter 6 7

Lab Sheet 6. 2 : Output Chapter 6 8

Lab Sheet 6. 2 : Output Chapter 6 8

Comments • Be careful to avoid infinite loops – loops that never end. •

Comments • Be careful to avoid infinite loops – loops that never end. • Visual Basic allows for the use of either the While keyword or the Until keyword at the top or the bottom of a loop. • Try using putting While at the bottom or Until at the top of the screen and discover what is the difference. Chapter 6 9