Learn
The break
is a one-line statement that means “exit the current loop.” An alternate way to make our counting loop exit and stop executing is with the break
statement.
First, create a
while
with a condition that is always true. The simplest way is shown.Using an
if
statement, you define the stopping condition. Inside theif
, you writebreak
, meaning “exit the loop.”
The difference here is that this loop is guaranteed to run at least once.
Instructions
1.
See what the break
does? Feel free to mess around with it (but make sure you don’t cause an infinite loop)! Click Run when you’re ready to continue.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.