For Loop

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

For Loop Syntax

A for 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 for loop.

The syntax of the for loop is:

for (<Initialization>, <Condition>, <Iteration>) {
  <Statements>
}

This is equivalent to the following:

<Initialization>

while (<Condition>) {
  <Statements>
  <Iteration>
}

First the initialization statement is executed. Then a block of statements is executed for as long as the conditional expression is true. The block of statements is composed of the statements inside the for block, followed by the iteration statement given in the for loop.

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

sum = 0

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

Inside the for 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 for loop. A continue statement skips the remaining statements in the body of the loop, and begins another iteration, executing the iteration statement given in the for loop, checking the conditional expression, and exiting the loop if false.

 

Copyright © 2004-2017 Hilbert, LLC
All rights reserved