# Nakafa Framework: LLM
URL: https://nakafa.com/en/subject/university/bachelor/ai-ds/ai-programming/control-flow
Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/control-flow/en.mdx
Output docs content for large language models.
---
export const metadata = {
    title: "Control Flow",
    description: "Learn control flow in Python including break, continue, pass, else, enumerate, while loops, zip, range, and if/elif/else with practical examples.",
    authors: [{ name: "Nabil Akbarazzima Fatih" }],
    date: "09/18/2025",
    subject: "AI Programming",
};
## Loop Control Statements
Python provides several special statements to control the execution flow in loops. These statements allow you to control when loops should stop, continue, or perform certain actions.
### Break, Continue, Pass, and Else
The `break` statement is used to stop a loop completely when a certain condition is met. Think of it like an emergency button that immediately stops a machine.
The `continue` statement skips the rest of the code in the current iteration and continues to the next iteration. This is different from `break` which stops the entire loop.
The `pass` statement does nothing but is useful as a placeholder for code that hasn't been written yet. Python requires at least one statement in every code block.
The `else` block in loops will be executed when the loop ends normally, not because of a `break`. This is useful for providing special actions when the loop completes without interruption.
## Enumerate Function
The `enumerate()` function is very useful when you need both the index and value of each element during iteration. This function takes an iterable object and returns tuples containing (index, value) pairs.
By default, `enumerate()` starts counting from 0, but you can set the starting value with the second parameter.
It's important to remember that each tuple (item, count) in the for loop will be automatically unpacked into variables `i` and `fruit`.
## While Loop
The `while` loop executes a code block as long as the given condition is `True`. Unlike `for` loops where the number of iterations is known, `while` loops are used when the number of iterations is unknown at the start.
The `while` loop is suitable when you need to iterate until a certain condition is met and don't know how many iterations are required.
 B{While condition True?}
        B -->|Yes| C[Execute code block]
        C --> D[Update variables]
        D --> B
        B -->|No| E[Exit loop]
        E --> F[Continue program]
  `}
/>
## Zip Function
The `zip()` function allows you to iterate over multiple iterables simultaneously. Think of it like a zipper that combines two sides into one.
### Basic Zip Usage
The `zip()` function returns an iterator containing tuples. Each tuple contains the i-th element from each input iterable. The iterator stops when the shortest iterable is exhausted.
### Converting Zip Results
### Unzipping with Operator *
You can reverse the zip process using the unpacking operator (*).
This process is called "unzipping" because it separates data that has been combined.
## Range and For Loop
The `range()` function creates a sequence of numbers that can be iterated. This is very useful for loops with numeric indices.
### Range Syntax
Range follows an arithmetic formula where each i-th element is `start + i * step` for all `i` from 0 to `n-1`.
### Range vs List Efficiency
Range is more efficient than creating a list containing all numbers because range generates numbers on-demand, rather than storing them all in memory.
## Conditional Statements
Conditional statements allow programs to make decisions based on certain conditions. Python uses indentation (spaces or tabs) to determine which code blocks belong to conditions.
### If/Elif/Else Syntax
Conditions don't need to be wrapped in parentheses, and a colon (:) after the condition is mandatory. Statement blocks must be indented with the same number of spaces.
### Nested If Statement
= 0:
    if x[1] >= 0:
        print('first quadrant')
    else:
        print('fourth quadrant')
elif x[1] >= 0:
    print('second quadrant')
else:
    print('third quadrant')
# Output: third quadrant`
    }
  ]}
/>
Nested if is useful for making more complex decisions with multiple levels of conditions.
## For Loop Syntax
For loops in Python are used to iterate elements in iterable objects like lists, tuples, strings, or ranges.
Iterable objects are ordered sequences of items, where each item can be taken one by one. The `in` operator is used for membership testing, colons mark the beginning of statement blocks, and statement blocks must be indented with spaces.