Cava loop structures

更新时间:
复制 MD 格式

Introduction

Cava uses loop structures to execute the same operation multiple times. Cava supports only for loops. It does not support while or do...while loops.

For loops

Syntax:

for (initialization; condition; update) {
    // The operation to execute.
}

The for loop works as follows:

  • The for loop first executes the initialization expression. This expression can be empty.

  • The loop then checks the condition. If the condition is true, the loop body is executed. If the condition is false, the loop stops.

  • After each loop iteration, the condition is updated and then checked again.

  • The continue statement stops the current iteration and proceeds to the next one.

  • The break statement stops the entire loop.

  • Loops can be nested.

Example:

class Example {
    static int main() {
        int a = 0;
        for (int i = 0; i < 10; ++i) {
            if (i == 1) {
                continue;
            }
            if (a > 10) {
                break;
            }
            a += i;
        }
        int j = 0;
        for ( ; i < 10; ++j) {
            a += j;
        }
        return a;
    }    
}