While Loop

Home • Gallery • Tutorials • Download • Purchase • Site Map
 

While Loop Syntax

A while loop is used to execute a set of Fractal Science Kit fractal program statements while a conditional expression is true. Once the expression evaluates to false, execution continues with the statement following the while loop.

The syntax of the while loop is:

while (<Condition>) {
  <Statements>
}

Clearly, the set of statements inside the while block must change the outcome of the conditional expression or an infinite loop will result.

The following example sums the 1st 10 items in an array:

sum = 0
i = 0

while (i < 10) {
  sum += array[i]
  i += 1
}

This could have been achieved using the more concise for loop as:

sum = 0

for (i = 0, i < 10, i += 1) {
  sum += array[i]
}

Inside the while loop, you can include a break statement or a continue statement to affect the execution path. A break statement breaks out of the loop, continuing execution with the statement following the while loop. A continue statement skips the remainder of the loop and begins another iteration, checking the conditional expression and exiting the loop if false.

 

Copyright © 2004-2017 Hilbert, LLC
All rights reserved