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 {}
.
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
.
An 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
do {
// your code executes here at least once
} while( condition );
A few more points:
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 until 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:
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 >>>;
}
repeat
A loop that iterates a number of times. Internal state tracks and decrements a count parameter.
// repeat loop
repeat(10)
{
// debug-print value of 'foo'
<<< 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)
}