4

Control Flow

Conditionals and loops

If / Else (ti / bibẹkọ)

Yoruba: ti, bibẹkọ ti, bibẹkọ
English: if, else if, else
Conditionals
ayanmo score = 85;

ti (score >= 90) {
    Irosu.fo("Grade: A");
} bibẹkọ ti (score >= 80) {
    Irosu.fo("Grade: B");
} bibẹkọ ti (score >= 70) {
    Irosu.fo("Grade: C");
} bibẹkọ {
    Irosu.fo("Grade: F");
}
let score = 85;

if (score >= 90) {
    Fmt.println("Grade: A");
} else if (score >= 80) {
    Fmt.println("Grade: B");
} else if (score >= 70) {
    Fmt.println("Grade: C");
} else {
    Fmt.println("Grade: F");
}

While Loop (nigba)

Yoruba: nigba
English: while
While Loop
ayanmo i = 1;

nigba (i <= 5) {
    Irosu.fo("Count: " + i);
    ayanmo i = i + 1;
}
// Output: Count: 1, Count: 2, ... Count: 5
let i = 1;

while (i <= 5) {
    Fmt.println("Count: " + i);
    let i = i + 1;
}
// Output: Count: 1, Count: 2, ... Count: 5

For Loop (fun ... ninu)

Yoruba: fun item ninu list
English: for item in list
For Loop
ayanmo fruits = ["apple", "banana", "orange"];

fun fruit ninu fruits {
    Irosu.fo("I like " + fruit);
}
// Output:
// I like apple
// I like banana
// I like orange
let fruits = ["apple", "banana", "orange"];

for fruit in fruits {
    Fmt.println("I like " + fruit);
}
// Output:
// I like apple
// I like banana
// I like orange

Iterating with Range

Range Loop
// Using Iwori.range() to create number sequences
ayanmo numbers = Iwori.range(1, 6);  // [1, 2, 3, 4, 5]

fun n ninu numbers {
    Irosu.fo("Number: " + n);
}
// Using Time/Iwori.range()
// 'Iwori' maps to 'Time' (0110)
let numbers = Time.range(1, 6);  // [1, 2, 3, 4, 5]

for n in numbers {
    Fmt.println("Number: " + n);
}

Break and Continue

Yoruba: duro, tesiwaju
English: break, continue
Loop Control
ayanmo i = 0;

nigba (i < 10) {
    ayanmo i = i + 1;
    
    ti (i == 3) {
        tesiwaju;  // skip 3 (continue)
    }
    
    ti (i == 7) {
        duro;  // stop at 7 (break)
    }
    
    Irosu.fo(i);
}
// Output: 1, 2, 4, 5, 6
let i = 0;

while (i < 10) {
    let i = i + 1;
    
    if (i == 3) {
        continue;  // skip 3
    }
    
    if (i == 7) {
        break;  // stop at 7
    }
    
    Fmt.println(i);
}
// Output: 1, 2, 4, 5, 6

Nested Loops

Nested Loops
// Multiplication table
fun i ninu Iwori.range(1, 4) {
    fun j ninu Iwori.range(1, 4) {
        ayanmo product = i * j;
        Irosu.so(product + " ");
    }
    Irosu.fo("");  // newline
}
// Output:
// 1 2 3
// 2 4 6
// 3 6 9
// Multiplication table
for i in Time.range(1, 4) {
    for j in Time.range(1, 4) {
        let product = i * j;
        Fmt.print(product + " ");
    }
    Fmt.println("");  // newline
}

What You Learned