Learn Aero / Control Flow

Control Flow

if / else

let x = 10;
if (x > 0) {
    print("positive\n");
} else {
    print("non-positive\n");
}

The condition must be a bool expression; numbers cannot be used directly as conditions.

if / else if Chains

let score = 85;
if (score >= 90) {
    print("A\n");
} else if (score >= 80) {
    print("B\n");
} else if (score >= 70) {
    print("C\n");
} else {
    print("D\n");
}

while Loops

let i = 0;
while (i < 5) {
    print("%d\n", i);
    i = i + 1;
}
// 输出 0 1 2 3 4

Combined Usage

// 打印 1 到 100 之间所有能被 3 整除的数
let i = 1;
while (i <= 100) {
    if (i % 3 == 0) {
        print("%d\n", i);
    }
    i = i + 1;
}

No break / continue yet: Aero 0.1's while has no break or continue. To exit a loop early, simulate it with a condition variable.

// 模拟 break:找到第一个大于 100 的 7 的倍数
let i = 1;
let found = 0;   // 0 = 假,1 = 真
while (i <= 1000 && found == 0) {
    if (i % 7 == 0) {
        print("%d\n", i);
        found = 1;
    }
    i = i + 1;
}

Loop Scoping Pitfall

Variables declared inside a loop that are meant to "persist across iterations" get reset every time:

let i = 0;
while (i < 5) {
    let sum = 0;          // 错!每次循环 sum 都归零
    sum = sum + i;
    i = i + 1;
}

Declare sum outside the block and accumulate inside the loop.

Aero 文档 · 基于 Aero 1.2.0 文档整理 · MIT
SereinTeam