Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition.

Similar presentations


Presentation on theme: "Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition."— Presentation transcript:

1

2 Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition

3 Chapter 6 - Visual Basic Schneider2 Outline & Objectives u Loop Structure u Elements of a Loop Structure u Processing Lists of Data with Do Loops u Option buttons vs. Check boxes u Multiple forms

4 Chapter 6 - Visual Basic Schneider3 Types of LOOP Structures u Do While ……. Loop u Do Until …… Loop u For …… Next loop

5 Chapter 6 - Visual Basic Schneider4 Basic Definition u Looping : the process of repeating a series of statements as many times as needed. u Looping also called iteration.

6 Chapter 6 - Visual Basic Schneider5 Basic Components of Loops u Loop control variable: A variable used to determine whether a loop will be executed u Loop body: The statement (s) that are executed each time a loop repeats

7 Chapter 6 - Visual Basic Schneider6 The Do While ……. Loop Do While condition is true statement(s) Loop u Compare this to IF …. Then

8 Chapter 6 - Visual Basic Schneider7 Flowchart for a Do While Loop Is the condition true Execute statements within the loop Execute statements that follow the loop Yes No

9 Chapter 6 - Visual Basic Schneider8 The Do While ……. Loop u Is executed as long as the condition is True. u If condition is False then the next statement after the Loop is executed. u Step Demo DoCount

10 Chapter 6 - Visual Basic Schneider9 Controlling Loops u Methods of controlling loops: u Counter-controlled loops u repeat a specific number of times u Event-controlled loops u repeat until something happens in the loop body to change the value of loop control variable.

11 Chapter 6 - Visual Basic Schneider10 Example of event-controlled loops passWord = "" Do While passWord <> "SHAZAM" passWord = UCase(InputBox("What is the password?")) Loop

12 Chapter 6 - Visual Basic Schneider11 Counter-controlled Loops u Is useful when the programmer knows how many times the loop should be executed. u Initialize the counter by setting it to a beginning value before entering the loop. u The counter is increased (or decreased) by the same value during each repetition.

13 Chapter 6 - Visual Basic Schneider12 Example num = 1 Do While num <= 10 picOutput.Print num; num = num + 1 Loop Watch out for infinite loops!

14 Chapter 6 - Visual Basic Schneider13 Do Until ……. Loop u Is executed until the condition becomes True u Any Do While…. Loop can be rewritten as a Do Until ….. Loop

15 Chapter 6 - Visual Basic Schneider14 Example (requires the user to give a password before opening a file) Do passWord = UCase(InputBox("What is the password?")) Loop Until passWord = "SHAZAM“ Demo - CTRL+Break

16 Chapter 6 - Visual Basic Schneider15 Example (years to deplete a saving account) Jane has saved $150,000 for retirement. She is now 65 and wants to draw $15,000 per year to supplement her pension. She is earning interest on her account at the rate of 7.5% per year. How old will Jane be before all her savings are used up? Demo Savings

17 Chapter 6 - Visual Basic Schneider16 Comparing While… and Until Loops u The Do While … Loop executes while the condition is true u The Do Until….. Loop executes until the condition is true u Both can be used to create any type of loop

18 Chapter 6 - Visual Basic Schneider17 Schneider p. 255 - 261

19 Chapter 6 - Visual Basic Schneider18 Processing List of Data with Loops u EOF Function u The EOF function tells us if we have read to the end of a file. u Checks for the end-of-file marker.

20 Chapter 6 - Visual Basic Schneider19 EOF Function u EOF(n) where n is the reference number for the file. u If there are more records to be read, EOF is False. u When the end-of-file marker is reached; EOF becomes True.

21 Chapter 6 - Visual Basic Schneider20 Example of EOF Open “Phone.txt” for Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum picOutput.Print nom, phoneNum Loop Close #1

