control structures
if / elsewhile & do…while…untilforbreak / continueChucK 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(花括號).
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. (條件是一個值為整數(shù)的表達式)
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(嵌套).
A few more points:
while
statements can be nested.see break/continue
for additional(附加的) control over your loops 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 loopuntil( false ){ // your great code loops forever!}A few more points:
while
statements can be nested.see break/continue
for additional(附加的) control over your loops A loop that iterates(迭代) a given number of times.(迭代給定次數(shù)) A temporary(暫時的) variable(變量) is declared that keeps track(跟蹤) of the current index and is evaluated and incremented at each iteration(迭代).
// for loopfor( 0 => int foo; foo < 4 ; foo++ ){ // debug-print value of 'foo' <<<foo>>>;}Break
allows the program flow to jump out of a loop.
Continue
allows a loop to continue looping but not to execute(實行) the rest of the block for the iteration where continue was executed.(跳過當前的循環(huán))
新聞熱點
疑難解答