# Control Structures

Control structures are used to **control the flow** of execution in a program. They help your code make decisions, repeat tasks, and react to different situations.

In Python, the main types of control structures are:

1. **Conditional Statements** (`if`, `elif`, `else`)
2. **Match-Case** (introduced in Python `3.10`)
3. **Loops** (`for`, `while`)
4. **Loop Control Statements** (`break`, `continue`, `pass`)

## Conditional Statements

Conditional statements allow your program to decide what to do based on certain conditions.

Syntax:

```python
if condition:
    # code runs if condition is True
elif another_condition:
    # runs if the first was False and this is True
else:
    # runs if none of the above are True
```

Example:

```python
x = 5

if x > 0:
    print("Positive")
elif x == 0:
    print("Zero")
else:
    print("Negative")

# Output: Positive
```

{% hint style="info" %}
You can use `if` without using `elif` and `else` as well. Even just `if-else`.
{% endhint %}

Example:

```python
x = 5

if x > 0:
    print("Positive")

# Output: Positive
```

## Match-Case

`match-case` is similar to `switch-case` in other languages. It's used to match a value against different patterns.

Syntax:

```python
match variable:
    case value1:
        # do something
    case value2:
        # do something else
    case _:
        # default case (like "else")
```

Example:

```python
fruit = "apple"

match fruit:
    case "apple":
        print("Its an apple")
    case "banana":
        print("Its a banana")
    case _: # Here '_' is for default. If nothing matches, this gets matched.
        print("Unknown fruit")

# Output: It’s an apple
```

## Loops

Loops are used to repeat a block of code multiple times.

### For Loop

Used to iterate over sequences like lists, strings, or ranges.

Example:

```python
for i in range(3):
    print(i)

# Output:
# 0
# 1
# 2
```

#### Looping through a list

We also call it *iterating through a list*.

<pre class="language-python"><code class="lang-python">fruits = ["apple", "banana", "cherry"]
<strong># Note: We can iterate over any data types we learned so far and not just lists.
</strong>
for i in fruits:
    print(i)

# Output:
# apple
# banana
# cherry
</code></pre>

### While Loop

Repeats as long as a condition is `True`.

```python
x = 0

while x < 3:
    print(x)
    x += 1

# Output:
# 0
# 1
# 2
```

## Loop Control Statements

These help manage how loops behave.

### break

Exits the loop immediately.

```python
for i in range(5):
    if i == 3:
        break
    print(i)

# Output:
# 0
# 1
# 2
```

### continue

Skips the current iteration and moves to the next.

```python
for i in range(5):
    if i == 2:
        continue
    print(i)

# Output:
# 0
# 1
# 3
# 4
```

### pass

Used as a placeholder. Does nothing, just a syntactic placeholder.

```
for i in range(3):
    pass  # this loop does nothing
```

## Nested Control Structures

You can use control structures inside each other.

```python
for i in range(3):
    if i % 2 == 0:
        print(f"{i} is even")
    else:
        print(f"{i} is odd")

# Output:
# 0 is even
# 1 is odd
# 2 is even
```

{% hint style="info" %}
We can create nested control structures as much as we like, just like we learned with [Variables and Data Types](/fundamentals/python/basics-of-python/variables-and-data-types.md).
{% endhint %}

## The `else` clause with loops

Yes, Python allows `else` to be used with loops!

* In a `for` or `while` loop, the `else` block runs if the loop **didn’t break**.

```python
for i in range(5):
    print(i)
else:
    print("Loop finished normally")

# Output:
# 0
# 1
# 2
# 3
# 4
# Loop finished normally
```

If a `break` happens, the `else` part is skipped.

```python
for i in range(5):
    if i == 3:
        break
    print(i)
else:
    print("Loop finished normally")

# Output:
# 0
# 1
# 2
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.ncateam.xyz/fundamentals/python/basics-of-python/control-structures.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
