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( condition ) { // insert code here }In the above code, "condition" is any expression that evaluates to an int.
if( condition ) { // your code here } else { // your other code here }If statements can be nested.
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 }
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:
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:
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 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.