The Iteration Control Structures in V-Basic

By Tracy Lewis,2014-05-02 18:43
11 views 0
The Iteration Control Structures in V-Basic

    The Iteration Control Structures in V-Basic

    (Well, at least 3 of them!)


    Control Arrays

    1. For loop an automatic counting loop

    ; Carries out 3 tasks:

    o Initialization

    o Testing the control variable

    o Incrementing/decrementing the control variable

    ; General syntax

    FOR control_variable = start_value TO end_value [STEP step_value]

    Statements to be executed in loop

     Next control_variable

; Examples:

    For x = 1 to 70 Step 3 Will print 1, 4, 7, 10,

     Print x etc. in a column directly

    Next x on the form!

    For J = 5 to 2 Step 1

     Print J Will add up 5, 4, 3, & 2

    Sum = sum + J & print each number on

    Next J the form

     Will print the numbers For C = 0 to 6 0, 1, 2, 3, 4, 5, 6 on the Print C form (step defaults to Next C +1)

    2. Do While / loop a pretest loop

    ; Only carries on a loop while the condition stays true

    ; May never execute the loop because the first time the condition may be false

    ; General Syntax

    DO WHILE condition

     Statements to execute


    ; Examples

    X = 5

    Do While X <= 10 Prints 5, 6, 7, 8, 9, 10 on Print x the form. X = X + 1


    X = 5 Produces an infinite Do while X <= 10 loop printing 5 over Print X and over! Why?? Loop

    R = 5

    Prints 5,4,3,2,1 on the Do while R >0

    form. Print R

     R = R 1


    3. Do / Loop Until a posttest loop

    ; Only carries on the loop when the condition is false, once it is true the loop stops ; Does not check the condition until the end of the loop; therefore, will always

    execute the loop at least once

    ; General Syntax


     Statements to execute

    LOOP UNTIL condition

    ; Examples

    Prints 5,6,7,8,9 & 10 on X = 5

    form. Do

     Print X

     X = X + 1

    Loop Until X > 10

    X = 5 Prints 5 on the

    Do form…notice the logic is

     Print X not too good here!

     X + X + 1

    Loop until X > 4

    4. Control Arrays in V-Basic

    ; a group of controls of the same type that have the same name and share the same

    set of even procedures

    ; VB assigns an index to each of the controls in the array, with the first index of 0. ; There are two ways to create a control array:

    1. If the controls are already on the form, you can give them the same name;

    then VB will ask if you want to create a control array…say Yes.

    2. If controls are not on form, place the first control on form, then set its

    properties appropriately, then copy the control to the clipboard. Next,

    paste the desired number of controls on the form. When asked if you want

    to create a control array…say Yes.

    ; Arrays whose contents are related by their positions in the arrays (by their index)

    are called parallel arrays.

     txtEx(0) OptEx(0)

     OptEx(1) txtEx(1) OptEx(2) txtEx(2)

    ; Example code to use parallel arrays: When an option button is selected, the

    corresponding textbox will get the focus.

    Private Sub optEx_Click(Index as Integer)


    End Sub

Report this document

For any questions or suggestions please email