ooPIC Logo

While Loop Construct

Main Index
Language Idx
Back to top of pageDescription:
 

A While Loop Construct repeatedly executes a group of statements, as long as a given condition is true.

Back to top of pageSyntax:
 The following shows the format of the While Loop Construct syntax.
Visual Basic & BASICC & Java
While <condition>
  <statements>
  [Exit While]
  <statements>
Wend
while (<condition>)
{
  <statements>
  [break;]
  [continue;]
  <statements>
}

The following table lists the elements of the While Loop Construct syntax.
Basic Syntax ElementC & Java Syntax ElementDescription
WhilewhileWhile Loop Construct beginning keyword.
<condition><condition>Numeric expression that evaluates to true or false.
<statements><statements>One or more statements executed while the condition is met.
Exit WhilebreakLoop Termination keyword.  Can occure any number of time whthin the <statements> 
 continueLoop Continuation keyword. Can occure any number of time whthin the <statements>
Wend While Loop Construct ending keyword.

Back to top of pageOperation:
 

Basic, C and Jave Syntax:
When a While Loop Construct is encountered during the program's execution, it evaluates the condition specified in the <condition> element. If the condition is met, the statements within the <statements> element are executed until the end of the While Loop Construct is encountered, at which point the program flow returns to the begining of the While Loop Construct and repeats the process. If the condition is not met or the optional Loop Termination keywords are encountered, execution resumes with the statement following the end of the While Loop Construct.C & Java Syntax only:
If while executing the statements within the <statements> element, a Loop Continuation keyword is encountered, then the program flow skips the remaining statements and resumes just before the ending of the While Loop Construct.

Back to top of pageExample:
 In the following example, a While 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
While A < 100
  <statements>
Wend
While (A < 100){
  <statements>
}

In the following example, a While Loop Construct is shown that executes the statements in <statements1> only if the original value of A is equal to 1, 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 While Loop Construct.
Visual Basic & BASICC & Java
While A = 1
  <statements1>
  Exit While
  <statements2>
Wend
While (A == 1){
  <statements1>
  Break;
  <statements2>
}

In the following example, a While Loop Construct is shown that executes the statements in <statements1> only if the original value of A is Greater than 50, and then repeats the execution of the statements in <statements1> as long as the value of A remains greater than 50, but only executes the statements in <statements2> if the value of B 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 While Loop Construct.
Visual Basic & BASICC & Java
Not posible using only the While Loop Construct in Basic syntax.
To skip the statements within <statements2>, place them within an If Selection Contruct as shown:
While A = 1
  <statements1>
  If B = 1 Then 
    <statements2>
  End If
Wend
While (A > 50){
  <statements1>
  If (B != 1) Continue;
  <statements2>
}

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

Back to top of pageRelated Items:

 For Loop Construct, Do 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.