Introduction

Gal Normal

Hey, I've heard about "control flow" in programming. What is it?

Geek Curious

Control flow refers to the order in which statements are executed in a program. It can be altered using loops and conditional statements.

Gal Happy

Interesting! Can you teach me how to use control flow?

Geek Smiling

Sure! Let's start with conditional statements.

Conditional Statements

Gal Eager

How do we use conditional statements?

Geek Ready

Conditional statements allow you to execute different parts of the code based on whether a condition is true or false. The most common conditional statement is the "if" statement.

x = 10

if x > 5:
    print("x is greater than 5")

Output:

x is greater than 5
Gal Wondering

What if we want to check for multiple conditions?

Geek Encouraging

You can use "elif" (short for "else if") and "else" statements to check for multiple conditions.

x = 10

if x > 15:
    print("x is greater than 15")
elif x > 5:
    print("x is between 6 and 15")
else:
    print("x is 5 or less")

Output:

x is between 6 and 15

Loops

Gal Enthusiastic

Now, tell me about loops! They're like repeating stuff, right?

Geek Nodding

Yes! Loops are used to execute a block of code multiple times. There are two main types of loops: "for" loops and "while" loops.

For Loops

Gal Excited

Show me how to use a "for" loop!

Geek Smiling

A "for" loop is used to iterate over a sequence, like a list or a range of numbers. Here's an example:

for i in range(5):
    print(i)

Output:

0
1
2
3
4

While Loops

Gal Curious

How about a "while" loop?

Geek Explaining

A "while" loop keeps executing a block of code as long as a certain condition is true.

counter = 0

while counter < 5:
    print(counter)
    counter += 1

Output:

0
1
2
3
4

Conclusion

You’ve learned the basics of control flow in programming! By using conditional statements and loops, you can create more dynamic and efficient programs. Keep practicing, and you’ll become a control flow expert in no time! 🎉