Saturday 8 September 2012

Looping Control Structures


A looping structure allows a set of statements to be repeatedly executed. Visual Basic provides as many as five looping structures. The choice of the looping structure mainly depends on the requirement. If for example, you have to execute a set of statements for 10 times then For loop is ideal, if you have to execute statement until a condition is satisfied then Loop Until is to be used. In the following sessions I will discuss more about all the available looping structures in Visual Basic 6.0.

¨       DO WHILE … LOOP
¨       DO UNTIL … LOOP
¨       DO … LOOP WHILE
¨       DO … LOOP UNTIL
¨       FOR ... NEXT

The following sections discuss about each of the available looping structrues.

Do While … Loop
Executes given set of statements as long as the condition is true. Once condition is false then loop is terminated and control is transferred to the immediate next statement after the loop.

Do While condition
    Statements
Loop

The following example will display numbers from 1 to 10.

I = 1
Do while I <= 10
    Print I
    I  = I + 1
Loop

Do Until… Loop
Executes given set of statements until the condition is true. That means the statements are executed as long as condition is false. Once condition is true, loop is terminated and control is transferred to the immediate next statement after the loop.

Do Until condition
    Statements
Loop

The following example will display numbers from 1 to 10.

I = 1
Do until I > 10
    Print  I
    I  = I + 1
Loop

Do … Loop While
Executes given set of statements as long as the condition is true. But in this, condition is checked after the loop is executed. As a result, the statements are executed at least for once.  Except the difference between the minimum number of times the loop is executed, where for Do .. While it is 0 and for Do..Loop While it is 1, both these loops work identically.

do
    Statements
loop while condition

The following example displays numbers from 1 to 10.

I = 1
Do
    Print I
    I  = I + 1
Loop while I  <= 10

Do… Loop Until
This is same as Do … Loop While except that it executes statements until the condition is true. In other words, as long as the condition is false.

do
    Statements
loop until condition

The following example displays numbers from 1 to 10.

I = 1
Do
    Print  I
    I  = I + 1
Loop until  I > 10

For ... Next Loop
This loop is used where the number of repetitions is known at the time of writing the program. For example, if you want to print numbers 1 to 10 then you know that the loop is to be repeated for 10 times. This loop is ideal in cases like this. Another advantage of this loop is; counter is automatically incremented or decremented by the loop.  It is also possible to control the increment using Step option.

For counter = start to end  [step value]
     Statements
Next  [counter]

Counter
Is an integer variable whose value is initially set to start and then incremented after each iteration of the loop. When its value exceeds end value then loop is terminated.
Start
Specifies the value that is to be assigned to counter at the beginning of the loop. It may be any valid Visual Basic expression.
End
Specifies the ending value. Once counter exceeds this value the loop is terminated. This will be more than start for normal loop and less than start in reverse loop.
Step
This entry is optional. If not given, it defaults to 1. This specifies the value by which counter is to be incremented at the end of each repetition. This should be negative, if start value is more than end value (reverse loop).
Statements
The statements to be executed.
Next
Identifies the ending of the statements to be executed. Upon reaching this, For increments the counter and checks whether counter exceeds end value. If not it enters into next iteration otherwise loop will be terminated. Though giving counter after Next is optional, it may make especially nested loops clear.
In the following example for loop is used to display only even numbers in the range 50 to 100.

For  c = 50 to 100 step 2
     Print c
Next  c

Initially C is set to 50. After each iteration C is incremented by 2. When C exceeds 100 the loop is terminated, otherwise the value of C is printed.

The following is an example to print numbers from 10 to 1.

For c = 10 to 1  step  -1
    Print c
Next  c

Here, as step value is –1 after each iteration counter (C) is decremented by 1. If C goes below 1 then loop is terminated.

Exit Do Statement
This statement is used to terminate DO loops from within the loop. Normally a loop is terminated or continued based on the result of the condition given in loop. But in some occasions, it is required to check for a condition within the loop and terminate loop depending upon the result of the condition. 

In the following example, the loop is repeated until user entered 10 numbers or until a negative number is entered.

Cnt = 1
Do while cnt <= 10
       ‘ take value and place it in variable N
      
       if   N < 0 then
              exit do
      end if
     
loop

Exit For Statement
This is conceptually same as Exit do, but this is used to terminate a FOR loop.

GoTo Statement
This is used to transfer control to the specified label. Goto is NOT recommended, as it is difficult to follow the logic of the program if control is transferred from one place to another using GoTo. In other words GoTo spoils the structure of the program. However in some cases GoTo might come out as better choice than the remaining.

GoTo label

Here is an example, when control is transferred to the beginning of the procedure if user responds by entering you.


' It is an imaginary procedure to demonstrate how to use GOTO
Sub Procedure ()
Retry:
      Statement
      If resp = “YES” Then
            GoTo retry
      End if
End sub

No comments:

Post a Comment