> Jitzu provides expressive control flow constructs including conditionals, loops, and early returns. Many control flow constructs are expressions, meaning they return values.

# Control Flow

Jitzu provides expressive control flow constructs including conditionals, loops, and early returns.
Many control flow constructs are expressions, meaning they return values.

## Conditionals

### Basic if Statements

```jitzu
// Simple if statement
if 2 > 1 {
    print("2 is greater than 1")
}
```

### if-else Chains

```jitzu
// if-else chain
if 2 < 1 {
    print("This won't print")
} else if 1 > 1 {
    print("Nor this")
} else {
    print("But this will")
}
```

### if Expressions

Since `if` is an expression, it returns a value:

```jitzu
// if expressions (return values)
let x = 10
let result = if x > 0 { "positive" } else { "non-positive" }
print(result) // "positive"

// Inline conditional
let message = if logged_in { "Welcome back!" } else { "Please log in" }
```

## Loops

### Range Loops

Jitzu provides convenient range-based loops:

```jitzu
// Inclusive range (1 to 5)
for i in 1..=5 {
    print(\` > {i}\`)
}
// Output: 1, 2, 3, 4, 5

// Exclusive range (1 to 4)
for i in 1..5 {
    print(\` > {i}\`)
}
// Output: 1, 2, 3, 4

// Character ranges
for c in 'a'..='z' {
    print(\` > {c}\`)
}
// Output: a, b, c, ..., z
```

### Collection Iteration

```jitzu
// Iterate over vector elements
let numbers = [1, 2, 3, 4, 5]
for num in numbers {
    print(\`Number: {num}\`)
}
```

### While Loops

```jitzu
// Basic while loop
let mut i = 0
while i < 10 {
    print(\`Count: {i}\`)
    i += 1
}

// While with complex condition
let mut attempts = 0
let mut success = false

while attempts < 3 && !success {
    success = try_operation()
    attempts += 1
}

// Infinite loop with break
let mut counter = 0
while true {
    counter += 1
    if counter > 100 {
        break
    }
}
```

### Loop Control

Control loop execution with `break` and `continue`:

```jitzu
// Using break and continue
for i in 1..=100 {
    if i == 50 {
        break // Exit the loop completely
    }

    if i % 2 == 0 {
        continue // Skip to next iteration
    }

    print(i) // Only prints odd numbers up to 49
}
```

## Early Returns

```jitzu
// Early return from function
fun find_first_even(numbers: Int[]): Option<Int> {
    for num in numbers {
        if num % 2 == 0 {
            return Some(num)
        }
    }
    None // No even number found
}

// Guard clauses
fun process_user(user: User): Result<String, String> {
    if !user.is_active {
        return Err("User is not active")
    }

    if user.age < 18 {
        return Err("User must be 18 or older")
    }

    // Main processing logic here
    Ok(\`Processing user: {user.name}\`)
}
```

## Best Practices

- Use guard clauses to handle edge cases first
- Reduce nesting by returning early
- Use `for` loops for known iterations
- Use `while` loops for condition-based iteration
- Combine conditions rather than deeply nesting

```jitzu
// Avoid deep nesting
// Bad:
if condition1 {
    if condition2 {
        if condition3 {
            // deeply nested code
        }
    }
}

// Better: Combine conditions
if condition1 && condition2 && condition3 {
    // main logic here
}
```

Control flow is fundamental to programming logic in Jitzu. Next, explore [Types](/docs/language/object-oriented) to learn about custom type definitions and composition.
