# Control Flow

#### Conditional Statements

**if**

```python
name = 'Arthur'
if name == 'Arthur':
    print("I didn't know we 'ad a king!")
```

*NOTE*

* Indentation creates a block
* A colon, `:`, is required at the beginning of a block
* Use 4 spaces per indentation level (from the [official style guide](https://peps.python.org/pep-0008/#indentation))

**elif**

```python
name = 'Galahad
if name == 'Arthur':
    print("I didn't know we 'ad a king!")
elif name == 'Galahad':
    print("Sir Galahad! You would not be so ungallant as to refuse our hospitality.")
```

**else**

```python
name = 'Galahad
if name == 'Arthur':
    print("I didn't know we 'ad a king!")
elif name == 'Galahad':
    print("Sir Galahad! You would not be so ungallant as to refuse our hospitality.")
else:
    print("Who are you?")
```

#### Loops

**while loop**

```python
i = 0
while i < 11:
    print('Loop #', i)
    i += 1
```

**for loop**

```python
for char in 'spam':
    print(char)
```

**range** `range(start, stop, step)`

The `range` function returns a sequence of numbers that starts from `start` (0 by default) and stops before `stop`, in increments of `step` (1 by default).

```python
for i in range(11):
    print('Loop #', i)
```

```python
for i in range(1, 11):
    print('Loop #', i)
```

```python
for i in range(10, 101, 10):
    print('Loop #', i)
```

**break & continue** You can stop a loop with `break` or skip to the next iteration with `continue`

```python
for i in range(100):
    if i % 2 != 0:
        print('odd number')
        continue
    elif i == 42:
        print('Stop at 42')
        break
    else:
        print(i)
```


---

# 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://ga0-2.gitbook.io/seifxr10anz-content/python-cheatsheet/control-flow.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.
