> For the complete documentation index, see [llms.txt](https://pythonforstarters.solomonmarvel.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pythonforstarters.solomonmarvel.com/introduction-to-python/control-flow-in-python.md).

# Control flow in python

#### [Control Flow in Python](broken://pages/rNOOgup7DVXVLgJmdSsV) <a href="#control-flow-in-python" id="control-flow-in-python"></a>

A program's control flow is the order in which the program's code executes. The control flow of a Python program is regulated by conditional statements, loops, and function calls.

[**If statement in python**](broken://pages/rNOOgup7DVXVLgJmdSsV)

In a Python program, the if statement is how you perform this sort of decision-making. It allows for conditional execution of a statement or group of statements based on the value of an expression.

```
# example 1: Make a guess game
guess = 5
if(int(guess) == 7):
    print('You won')
    

# example 2: if statement example 2
if 2>5 and 2>1:
    print("One of this condition is correct")


# examle 3: if elif else conditionals
name = "John"

if name == "John":
    print(name)
elif name == "Andrew":
    print('name is not marv')
else:
    print('The name is neither John nor Andrew')

# if statement short hand (tenary operator)    
condition = True if 5 > 5 else False
```

[**For Loop**](broken://pages/rNOOgup7DVXVLgJmdSsV)

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

```
# for loop control flow - example 1
for i in range(2):
    if i == 0:
        continue
    elif i%2 == 0:
        print (f'{i} is an even number')
    else:
        print(f'{i} is an odd number')

name = "Joy"
result = "Name is Joy" if name == "Joy" else "Name is not Joy"
print(result)
```

[**For loop break keyword**](broken://pages/rNOOgup7DVXVLgJmdSsV)

'break' in Python is a loop control statement. It is used to control the sequence of the loop. Suppose you want to terminate a loop and skip to the next code after the loop; break will help you do that. A typical scenario of using the Break in Python is when an external condition triggers the loop's termination.

```
# Using the continue keyword in a loop
for i in range(50):
    if(i == 25):
        break
    print(i)
```

[**For loop continue keyword**](broken://pages/rNOOgup7DVXVLgJmdSsV)

'continue' in Python is a loop control statement. It is used to control the sequence of the loop. Suppose you want to skip a certain item and move to the next in a loop, 'continue' will help you achieve that.

```
# Using the continue keyword in a loop
for i in range(50):
    if(i == 25):
        continue
    print(i)
```

[**While loop in python**](broken://pages/rNOOgup7DVXVLgJmdSsV)

A while loop will run a piece of code while a condition is True. It will keep executing the desired set of code statements until that condition is no longer True. A while loop will always first check the condition before running. If the condition evaluates to True then the loop will run the code within the loop's body.

```
# While loop

max = 5
count = 1
while count < max:
    print("We are live")
    count += 1
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://pythonforstarters.solomonmarvel.com/introduction-to-python/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.
