#๐ break & continue statement
21 messages ยท Page 1 of 1 (latest)
@cloud pier
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
in a for/while loop:
If you use continue, it will skip that specific iteration and move on to the next one
For break, it basically means it exits the loop
so like when you using loops and when you call break: it's stop the loop right there
imagine you watching a movie you get call from your mom "come home" you stop watching a movie right there
that's break
and continue is like saying " I will skip this one(loop or specific turn), then i will continue
it's like you skip the breakfast
but you still gonna eat lunch and dinner
Example with continue
fruits : list[str] = ["apple", "orange", "banana", "mango"]
for fruit in fruits:
if fruit == "orange":
continue # This moves onto banna immediately and skips "orange"
else:
print(f"This {fruit} is NOT an orange!)
Prints:
- This {apple} is NOT an orange!
- This {banana} is NOT an orange!
- This {mango} is NOT an orange!
Example with break
fruits : list[str] = ["apple", "orange", "banna", "mango"]
for fruit in fruits:
print(fruit)
if fruit == "orange":
break # Stops the loop/exits the loop
Prints:
- apple
- Orange # Exits the loop after this, that's why the rest of the fruits are not listed
you got anology with code
THANK YOU GUYS SO MUCH
Nice explanation
@cloud pier close the post
how?
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.