Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Loops & Iteration | Control Flow & Logic
Introduction to Python with AI

bookLoops & Iteration

In programming, we often need to repeat actions β€” like iterating through a list, checking input, or running calculations. Loops automate these tasks without duplicating code.

Python offers two main loop types: for and while, each with its own strengths.

Note
Example Prompts
  • Explain how for loop works. Show code example.
  • Explain how while loop works. Show code example.
  • Show a while loop in Python where the loop breaks when a certain condition is met. Include a break statement in the example.
  • Show a while loop in Python that uses the continue statement to skip even numbers and only print odd ones.

For Loops

A for loop iterates over a sequence like a list, string, or range. It runs the code block once for each element.

for loops are best when you know how many times to repeat or need to process a fixed set of values.

While Loops

A while loop runs as long as its condition is true. It's more flexible than a for loop but needs care to avoid infinite loops.

Use it when you don't know beforehand how many repetitions are needed β€” for example, waiting for valid user input.

Breaking Out of Loops

The break statement lets you exit a loop immediately, even if the sequence or condition isn't finished. It's useful when you've found what you need, want to react to an event, or improve performance by stopping early.

Skipping Iterations

The continue statement skips the rest of the current loop iteration and moves to the next one. It's useful for ignoring certain values, like blank lines in a file or even numbers in a list.

Summary

  • Loops let you repeat actions automatically, which helps simplify code;
  • for loops iterate over a fixed sequence;
  • while loops keep running until a condition is false;
  • Use break to exit a loop early;
  • Use continue to skip specific iterations;
  • Always be careful with while loops β€” they must eventually stop.

Try It Yourself

  1. Write a loop that counts from 1 to 10;
  2. Use continue to skip the number 5;
  3. Use break to stop the loop when the number reaches 8.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me an example of how to write that loop in Python?

What happens if I remove the `continue` or `break` statements from the loop?

Can you explain the difference between `break` and `continue` with more examples?

Awesome!

Completion rate improved to 5

bookLoops & Iteration

Swipe to show menu

In programming, we often need to repeat actions β€” like iterating through a list, checking input, or running calculations. Loops automate these tasks without duplicating code.

Python offers two main loop types: for and while, each with its own strengths.

Note
Example Prompts
  • Explain how for loop works. Show code example.
  • Explain how while loop works. Show code example.
  • Show a while loop in Python where the loop breaks when a certain condition is met. Include a break statement in the example.
  • Show a while loop in Python that uses the continue statement to skip even numbers and only print odd ones.

For Loops

A for loop iterates over a sequence like a list, string, or range. It runs the code block once for each element.

for loops are best when you know how many times to repeat or need to process a fixed set of values.

While Loops

A while loop runs as long as its condition is true. It's more flexible than a for loop but needs care to avoid infinite loops.

Use it when you don't know beforehand how many repetitions are needed β€” for example, waiting for valid user input.

Breaking Out of Loops

The break statement lets you exit a loop immediately, even if the sequence or condition isn't finished. It's useful when you've found what you need, want to react to an event, or improve performance by stopping early.

Skipping Iterations

The continue statement skips the rest of the current loop iteration and moves to the next one. It's useful for ignoring certain values, like blank lines in a file or even numbers in a list.

Summary

  • Loops let you repeat actions automatically, which helps simplify code;
  • for loops iterate over a fixed sequence;
  • while loops keep running until a condition is false;
  • Use break to exit a loop early;
  • Use continue to skip specific iterations;
  • Always be careful with while loops β€” they must eventually stop.

Try It Yourself

  1. Write a loop that counts from 1 to 10;
  2. Use continue to skip the number 5;
  3. Use break to stop the loop when the number reaches 8.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt