Looping statement: – Some statement are used in visual basic programming for repetition. These statement are called looping statement. Visual basic provide a large amount of looping statements. Function of all looping statement are about same. The computer is capable of repeating a group of statement number of times without calling the procedure for each new set of data. The process of repeating a series of statements called looping. Visual basic support the following looping statements. They are Do-Loop, For-Next and While-Wend.
Do-Loop: – This looping statements execute single or block of statements for as long as a condition is true. Visual basic evaluate an expression, and if it is true, the statements are executed. If the expression is false, the program continues and the statement following the loop is executed.
There are two variations of Do-Loop statement and both use the same basic model. A loop can be executed either with WHILE or with UNTIL. If condition is true, loop continued otherwise loop terminated. There are forms of Do-Loop statement. They are –
Do While <Text Expression> Statement Block Loop
Do Until <Text Expression> Statement block Loop
Do Statement Block Loop While <Text Expression>
Do Statement Block Loop Until <Text Expression>
Here, first to forms of Do-Loop statement are called top tested loop and second to forms are called bottom tested loop. Top tested loop is also called pretest and bottom tested loop are called posttest loop.
Basic difference between While and Until that is the keyword while continue the loop until condition is true and keyword until continue the loop until condition is false.
For-Next: – The For-Next is a fixed looping statement. Single or block of statements are executed fixed number of time. The structure of For-Next statement begins with a go-to statement and ends with a next statement. In between are the statement to be executed. Syntax of the for-next statement is –
For <Counter> = <Start> To <End> [Step Increment / Decrement] Statement Block Next [Counter]
The arguments counter, start, end and increment / Decrement are all numeric. The for-to statement specifies the number of passes through the loop. Within this is statement counter is variable whose value begins with start, increase and decrease by numeric value in each time until the loop is executed, until it reached end. Note that the value of counter will be end during the last pass through the loop. The next statement identifies the end of the loop. In executing, visual basic follows this step.
- Sets the counter value = start.
- Test to see if counter is greater than or less than end. If show visual basic exist the loop.
- Execute the statements.
- Increment / Decrement Counter by Specified number Repeats steps Two Through four.
While-Wend: – The While-Wend statement is a conditional looping statement this statement execute single or block of statement while a condition is true. Syntax of the While-Wend statement is –
While <Test Expression> Statement Block Wend
If condition is true, all statements between while and wend are executed. If condition is still true, the process is repeated. If condition become false, the program resume with the student following the wend statement. This While-Wend is analogous to a Do-While-Loop statement.