Do Loop: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
  DO [WHILE|UNTIL ''condition1'']
  DO [WHILE|UNTIL ''condition1'']
     [loop body]
     [loop body]
    [exit do]
  LOOP [WHILE|UNTIL ''condition2'']
  LOOP [WHILE|UNTIL ''condition2'']


A '''DO LOOP''' allows you to specify that an application should repeat an statement or set of statements while some '''condition''' remains true and/or until another '''condition''' is true.
A '''DO LOOP''' allows you to specify that an application should repeat a statement or set of statements while some '''condition1''' remains true and/or until another '''condition2''' becomes true.


In the above syntax, '''condition1''', '''condition2''', the '''loop body''', and the '''exit do''' statement are optional, but the use of at least one of them is encouraged for clarity.


In the above syntax, both '''condition1''' and '''condition2''' are optional, but the use of at least one of them is encouraged for clarity.
The '''loop body''' may consist of ''zero'' or more statements. However, the use of at least one statement in the loop body is encouraged for clarity.


The '''loop body''' consists of zero or more statements.
Eventually, the condition(s) '''should''' become false. At this point, the repetition terminates, and the first statement after the DO LOOP executes.


Eventually, the condition(s) '''should''' become false. At this point, the repetition terminates, and the first statement after the DO LOOP executes.
The DO LOOP stops execution in 3 cases:


'''Note''' that if your DO LOOP '''condition''' is never false, your DO LOOP may repeat infinitely. In this case, the program stays ''stuck'' in RUN mode.
# The condition following the WHILE keyword becomes TRUE
# The condition following the UNTIL keyword becomes TRUE
# An EXIT DO statement is encountered


====Examples====
====Examples====
Line 32: Line 37:
  00050 loop
  00050 loop


You may want to combine the use of both WHILE and UNTIL in the same DO LOOP in order to separate two unrelated conditions
You may want to combine the use of both WHILE and UNTIL in the same DO LOOP:
 
00010 do while fkey <> 99                  ! loop will stop if user presses ESC
00020    let kstat$(1)
00030    print "FKEY is now "&str$(FKEY)
00040 loop until fkey = 12                ! loop will stop if user presses F12
 
Results identical to those from the above example may be obtained from the following DO LOOP:
 
00010 do while fkey <> 99 and fkey <> 12                ! loop will stop if user presses ESC or F12
00020    let kstat$(1)
00030    print "FKEY is now "&str$(FKEY)
00040 loop
 
====EXIT DO====
 
00010 do
00020    print 'This loop will stop when you press F1'
00030    let kstat$(1)
00040    if FKEY = 1 then EXIT DO
00050 loop
 
As you can see, the combination of WHILE, UNTIL, and EXIT DO allows for various combinations of DO LOOP conditions.


<noinclude>
<noinclude>
[[Category:Control Structures]]
[[Category:Control Structures]]
</noinclude>
</noinclude>

Revision as of 16:10, 9 February 2012

DO [WHILE|UNTIL condition1]
   [loop body]
   [exit do]
LOOP [WHILE|UNTIL condition2]

A DO LOOP allows you to specify that an application should repeat a statement or set of statements while some condition1 remains true and/or until another condition2 becomes true.

In the above syntax, condition1, condition2, the loop body, and the exit do statement are optional, but the use of at least one of them is encouraged for clarity.

The loop body may consist of zero or more statements. However, the use of at least one statement in the loop body is encouraged for clarity.

Eventually, the condition(s) should become false. At this point, the repetition terminates, and the first statement after the DO LOOP executes.

The DO LOOP stops execution in 3 cases:

  1. The condition following the WHILE keyword becomes TRUE
  2. The condition following the UNTIL keyword becomes TRUE
  3. An EXIT DO statement is encountered

Examples

For example, the loop below will print 3, then double the 3 to 6 and print it, then double the 6 to 12 and print it, and so on until the variable becomes greater than 100. At this point the repetition stops.

00010 let a=3
00020 do until a > 100
00030    print a
00040    let a = 2 * a
00050 loop

You can reach an identical result by using WHILE and reversing the condition from a > 100 to a <= 100:

00010 let a=3
00020 do while a <= 100
00030    print a
00040    let a = 2 * a
00050 loop

You may want to combine the use of both WHILE and UNTIL in the same DO LOOP:

00010 do while fkey <> 99                  ! loop will stop if user presses ESC
00020    let kstat$(1)
00030    print "FKEY is now "&str$(FKEY)
00040 loop until fkey = 12                 ! loop will stop if user presses F12

Results identical to those from the above example may be obtained from the following DO LOOP:

00010 do while fkey <> 99 and fkey <> 12                 ! loop will stop if user presses ESC or F12
00020    let kstat$(1)
00030    print "FKEY is now "&str$(FKEY)
00040 loop

EXIT DO

00010 do
00020    print 'This loop will stop when you press F1'
00030    let kstat$(1)
00040    if FKEY = 1 then EXIT DO
00050 loop

As you can see, the combination of WHILE, UNTIL, and EXIT DO allows for various combinations of DO LOOP conditions.