# Control Flow in Python

Control flow refers to the order in which instructions are executed in a program. By default, Python executes code line by line from top to bottom. However, with control flow structures like **conditional statements** and **loops**, you can make your programs more dynamic and responsive. In this chapter, we will explore how to make decisions in your code and repeat tasks using loops.

#### Conditional Statements

Conditional statements allow your program to make decisions based on certain conditions. The most common conditional statement in Python is the `if` statement, which lets you execute specific code only if a condition is true. You can also use `elif` (short for "else if") to check additional conditions, and `else` to define code that runs if none of the previous conditions are true.

For example, imagine you want to write a program that checks whether a number is positive, negative, or zero. You could use an `if`, `elif`, and `else` structure to handle each possibility.

* **if**: Checks if a condition is true and executes the block of code inside.
* **elif**: Used to check multiple conditions after an initial `if`.
* **else**: Executes when none of the previous conditions are met.

#### Loops

Loops allow you to repeat a block of code multiple times, which is useful when you need to perform a task repeatedly or iterate through a collection of items. Python provides two main types of loops: **for loops** and **while loops**.

* **For Loop**: A `for` loop lets you iterate over a sequence (like a list, string, or range of numbers) and perform an action for each element in the sequence. For example, if you have a list of numbers and you want to print each one, a `for` loop is an ideal solution.
* **While Loop**: A `while` loop continues to execute as long as a specified condition remains true. For example, you could use a `while` loop to repeatedly prompt the user for input until they provide a valid answer.

Loops are a powerful tool for reducing repetitive tasks and making your code more efficient. However, it's important to ensure that your loop has a clear exit condition. If not, you might accidentally create an **infinite loop**, which will cause your program to keep running without stopping.

#### Breaking and Continuing Loops

Sometimes, you might want to stop a loop early or skip to the next iteration without finishing the current one. Python provides two useful statements for controlling loops:

* **break**: The `break` statement allows you to exit a loop prematurely, even if the loop's condition has not been fully met.
* **continue**: The `continue` statement allows you to skip the rest of the code inside a loop for the current iteration and move on to the next iteration.

#### Conclusion

Understanding control flow is essential to writing more flexible and dynamic programs. By using conditional statements to make decisions and loops to repeat tasks, you can write programs that can adapt to various situations. In the next chapter, we’ll dive into the concept of functions, which allow you to organize your code into reusable blocks.

**Some content generated with AI**


---

# 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://python.jerryhobby.com/control-flow-in-python.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.
