FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

chuck update

Control Structures

Control Structures

ChucK includes standard control structures similar to those in most programming languages. Control structures make use of blocks of code. A code block is delineated either by semicolons or by curly brackets.

if / else

The if statement executes a block if the condition is evaluated as non-zero.
if( condition )
{
  // insert code here
}
In the above code, "condition" is any expression that evaluates to an int.
The else statement can be put after the if block to handle the case where the condition evaluates to 0.
if( condition )
{
  // your code here
}
else
{
  // your other code here
}
If statements can be nested.

repeat

The repeat statement creates a loop that repeats a certain number of times. This number is evaluated only once (right before the first repetition).

repeat(3)
{
  //this code will run three times
}

while

The while statement is a loop that repeatedly executes the body as long as the condition evaluates as non-zero.

// here is an infinite loop
while( true )
{
  // your code loops forever!
  // (sometimes this is desirable because we can create
  // infinite time loops this way, and because we have
  // concurrency)
}

do

The while loop will first check the condition, and executes the body as long as the condition evaluates as non-zero. To execute the body of the loop before checking the condition, you can use a do/while loop. This guarantees that the body gets executed as least once.

do
{
  // your code executes here at least once
}
while( condition );

A few more points:

  •  while statements can be nested.
  •  see break/continue for additional control over your loops

until

The until statement is the opposite of while, semantically. A until loop repeatedly executes the body until the condition evaluates as non-zero.

// an infinite loop
until( false )
{
  // your great code loops forever!
}

The while loop will first check the condition, and executes the body as long as the condition evaluates as zero. To execute the body of the loop before checking the condition, you can use a do/until loop. This guarantees that the body gets executed as least once.

do
{
  // your code executes here at least once
}
until( condition );

A few more points:

  •  until statements can be nested.
  •  see break/continue for additional control over your loops

for

A loop that iterates a given number of times. A temporary variable is declared that keeps track of the current index and is evaluated and incremented at each iteration.

// for loop
for( 0 => int foo; foo < 4 ;⁞ foo++ )
{
  // debug-print value of ’foo’
  <<>>>;
}

break / continue

Break allows the program flow to jump out of a loop.

 // infinite loop
while( 1 )
{
  if( condition )
    break;
}

Continue allows a loop to continue looping but not to execute the rest of the block for the iteration where continue was executed.

// another infinite loop
while( 1 )
{
  // check condition
  if( condition )
    continue;

  // some great code that may get skipped (if continue is taken)
}

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.