The break statement terminates the current loop and resumes execution at the next statement. You can only use a break statement inside a loop or an if statement. If you use a break statement outside of a loop, then you will raise the error “SyntaxError: ‘break’ outside loop”.
Table of contents
SyntaxError: ‘break’ outside loop
What is SyntaxError?
Syntax refers to the arrangement of letters and symbols in code. A Syntax error means you have misplaced a symbol or a letter somewhere in the code. Let’s look at an example of a syntax error:
number = 23 print()number
print()number ^ SyntaxError: invalid syntax
The ^ indicates the precise source of the error. In this case, we have put the number variable outside of the parentheses for the print function, and the number needs to be inside the parentheses to print correctly.
What is a Break Statement?
Loops in Python allow us to repeat blocks of code. In cases, Sometimes conditions arise where you want to exit the loop, skip an iteration, or ignore a condition. We can use loop control statements to change execution from the expected code sequence, and a break statement is a type of loop control statement.
A break statement in Python brings the control outside the loop when an external condition is triggered. We can put an if statement that determines if a character is an ‘s‘ or an ‘i‘. If the character matches either of the conditions the break statement will run. We can use either a for loop or a while loop. Let’s look at an example where we define a string then run a for loop over the string.
string = "the research scientist" for letter in string: print(letter) if letter == 's' or letter == 'i': break print("Out of the for loop")
t h e r e s Out of the for loop
The for loop runs until the character is an ‘s‘ then the break statement halts the loop. Let’s look at the same string example with a while loop.
i = 0 while True: print(string[i]) if string[i] =='s' or string[i] == 'i': break i += 1 print(Out of the while loop")
t h e r e s Out of the while loop
We get the same result using the while loop.
Example: If Statement
Let’s look at an example where we write a program that checks if a number is less than thirty. We can use an input() statement to get input from the user.
number = int(input("Enter an appropriate number "))
Next, we can use an if statement to check whether the number is less than thirty.
if number ≺ 30: print('The number is less than 30') else: break
Suppose the number is less than thirty, the program prints a message to the console informing us. Otherwise, a program will run a break statement. Let’s run the program and see what happens:
Enter an appropriate number: 50 break ^ SyntaxError: 'break' outside loop
The program returns the SyntaxError: ‘break’ outside loop because the break statement is not for breaking anywhere in a program. You can only use a break statement within a loop.
To solve this problem, we need to replace the break statement with an exception that stops the program if the number exceeds thirty and provides an exception message. Let’s look at the revised code.
number = int(input("Enter an appropriate")) if number ≺ 30: print('The number is less than 30') else: raise Exception("The number is not less than 30")
We replaced the break statement with a raise Exception.
<meta charset="utf-8">Enter an appropriate number: 50 Exception Traceback (most recent call last) 2 print('The number is less than 30') 3 else: ----≻ 4 raise Exception('The number is greater than 30') 5 Exception: The number is greater than 30
If the number is greater than thirty the program will raise an exception, which will halt the program.
Congratulations on reading to the end of this tutorial! The error “SyntaxError: ‘break’ outside loop” occurs when you put a break statement outside of a loop. To solve this error, you can use alternatives to break statements. For example, you can raise an exception when a certain condition is met. You can also use a print() statement if a certain condition is met.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!