22 Chapter 6 - Visual Basic Schneider21 Counters and Accumulators u A Counter is a numeric variable that keeps track of the number of items that have been processed in the loop. u An Accumulator is a numeric variable that totals numbers. u Demo Average

23 Chapter 6 - Visual Basic Schneider22 Example: Counter & Accumulator Private Sub cmdAnalyze_Click() Dim numCoins As Integer, sum As Single, value As Single Open "COINS.TXT" For Input As #1 numCoins = 0 sum = 0 Do While Not EOF(1) Input #1, value numCoins = numCoins + 1 sum = sum + value Loop picValue.Print "The value of the"; numCoins; "coins is"; sum; "cents." Close #1 End Sub

24 Chapter 6 - Visual Basic Schneider23 Example of a Data File 1, 1, 5, 10, 10, 25

25 Chapter 6 - Visual Basic Schneider24 Output for the Previous Example The value of the 6 coins is 52 cents.

26 Chapter 6 - Visual Basic Schneider25 For ……... Next Loop u Is used to create a counting loop. u Loop control variable has an initial value. u Loop control variable has a terminating value. u Loop control variable has a Step value.

27 Chapter 6 - Visual Basic Schneider26 Example ( display a table of the first 5 numbers and their square) For i = 1 To 5 picTable.Print i; i ^ 2 Next i Demo counter Initial Value Loop Control variable Terminating value

28 Chapter 6 - Visual Basic Schneider27 Rules for Using For... Next loop u The initial, terminal, and step values cannot be modified in the loop body. u You should never modify the value of the loop control variable in the loop body. u Each For statement must end with a Next statement.

29 Chapter 6 - Visual Basic Schneider28 Example (the step value is negative) For Counter 8 To 1 Step -2 picOutput.Print Counter Next Counter

30 Chapter 6 - Visual Basic Schneider29 Nested Loops For Outer = 1 To 4 For Inner = 1 To 2.. Next Inner Next Outer

31 Chapter 6 - Visual Basic Schneider30 For i = 1 To 10 For j = 1 To 10 picOutput.Print "*"; Next j picOutput.Print Next I What wil this code do?

32 Chapter 6 - Visual Basic Schneider31 Schneider p. 268 – 275 Schneider p. 282 - 289

33 Chapter 6 - Visual Basic Schneider32 Option Buttons u Used in situations where you want to limit the user to only one of two or more related and mutually exclusive choices u Only one in a group can be selected (on) at any one time u When selected, an option button’s Value property contains the Boolean value True; otherwise it contains the Boolean value False

34 Chapter 6 - Visual Basic Schneider33 Option Buttons u Minimum number in an interface is two u Recommended maximum number is seven u A default button should be selected when the interface first appears (use form_load event) u Demo State_opt

35 Chapter 6 - Visual Basic Schneider34 Frame Control u Acts as a container for other controls u Used to visually separate controls from one another u The frame and the controls contained within the frame are treated as one unit u You must use a frame control if you want to have more than one group of option buttons (Demo)

36 Chapter 6 - Visual Basic Schneider35 Check Box u Used in situations where you want to allow the user to select any number of choices from one or more independent and non-exclusive choices u Any number of check boxes can be selected at any one time u When selected, a check box’s Value property contains the number 1 (vbChecked). When unselected, it contains the number 0 (vbUnchecked)

37 Chapter 6 - Visual Basic Schneider36 Check Box u Assign a unique access to each check box u You also can use the spacebar to select/deselect a check box that has the focus u Unlike option buttons, you must test a checkbox when it is clicked to see if it is checked or unchecked

38 Chapter 6 - Visual Basic Schneider37 u Demo Rentals_opt u Demo Math and Math2

39 Chapter 6 - Visual Basic Schneider38 Multiple Forms u Adding a second form u Form load and unload commands u Form hide and show commands u Syntax - referring to controls on other forms u Demo - Order


Download ppt "Chapter 6 - Visual Basic Schneider1 Chapter 6 Repetition."

Similar presentations


Ads by Google