Do...Loop: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
Brendon (talk | contribs)
Created page with "'''Statement''' <pre> DO [WHILE | UNTIL ''condition''] [''statements''] [EXIT DO] [''statements''] LOOP or DO [''statements''] [EXIT DO] [''statements'']..."
 
No edit summary
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''Statement'''
== Statement ==
 
<pre>
<pre>
DO [WHILE | UNTIL ''condition'']
Do [While | Until condition]
 
   [statements]
   [''statements'']
   [EXIT Do]
 
   [statements]
   [EXIT DO]
Loop
 
</pre>
   [''statements'']
 
LOOP


or
or


DO
<pre>
 
Do
   [''statements'']
   [statements]
 
   [EXIT Do]
   [EXIT DO]
   [statements]
 
Loop [While | Until condition]
   [''statements'']
</pre>


LOOP [WHILE | UNTIL ''condition'']
== Description ==
</pre>


'''Description'''
Do...Loop repeats a block of statements While a given condition is TRUE, or Until a given condition becomes TRUE. The required component, ''condition'', is any expression that can be evaluated as TRUE or FALSE. The optional component, ''statements'', are the statements executed during the body of the loop. Any number of optional Exit Do statements can be used to exit a loop before it is finished. Do...Loop statements can be nested, and any Exit DO statements in a nested loop transfer execution to one level above the loop where the Exit Do occurs.


DO...LOOP repeats a block of statements WHILE a given condition is TRUE, or UNTIL a given condition becomes TRUE. The required component, ''condition'', is any expression that can be evaluated as TRUE or FALSE. The optional component, ''statements'', are the statements executed during the body of the loop. Any number of optional EXIT DO statements can be used to exit a loop before it is finished. DO...LOOP statements can be nested, and any EXIT DO statements in a nested loop transfer execution to one level above the loop where the EXIT DO occurs.
Placing the While/Until clause directly after Do causes ''condition'' to be evaluated before the first loop is executed, if ''condition'' is FALSE statements are never executed; placing the While/Until clause after Loop causes the body of the loop to be executed before ''condition'' is evaluated, statements will always be executed, at least once.


Placing the WHILE/UNTIL clause directly after DO causes ''condition'' to be evaluated before the first loop is executed, if ''condition'' is FALSE statements are never executed; placing the WHILE/UNTIL clause after LOOP causes the body of the loop to be executed before ''condition'' is evaluated, statements will always be executed, at least once.
== Example ==


'''Example'''
<tabber>
JavaScript=
<syntaxhighlight lang="JavaScript">
// Do...Loop Example
/* Do...Loop repeats a block of statements */


<pre>
var Counter;
REM DO...LOOP Example
Counter = 1;
'DO...LOOP repeats a block of statements
//Loop that prints 1 to 5
DIM Counter
while (Counter < 6) {
  NSB.Print("Do While...Loop: " + Counter);
  Counter = Counter + 1;
}
NSB.Print("");
//Infinite loop that uses Break to terminate
Counter = 1000;
do {
  NSB.Print("Infinite Loop: " + Counter);
  if(Counter >= 1000000 )  { break; }
  Counter = Counter * 10;
} while(1);
</syntaxhighlight>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
Rem Do...Loop Example
'Do...Loop repeats a block of statements
Dim Counter
Counter = 1
Counter = 1
'Loop that prints 1 to 5
'Loop that prints 1 to 5
DO WHILE Counter < 6
Do While Counter < 6
   PRINT "DO WHILE...LOOP:", Counter
   Print "Do While...Loop:", Counter
   Counter = Counter + 1
   Counter = Counter + 1
LOOP
Loop
PRINT
Print
'Infinite loop that uses EXIT DO toterminate
'Infinite loop that uses Exit Do to terminate
Counter = 1000
Counter = 1000
DO
Do
   PRINT "Infinite Loop:", Counter
   Print "Infinite Loop:", Counter
   IF Counter >= 1000000 THEN
   If Counter >= 1000000 Then Exit Do
    EXIT DO
   Counter = Counter * 10
   Counter = Counter * 10
LOOP
Loop
</pre>
</syntaxhighlight>
</tabber>


'''Output'''
== Output ==


<pre>
<pre>
DO WHILE...LOOP:    1
Do While...Loop:    1
DO WHILE...LOOP:    2
Do While...Loop:    2
DO WHILE...LOOP:    3
Do While...Loop:    3
DO WHILE...LOOP:    4
Do While...Loop:    4
DO WHILE...LOOP:    5
Do While...Loop:    5
Infinite Loop:      1000
Infinite Loop:      1000
Infinite Loop:      10000
Infinite Loop:      10000
Line 68: Line 85:
</pre>
</pre>


'''Related Items'''
== Related Items ==
 
[[exit|Exit]], [[for each...next|For Each...Next]], [[for...next|For...Next]], [[while...wend|While...Wend]]
 
[[Category:Language Reference]]


[[exit|EXIT]], [[for each...next|FOR EACH...NEXT]], [[for...next|FOR...NEXT]], [[while...wend|WHILE...WEND]]
[[Category:Statements - Flow of control]]

Latest revision as of 14:10, 24 July 2019

Statement

Do [While | Until condition]
  [statements]
  [EXIT Do]
  [statements]
Loop

or

Do
  [statements]
  [EXIT Do]
  [statements]
Loop [While | Until condition]

Description

Do...Loop repeats a block of statements While a given condition is TRUE, or Until a given condition becomes TRUE. The required component, condition, is any expression that can be evaluated as TRUE or FALSE. The optional component, statements, are the statements executed during the body of the loop. Any number of optional Exit Do statements can be used to exit a loop before it is finished. Do...Loop statements can be nested, and any Exit DO statements in a nested loop transfer execution to one level above the loop where the Exit Do occurs.

Placing the While/Until clause directly after Do causes condition to be evaluated before the first loop is executed, if condition is FALSE statements are never executed; placing the While/Until clause after Loop causes the body of the loop to be executed before condition is evaluated, statements will always be executed, at least once.

Example

// Do...Loop Example
/* Do...Loop repeats a block of statements */

var Counter;
Counter = 1;
//Loop that prints 1 to 5
while (Counter < 6) {
  NSB.Print("Do While...Loop: " + Counter);
  Counter = Counter + 1;
}
NSB.Print("");
//Infinite loop that uses Break to terminate
Counter = 1000;
do {
  NSB.Print("Infinite Loop: " + Counter);
  if(Counter >= 1000000 )  { break; }
  Counter = Counter * 10;
} while(1);

Rem Do...Loop Example
'Do...Loop repeats a block of statements
Dim Counter
Counter = 1
'Loop that prints 1 to 5
Do While Counter < 6
  Print "Do While...Loop:", Counter
  Counter = Counter + 1
Loop
Print
'Infinite loop that uses Exit Do to terminate
Counter = 1000
Do
  Print "Infinite Loop:", Counter
  If Counter >= 1000000 Then Exit Do
  Counter = Counter * 10
Loop

Output

Do While...Loop:     1
Do While...Loop:     2
Do While...Loop:     3
Do While...Loop:     4
Do While...Loop:     5
Infinite Loop:       1000
Infinite Loop:       10000
Infinite Loop:       100000
Infinite Loop:       1000000

Related Items

Exit, For Each...Next, For...Next, While...Wend