ooPIC Logo

Do Loop Construct

Main Index
Language Idx
Back to top of pageDescription:
 A Do Loop Construct repeatedly executes a group of statements, as long as a given condition is true.

In Basic syntax, the given condition is optional, and may be placed at the beginning of the loop where it would be used to specify when, if ever, to enter the loop, or the end of the loop where it would be used to specify when, if ever, to exit the loop. In addition, both a beginning condition and an ending condition may be used on the same loop.

In C and Java syntax, the given condition is not optional and must be placed at the end of the loop where it is used to specify when, if ever, to exit the loop.

Back to top of pageSyntax:
 The following shows the format of the Do Loop Construct syntax.
Visual Basic & BASICC & Java
Do [While|Until <conditionB>]|[<val> Times]
  <statements>
  [Exit Do]
  [Continue;]
  <statements>
Loop [While|Until <conditionA>]
Do{
  <statements>
  [break;]
  [continue;]
  <statements>
} while <conditionA>;

The following table lists the elements of the Do Loop Construct syntax.
ElementDescription
BasicC & Java
DodoWhile Loop Construct beginning keyword.
<conditionB> Numeric expression that evaluates to True or False before the <statements> are executed.
<conditionA><conditionA>Numeric expression that evaluates to True or False after the <statements> are executed.
<statements><statements>One or more statements executed if the conditions of the evaluations are met.
WhilewhileCondition prefix.
Until Condition prefix (inverting)
Exit DobreakLoop Termination keyword. Can occur any number of times within the <statements>.
ContinuecontinueLoop Continuation keyword. Can occur any number of times within the <statements>.
Loop Do Loop Construct ending keyword.
val Numeric expression that specifies the number of times to loop.
Times Number of times to loop keyword.

Back to top of pageOperation:
 Visual Basic and BASIC Syntax:
When a Do Loop Construct is encountered during the program's execution, it evaluates the condition specified in the <conditionB> element. If the condition is met or it is omitted, then the statements within the <statements> element are executed until the "Loop" keyword is encountered at which point the condition specified in <conditionA> is evaluated. If that condition is met or it is omitted, then the program flow returns to the beginning of the Do Loop Construct and the process repeats. If either condition is not met in its evaluation, then the program flow skips to the statement following the Do Loop Construct and the program's execution resumes.

If during the program's execution of the statements within the <statements> element, the optional Exit Do keywords are encountered, then the program flow skips to the statement following the Do Loop Construct and the program's execution resumes.

The While and Until keywords dictate how the conditions are evaluated. If the While keyword is used, the condition is evaluated normally. If the Until keyword is used, the Boolean value of the condition's evaluation is reversed.

If both conditions are omitted, the statements within the <statements> elements will be executed indefinitely.

In addition to conditional loops, the Do Loop Construct can specify the number of times to loop by providing a numeric expression after the keyword Do and followed by the keyword Times.

C and Java syntax:
When a Do Loop Construct is encountered during the program's execution, the statements within the <statements> element are executed. After which, the condition specified by the <conditionA> element is evaluated. If the condition is met then the program flow returns to the beginning the Do Loop Construct and repeats the process. If the condition is not met then the program's execution resumes with the statement following the Do Loop Construct.

If during the program's execution of the statements within the <statements> element, the optional break keyword is encountered, then the program flow skips the remaining statements and resumes with the statement following While Loop Construct.

If during the program's execution of the statements within the <statements> element, the optional continue keyword is encountered, then the program flow skips the remaining statements within the <statements> and resumes just before the ending of the While Loop Construct.

Back to top of pageExample:
 In the following example, a Do Loop Construct is shown that unconditionally executes the statements in <statements> once, and then repeats the execution of the statements in <statements> while a condition is true.
Visual Basic & BASICC & Java
Do
  '<statements>
Loop While A < 100
Do{
  //<statements>
} While (A < 100);

In the following example, a Do Loop Construct is shown that unconditionally executes the statements in <statements> once, and then repeats the execution of the statements in <statements> until a condition is true.
Visual Basic & BASICC & Java
Do
  '<statements>
Loop Until A > 10
Not possible using the Do Loop Construct in strict C and Java syntax.  A work-around would be to invert the condition and loop while the inverted condition is true as shown:
Do{
  //<statements>
} While (A <= 10);

In the following example, a Do Loop Construct is shown without a condition, which repeatedly executes the statements in <statements> indefinitely.
Visual Basic & BASICC & Java
Do
  '<statements>
Loop
Not possible using the Do Loop Construct in strict C and Java syntax.  A work-around would be to supply a condition of "1" and loop while the condition is true as shown:
Do{
  //<statements>
} While (1);

In the following example, a Do Loop Construct is shown that executes the statements in <statements> only if the original value of A is less than 100 and then repeats the execution of the statements in <statements> as long as the value of A remains less than 100.
Visual Basic & BASICC & Java
Do While A < 100
  '<statements>
Loop
Not possible using the Do Loop Construct in strict C and Java syntax.  A work-around would be to use the While Loop Construct instead.

In the following example, a Do Loop Construct is shown that executes the statements in <statements1>, but never gets to execute the statements in <statements2> because a Loop Termination keyword was encountered which caused the program flow to jump to the ending of the Do Loop Construct.
Visual Basic & BASICC & Java
Do
  '<statements1>
  Exit Do
  '<statements2>
Loop
Do{
  //<statements1>
  Break;
  //<statements2>
} While (1);

In the following example, a Do Loop Construct is shown that repeats the execution of the statements in <statements1>, but only executes the statements in <statements2> if the value of A is equal to 1, because a Loop Continuation keyword was encountered which caused the program flow to skip past all the remaining statements in the Do Loop Construct.
BASICC & Java
Do
  '<statements1>
  If A <> 1 Then Continue
  '<statements2>
Loop
Do{
  //<statements1>
  If (A!=1) Continue;
  //<statements2>
} While (1);
Visual Basic 
Not possible using only the Do Loop Construct in strict Visual Basic syntax.  A work-around would be to use an If  Construct as shown:
Do
  '<statements1>
  If A = 1 Then
    '<statements2>
  End If
Loop
 

In the following example, a Do Loop Construct is shown that executes the statements in <statements> 12 times using a counter variable.  NOTE: The For Loop Construct is better suited for the conditions in this example.
Visual Basic & BASICC & Java
A = 1
Do
  '<statements>
  A = A + 1
Loop While A <= 12
A = 1
Do{
  //<statements>
  A++
} While (A <= 12);

In the following example, a Do Loop Construct is shown that executes the statements in <statements> 12 times without a counter variable.
Visual Basic & BASICC & Java
Do 12 times.
  '<statements>
Loop 
Not possible using the Do Loop Construct in strict C and Java syntax.  A work-around would be to use the For Loop Construct instead.

Back to top of pageRelated Items:

 For Loop Construct, While Loop Construct
Back to top of pageVersion History and Bug List:
 Firmware Ver A1: Introduced.

Bugs: No known bugs.


 

ooPIC Compiler Ver 6.0 (c) Copyright 1997 - 2007 Savage Innovations, LLC.