ChucK : Language Specification > Control Structures

version: 1.5.x.x (numchucks)

< (prev): operators | (up): language specification | (next): manipulating time >

Control Structures

ChucK includes standard control structures similar to those in most programming languages. A condition (of type 'int') is evaluated and then a proceeding block is potentially executed. Blocks are separated either by semicolons or by curly brackets {}.

View sample code for control structures

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.

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)
} 

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'
    <<< foo >>>;
}

for-each

For-each is used to iterate over the contents of an array, using a temporary variable that will take on the respective value of each array element in each iteration of the loop. (Requires chuck-1.5.0.8 or higher.)

// an array
[1,2,3] @=> int array[];
// for each element 'a' in array 'array'
for( int a : array )
{
    // debug-print value of 'a'
    <<< a >>>;
}

// OR over a literal array of value
for( int a : [4,5,6] )
{
    // debug-print value of 'a'
    <<< a >>>;
}

// ALSO can make use the 'auto' type
for( auto a : array )
{
    // 'a' is automatically inferred from context to be of type 'int'
    <<< a >>>;
}

repeat

A loop that iterates a fixed number of times.

// repeat loop
repeat( 4 )
{
    // print now in seconds
    <<< now / second >>>;
    // advance time
    500::ms => now;
}

The repeat loop condition is evaluated immediately (when first encountered) and only once.

// variable
5 => int x

// in the repeat condition, 'x' is evaluated once and only once, at
// the start of the loop; if the value of x is updated subsequently,
// it will not alter the number of repetitions
repeat( x )
{
    // print now in seconds
    <<< now / second >>>;
    // advance time
    500::ms => now;
}

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)
}
< (prev): operators | (up): language specification | (next): manipulating time >