Why is loop structure needed




















Many programming tasks are repetitive, having little variation from one item to the next. A loop executes the same section of program code over and over again, as long as a loop condition of some sort is met with each iteration. This section of code can be a single statement or a block of statements a compound statement. Loops and Using Loops.

A loop essentially shortens the number of statements required. Looping provides a way to determine the number of repetitions automatically, based on evaluations made in your script. You use the While-EndWhile statement structure to perform Looping. A While loop consists of three parts:. JAWS performs all statements within the boundaries of the While and EndWhile key words repeatedly until the condition in the While statement becomes false. Once the value of iCount becomes equal to 5, the loop ends and no further processing occurs.

Another method of performing looping in your scripts is by using the For-EndFor looping statements. Similar to a While loop, a For loop consists of three parts: the keyword For that starts the loop, the condition being tested, and the EndFor keyword that terminates the loop.

JAWS performs all statements found in the boundaries of the loop as long as the loop condition is true. An example of the syntax of a For loop follows. Note that the step can be a negative value. When X reaches the end of the range in the range in the example above , the loop will cease to execute, and the program will continue to the code beyond the next statement.

It is possible to edit the value of the counter variable within a for.. While you may on rare occasions find good reasons to edit the counter in this manner, the example above illustrates one potential pitfall:. Because X is set to 7 at the end of every iteration, this code creates an infinite loop.

To avoid this and other unexpected behavior, use extreme caution when editing the counter variable! It is not required by the compiler that you specify the name of the loop variable in the Next statement but it will be checked by the compiler if you do, so it is a small help in writing correct programs.

Another very common situation is the need for a loop which enumerates every element of a list. The following sample code shows you how to do this:.

The list is commonly a Collection or Array, but can be any other object that implements an enumerator. Note that the iterating variable has to be either a Variant , Object or class that matches the type of elements in the given list. Do loops are a bit more flexible than For loops, but should generally only be used when necessary. Do loops appears in the following formats:. While loops both do while and loop while will continue to execute as long as a certain conditional is true.

An Until loop will loop as long as a certain condition is false, on the other hand. The only difference between putting either While or Until in the Do section or the Loop section, is that Do checks when the loop starts, and Loop checks when the loop ends. An example of a basic loop is as follows:. This loop will print hello several times, depending on the initial value of x.

As you may have noticed, Do loops have no built in counters. You can repeat the statements in a loop structure until a condition is True , until a condition is False , a specified number of times, or once for each element in a collection. The following illustration shows a loop structure that runs a set of statements until a condition becomes true:.

The While End While construction runs a set of statements as long as the condition specified in the While statement is True. For more information, see While End While Statement.

The Do Loop construction allows you to test a condition at either the beginning or the end of a loop structure.



0コメント

  • 1000 / 1000