This topic describes the syntax of for loops in Cava and provides a code example.
Overview
Cava uses loop structures to repeatedly execute an operation. Cava supports only `for` loops. `while` and `do...while` loops are not supported.
for loop
Description
A
forloop first executes the initialization part, which is optional.Next, the condition is evaluated. If the condition is met, the loop body is executed. If the condition is not met, the loop body is not executed, and the for loop ends.
After each loop iteration, the condition is updated and then re-evaluated.
In the loop body, you can use "continue" to terminate a single execution of the loop body in advance.
In the loop body, you can use "break" to terminate the entire for loop in advance.
Nested for loops are supported.
Syntax
for (initialization; condition; conditional update) {
// Loop body. The operation to execute.
}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;
}
}