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 Python Basics (Retired) Shopping List Shopping List Project

James Zwar
James Zwar
11,737 Points

Why don't break and continue have the same level of indentation?

I noticed that the break and continue commands for the while loop do not have the same level of indentation - and when I tried moving them to the same level there was an unexpected indent error.

Why is this the case? Why not have the same level if they both operate on the while loop?

Thanks

1 Answer

While break and continue both control the flow of our program, we want them to run only under certain conditions. In the shopping list program the 'brake' function is part of the 'if' block. In python most blocks are started with a colon ( : ) and after that everything in that block is indented. This lets the interpreter know where blocks start and end. We use different blocks all the time such as in loops and and if statements. Sometimes blocks are inside each other so you have many different levels nested depending on what runs when.

In javascript ( I notice you took some javascript) the indentation doesn't really matter as we define blocks with curly braces {} to let the system know where things start and stop.

So because we only want to end the loop (that keeps asking for the items) when we type 'done', we indent the brake command so it is inside the if block. Else it would always run and would always top the loop there. The continue we want to alway run if the program gets to the end of the loop. The use of contine is kind of odd here. The program should always continue to the next loop when it gets to the end of this program, but it may be good convention or just to show that function.