#๐Ÿ”’ break & continue statement

21 messages ยท Page 1 of 1 (latest)

cloud pier
#

Someone help me understand this break & continue statement, in the easiest way possible please.

safe sandalBOT
#

@cloud pier

Python help channel opened

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.

woven delta
#

um

#

i can help with anology

velvet thistle
woven delta
#

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

velvet thistle
#

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
woven delta
#

you got anology with code

cloud pier
#

THANK YOU GUYS SO MUCH

fallen ivy
woven delta
#

@cloud pier close the post

cloud pier
#

how?

woven delta
#

right click on your post

safe sandalBOT
#
Python help channel closed using Discord native close action

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.