1 / 22

CS 4 Intro to Programming using Visual Basic

Chapter 6 - VB 2005 by Schneider. 2. Chapter 6

mrinal
Download Presentation

CS 4 Intro to Programming using Visual Basic

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. CS 4 Intro to Programming using Visual Basic Do Loops Patchrawat Uthaisombut University of Pittsburgh 1

    2. Chapter 6 - VB 2005 by Schneider 2 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

    3. How to write the following program? The user inputs a number x. The program displays the multiplication table for x. P. Uthaisombut 3

    4. 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 4

    5. User Interface Design P. Uthaisombut 5

    6. Expected Results P. Uthaisombut 6

    7. 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 7

    8. Technical Issues How do we add exactly 12 lines into the List Box? P. Uthaisombut 8

    9. One solution Put 12 lstTable.Items.Add(…) in the code But there is a better solution … P. Uthaisombut 9

    10. Chapter 6 - VB 2005 by Schneider 10 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.

    11. Chapter 6 - VB 2005 by Schneider 11 Do Loop Syntax Do While condition statement(s) Loop A Do statement precedes the sequence of statements, and a Loop statement follows the sequence of statements. The condition, preceded by either the word “While” or the word “Until”, follows the word “Do” or the word “Loop”. A Do statement precedes the sequence of statements, and a Loop statement follows the sequence of statements. The condition, preceded by either the word “While” or the word “Until”, follows the word “Do” or the word “Loop”.

    12. Chapter 6 - VB 2005 by Schneider 12 Pseudocode and Flow Chart for a Do Loop

    13. Simple Illustration of Do Loop When the user click the button, display 1, 2,…, 7 on the List Box. P. Uthaisombut 13

    14. Chapter 6 - VB 2005 by Schneider 14 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 Num is the "loop control variable" because based on the value of num, the loop will either continue or stop. Note that the value of the loop control variable must change inside the loop.Num is the "loop control variable" because based on the value of num, the loop will either continue or stop. Note that the value of the loop control variable must change inside the loop.

    15. 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 15

    16. Chapter 6 - VB 2005 by Schneider 16 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

    17. Chapter 6 - VB 2005 by Schneider 17 Post Test Loop Do statement(s) Loop Until condition If condition is true, then the program continues with the line after the Loop statement. If condition is false, then the entire process is repeated beginning with the Do statement. In other words, the statements inside the loop are executed once and then are repeatedly executed until the condition is true.If condition is true, then the program continues with the line after the Loop statement. If condition is false, then the entire process is repeated beginning with the Do statement. In other words, the statements inside the loop are executed once and then are repeatedly executed until the condition is true.

    18. Chapter 6 - VB 2005 by Schneider 18 Example: Repeat Request Until Proper Response is Given Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = "SHAZAM"

    19. Chapter 6 - VB 2005 by Schneider 19 Pseudocode and Flowchart for a Post-Test Loop

    20. Chapter 6 - VB 2005 by Schneider 20 Example 4: Form

    21. Chapter 6 - VB 2005 by Schneider 21 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

    22. Chapter 6 - VB 2005 by Schneider 22 Example 4: Output

    23. Chapter 6 - VB 2005 by Schneider 23 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.

More Related