Problem with 'break'

Hello

I have a problem with ‘break’ in the following code.

x='Shomik'
if(type(x) is str):
    x=x+' '+"rocks"  # Line 3
    print('hello')
else:
    x=x+' '+'Python'
    break            # Line 7
    print('hi')
print(x)

This gives a SyntaxError: ‘break’ outside loop. I dont understand why. Please help.

The break statement is only allowed within loops, e.g., foror while. An ifstatement isn’t a loop. The Python docs have a section on the use of break and continue.

3 Likes