Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Control Flow

Felico provides several constructs for controlling program flow.

Conditional Expressions

If Expressions

let x = 10

if x > 5 {
    print("x is greater than 5")
}

If-Else

let x = 3

if x > 5 {
    print("x is greater than 5")
} else {
    print("x is 5 or less")
}

If-Else If-Else

let score = 85

if score >= 90 {
    print("A")
} else if score >= 80 {
    print("B")
} else if score >= 70 {
    print("C")
} else {
    print("F")
}

If as Expression

if is an expression and returns a value:

let x = 10
let message = if x > 5 { "big" } else { "small" }
print(message)  // "big"

Loops

While Loops

let i = 0
while i < 5 {
    print(i)
    i = i + 1
}

For Loops

(Coming soon: For loops with ranges and iterators)

// Future syntax
for i in 0..5 {
    print(i)
}

Loop Control

  • break - Exit the loop
  • continue - Skip to next iteration
let i = 0
while i < 10 {
    i = i + 1
    if i == 5 {
        continue
    }
    if i == 8 {
        break
    }
    print(i)
}

Pattern Matching

(Coming soon: Match expressions for pattern matching)

// Future syntax
match value {
    0 => print("zero"),
    1 => print("one"),
    _ => print("other"),
}

Next Steps