Loading...

Control Flow Statements with Copilot Prompts

In this lesson, you’ll learn how to make your code "think" and "repeat" — this is what control flow is all about!

Control flow statements help your program decide what to do next or repeat tasks, just like you decide to wake up when the alarm rings or brush your teeth every morning.

Let’s explore each concept together using Copilot prompts!

If-Else Statements

If-else statements allow your code to make decisions. You check a condition, and then you do something based on whether that condition is true or false.

Prompt

text
1 lines
|
20/ 500 tokens
1
# Write a Python function that checks if a number is positive, negative, or zero

When you type this comment and start your function (e.g., typing def check_number(), Copilot might suggest:

python
7 lines
|
34/ 500 tokens
1
2
3
4
5
6
7
def check_number(n):
    if n > 0:
        return "Positive"
    elif n < 0:
        return "Negative"
    else:
        return "Zero"
Code Tools

Explanation

  • if n > 0: This line checks if the number is greater than zero.
  • elif n < 0: This line checks if the number is less than zero. elif means "else if" — it checks another condition if the first one was false.
  • else: This runs when all the above conditions are false — here, it means the number must be zero.
  • return: This sends the result back to whoever called the function.

Practice

Try calling this function in your code:

python
3 lines
|
27/ 500 tokens
1
2
3
print(check_number(10))   # Positive
print(check_number(-5))   # Negative
print(check_number(0))    # Zero
Code Tools

You’ll see different results based on the input number!

Loops

Loops help you repeat actions without writing the same code over and over again.

For Loops

For loops are used when you want to repeat something a set number of times or go through items in a list, string, or other collection.

Prompt

text
1 lines
|
14/ 500 tokens
1
# Generate a loop that prints each character in a string

Copilot might suggest:

python
3 lines
|
18/ 500 tokens
1
2
3
def print_characters(word):
    for char in word:
        print(char)
Code Tools

Explanation

  • for char in word: This means "for each character in the string called word, do the following".
  • print(char): Prints the character.

Practice

Call this function with your name:

python
1 lines
|
7/ 500 tokens
1
print_characters("Alice")
Code Tools

You’ll see:

text
5 lines
|
3/ 500 tokens
1
2
3
4
5
A
l
i
c
e

While Loops

A while loop repeats as long as a condition is true.

Prompt

python
1 lines
|
17/ 500 tokens
1
# Write a loop that prints numbers from 1 to 5 using a while loop
Code Tools

Copilot might suggest:

python
5 lines
|
22/ 500 tokens
1
2
3
4
5
def print_numbers():
    num = 1
    while num <= 5:
        print(num)
        num += 1
Code Tools

Explanation

  • num = 1: Start with 1.
  • while num <= 5: Keep looping as long as num is less than or equal to 5.
  • print(num): Print the current number.
  • num += 1: Increase num by 1 every time (same as num = num + 1).

Practice

Try changing the condition to print up to 10 or 20, or start from a different number.

Nested Loops

Nested loops mean one loop inside another loop. This is used when you need to repeat actions in two dimensions — for example, printing rows and columns.

Prompt

text
2 lines
|
14/ 500 tokens
1
# Write a nested loop that prints a 3x3 grid of stars

Copilot might suggest:

python
4 lines
|
21/ 500 tokens
1
2
3
4
for i in range(3):
    for j in range(3):
        print("*", end=" ")
    print()
Code Tools

Explanation

  • Outer loop (for i in range(3)): Controls the rows (it repeats 3 times).
  • Inner loop (for j in range(3)): Controls the columns inside each row.
  • print("*", end=" "): Prints a star and stays on the same line.
  • print(): Moves to the next line after each row.

Practice

  • Change it to 5x5 by using range(5).
  • Replace * with numbers or letters to see different patterns.

Break and Continue

These statements help you control your loops more precisely.

Break

break stops the loop completely. Once it runs, the loop ends immediately.

Prompt

text
1 lines
|
10/ 500 tokens
1
# Show how to use break inside a loop

Copilot might suggest:

python
4 lines
|
18/ 500 tokens
1
2
3
4
for num in range(1, 10):
    if num == 5:
        break
    print(num)
Code Tools

Explanation

  • for num in range(1, 10): Starts at 1, goes up to 9.
  • if num == 5: When num is 5, break runs.
  • break: Stops the loop right away.

Output:

text
4 lines
|
2/ 500 tokens
1
2
3
4
1
2
3
4

The loop stops before printing 5.

Continue

continue skips the rest of the current loop iteration and moves to the next one.

Prompt

text
1 lines
|
10/ 500 tokens
1
# Show how to use continue inside a loop

Copilot might suggest:

python
4 lines
|
18/ 500 tokens
1
2
3
4
for num in range(1, 6):
    if num == 3:
        continue
    print(num)
Code Tools

Explanation

  • if num == 3: When num is 3, skip the rest of that loop round.
  • continue: Go directly to the next number.

Output:

python
4 lines
|
2/ 500 tokens
1
2
3
4
1
2
4
5
Code Tools

Practice Combining Them

Write a loop that prints numbers from 1 to 10 but:

  • Skips number 5.
  • Stops the loop completely if number 8 is reached.

Prompt

text
1 lines
|
18/ 500 tokens
1
# Write a loop that prints numbers from 1 to 10, skips 5, and stops at 8

Copilot might suggest:

python
6 lines
|
26/ 500 tokens
1
2
3
4
5
6
for num in range(1, 11):
    if num == 5:
        continue
    if num == 8:
        break
    print(num)
Code Tools

Expected Output:

text
6 lines
|
3/ 500 tokens
1
2
3
4
5
6
1
2
3
4
6
7

Key Takeaways

  • Use if-else to decide what to do based on conditions.
  • Use for loops to repeat a set number of times or go through items.
  • Use while loops to repeat as long as a condition is true.
  • Use nested loops for patterns or multi-level repetition.
  • Use break to stop, and continue to skip a specific round inside loops.

Mini Challenge

Prompt

text
1 lines
|
34/ 500 tokens
1
# Write a Python function that prints all numbers from 1 to 20. Skip numbers divisible by 3, and stop if the number is greater than 15.

Let Copilot help you write this, then test it!

Frequently Asked Questions

No! This lesson explains everything from the basics, even if you’ve never used a loop or if-statement before.

Yes, Copilot can generate complete loops, but it’s important to understand what each line does before using it.

Read it slowly, line by line. You can also ask Copilot to add comments or ask it to simplify the code.

Copilot is a helper, not a replacement for learning. Practice writing some code yourself so you fully understand loops and conditions.

Absolutely! You should always feel free to edit, improve, or experiment with Copilot's suggestions.

Still have questions?Contact our support team