Aiming for Jarvis, Creating D.A.N.I.

Tutorial 3: Control flow (or, How to keep your program from running in a straight line)

So, your variables are defined and your data is stored. That’s lovely, but if your program just executed every line once and then gave up, it would be about as useful as a chocolate teapot. Eventually, you need your code to make a choice or—heaven forbid—do the same thing more than once. Welcome to Control Flow.

If statements: The "Decision Tree"

Computers are great at making choices, provided you don't give them anything nuanced to think about. An if statement is just you asking the computer a binary question. It doesn't care about context; it just wants to know: is this true, or is it false?

fn main() -> int {
    temp := 22.0

    if temp > 30.0 {
        print("hot")
    } else if temp > 15.0 {
        print("comfortable")
    } else {
        print("cold")
    }

    return 0
}

Simple, right? Just remember that Skink isn't interested in your feelings, only in whether that boolean evaluates to true.

While loops: The "Keep going until I say stop"

Sometimes you have a task that needs to repeat, and you don't know how many times it’ll take. Maybe you’re waiting for a sensor to settle, or maybe you’re just trying to debug a memory leak that refuses to show its face reliably.

fn main() -> int {
    n := 5
    total := 0

    while n > 0 {
        total = total + n
        n = n - 1
    }

    print("Sum: {total}")
    return 0
}

Pro-tip: Be careful with your loop conditions. A while loop that never hits false is a great way to turn your workstation into a space heater. Also, note that we’re using = here for reassignment, not :=. Don't let your muscle memory trick you into re-declaring variables inside your loop, or you'll be wondering why your total never changes.

For loops: The "Traditional approach"

If you like your loops with a bit more structure, the classic for loop is there for you. It groups the initialization, the condition, and the increment all in one neat package. It's the loop for people who like to have all their ducks in a row before they start shooting.

fn factorial(n: int) -> int {
    result := 1
    for i := 1; i <= n; i = i + 1 {
        result = result * i
    }
    return result
}

For-in loops: The clean way to iterate

If you’re working with an array, don’t bother with manual index management. It’s messy, prone to off-by-one errors, and frankly, life is too short for that. Use for ... in instead.

fn main() -> int {
    temps := [20.0, 22.5, 19.0]
    sum := 0.0

    for t in temps {
        sum = sum + t
    }

    print("Average: {sum / 3.0}")
    return 0
}

Exercises

Don't just stare at the screen; make the machine do some work:

  1. The Sign Check: Write an if chain that takes an int and prints whether it’s "negative," "zero," or "positive." Try to be dramatic with the strings.
  2. Power Up: Use a while loop to print the powers of 2 (2, 4, 8...) all the way up to 1024. See if you can do it without triggering an infinite loop and cooking your CPU.
  3. Countdown: Write a for loop that counts from 10 down to 1. Think of it as a pre-flight check for your code.

Next

When you're ready to stop writing monolithic blocks of code and start organizing things properly, we'll talk about Functions.