Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Build an Application Display the List

Ali AlRabeah
Ali AlRabeah
909 Points

Is it common convention to have the "break" come before the "continue"?

Just wanted to know in regards to this loop and loops in general.

while True:
    new_item = input("> ")
    if new_item == "DONE":
        break
    elif new_item == "HELP":
        show_help()
        continue
    elif new_item == "SHOW":
        show_list()
        continue

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

In regards to that loop, I would say yes the break would come first because as soon as "Done" has been entered you know to end but keep in mind that those continue statements are just there for display purposes and in that simple loop example they don't have any impact on the flow.

Regarding loops in general, it is possible and common to have multiple breaks and continues, they are useful flow control tools. It is common to exit a loop (break) as soon as you can (simple to understand) like a user entering done or quit reaching you desired entity (like customer record) when all entities don't need to be processed. I think it is common to do a get out of here check at the beginning if possible and also at the end after any required processing is completed. Whether it is a actual break statement or somehow setting the loop condition to false (bumping the "for" counter out of range or "while" condition to false).

Hope that wasn't too much of a ramble :smile: