Manish Goel
Manish Goel

Reputation: 893

Why "if-else-break" breaks in python?

I am trying to use if-else expression which is supposed to break the loop if the if condition fails, but getting an invalid syntax error.

Sample code:

a = 5
while True:
    print(a) if a > 0 else break
    a-=1

Of course, if I write in the traditional way (not using the one liner) it works.

What is wrong in using the break command after the else keyword?

Upvotes: 14

Views: 51408

Answers (3)

orn688
orn688

Reputation: 898

To put it in slightly simpler terms, you're misusing the 'one-line if statement' (ternary operator). It always evaluates to an expression (i.e., a value). That is,

<expr1> if <condition> else <expr2>

evaluates to <expr1> if <condition> is True, and to <expr2> if <condition> is False. This resulting value can then be used like any Python value, for example:

y = 0
x = (5 if (y > 0) else 6)
print(x) # 6

Of course, the parentheses are completely unnecessary (even discouraged), but hopefully are useful for understanding the meaning of that line.

Therefore,

print(a) if a > 0 else break

tries to evaluate print(a) (which, by the definition of print() in Python 3, always returns None – perfectly valid, but probably not what you usually want) and then break, which does not evaluate to anything because it is a statement (action), not an expression (value), hence the invalid syntax error.

Hence, if you want to execute one of two statements depending on a condition, you really need the multi-line solution proposed by Willem Van Onsem. There may be hacky ways to do it in one line, but multiple lines is the usual solution for something like this in Python.

Upvotes: 4

hspandher
hspandher

Reputation: 16753

If is an expression, break similar to return is a statement. You can't use two statements in a single sentence (unless you use a semicolon which is ugly). I know it would have been really cool if we can do that, but alas that's the way it is.

Upvotes: 9

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477308

If I run this, I get the following error:

...     print(a) if a > 0 else break
  File "<stdin>", line 2
    print(a) if a > 0 else break
                               ^
SyntaxError: invalid syntax

This is because

print(a) if a > 5 else break

is a ternary operator. Ternary operators are no if statements. These work with syntax:

<expr1> if <expr2> else <expr3>

It is equivalent to a "virtual function":

def f():
    if <expr2>:
        return <expr1>
     else:
         return <expr3>

So that means the part next to the else should be an expression. break is not an expression, it is a statement. So Python does not expect that. You can not return a break.

In , print was not a function either. So this would error with the print statement. In print was a keyword.

You can rewrite your code to:

a = 5
while True:
    if a > 5:
        print(a)
    else:
        break
    a -= 1

You can read more about this in the documentation and PEP-308.

Upvotes: 25

Related Questions