Conditionals Cheatsheet

If-Else Statements

if x > 10 {
    // code
} else if x == 10 {
    // code
} else {
    // code
}
  • No Parentheses: if (x > 10) is not required.
  • Strict Braces: { must be on the same line.

Initial Statement

if val := calculate(); val > limit {
    // val is only available here
}

Switch Statements

switch value {
case 1:
    // implicit break
case 2, 3, 4: // Multiple values
    // code
case 5:
    fallthrough // Moves to next case
default:
    // fallback
}

Switch with No Expression

A switch with no expression is the same as switch true, which is a cleaner way to write long if-else chains.

switch {
case x > 100:
    // ...
case x < 0:
    // ...
default:
    // ...
}