ChucK初步(6)

control structures

  • if / else
  • while & do…while…
  • until
  • for
  • break / continue

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(花括号).

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 & do…while…

// 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 {
    // 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!
}

A few more points:

  • while 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>>>;
}

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)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值