230 likes | 391 Views
CS 4 Intro to Programming using Visual Basic. Do Loops Patchrawat Uthaisombut University of Pittsburgh. based on lecture notes by D. Schneider. Chapter 6 – Repetition. 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan.
E N D
CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat UthaisombutUniversity of Pittsburgh based on lecture notes by D. Schneider
Chapter 6 – Repetition • 6.1 Do Loops • 6.2 Processing Lists of Data with Do Loops • 6.3 For...Next Loops • 6.4 A Case Study: Analyze a Loan Chapter 6 - VB 2005 by Schneider
How to write the following program? • The user inputs a number x. • The program displays the multiplication table for x. P. Uthaisombut
User Interface Design • Use a Text Box to accept a number • Use a Button to accept the “print table” command from the user • Use a List Box to show the multiplication table. • Put a Label near the Text Box to give a brief instruction to the user P. Uthaisombut
User Interface Design P. Uthaisombut
Expected Results P. Uthaisombut
Technical issues • When does the program respond to the user? • How do we convert text input from the Text Box into an integer? • How do we add a line into a List Box? • How do we construct the right string on each line of the List Box? (How do we construct the string “5 * 1 = 5”, etc. ?) P. Uthaisombut
Technical Issues • How do we add exactly 12 lines into the List Box? P. Uthaisombut
One solution • Put 12 lstTable.Items.Add(…) in the code • But there is a better solution … P. Uthaisombut
6.1 Do Loops • A loop is one of the most important structures in programming. • Used to repeat a sequence of statements a number of times. • The Do loop repeats a sequence of statements either as long as or until a certain condition is true. Chapter 6 - VB 2005 by Schneider
Do Loop Syntax Condition is tested, If it is True, the loop is run. If it is False, the statements following the Loop statement are executed. Do Whilecondition statement(s) Loop These statements are inside the body of the loop and are run if the condition above is True. Chapter 6 - VB 2005 by Schneider
Pseudocode and Flow Chart for a Do Loop Chapter 6 - VB 2005 by Schneider
Simple Illustration of Do Loop • When the user click the button, display 1, 2,…, 7 on the List Box. P. Uthaisombut
Example 1 Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click 'Display the numbers from 1 to 7 Dim num As Integer = 1 Do While num <= 7 lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num Loop End Sub Chapter 6 - VB 2005 by Schneider
Back to our multiplication table Public Class Form1 Private Sub btnDisplay_Click(…) Handles btnDisplay.Click Dim x As Integer Dim y As Integer x = CInt(txtInput.Text) y = 1 Do While y <= 12 lstTable.Items.Add(x & " * " & y & " = " & x * y) y += 1 Loop End Sub End Class P. Uthaisombut
Example: Repeat Request as Long as Response in Incorrect Dim passWord As String = "" Do While passWord <> "SHAZAM" passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop passWord is the loop control variable because the value stored in passWord is what is tested to determine if the loop should continue or stop. Chapter 6 - VB 2005 by Schneider
Post Test Loop Do statement(s) Loop Untilcondition 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 - VB 2005 by Schneider
Example: Repeat Request Until Proper Response is Given Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = "SHAZAM" Chapter 6 - VB 2005 by Schneider
Pseudocode and Flowchart for a Post-Test Loop Chapter 6 - VB 2005 by Schneider
Example 4: Form txtAmount txtWhen Chapter 6 - VB 2005 by Schneider
Example 4: Code Private Sub btnCalculate_Click(...) Handles btnCalculate.Click Dim balance As Double, numYears As Integer balance = CDbl(txtAmount.Text) Do While balance < 1000000 balance += 0.06 * balance numYears += 1 Loop txtWhen.Text = "In " & numYears & _ " years you will have a million dollars." End Sub Chapter 6 - VB 2005 by Schneider
Example 4: Output Chapter 6 - VB 2005 by Schneider
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. • This textbook will use only While at the top and only Until at the bottom. Chapter 6 - VB 2005 by Schneider