I am currently working on a coding competition that requires the least amount of characters used to accomplish a task. I have many if statements in it which are in a while loop, and each one requires a break statement. Currently, my code is:
while <something>:
if <something> is <something>:
<do this>
break
to shorten this I can do:
while <something>:
if <something> is <something>:<do this>
But according to my knowledge, I can't break after this. Is there any way to break and execute a statement in the same line?
All expressions are statements, but not all statements are expressions.
if
and else
expressions are ternary operators. The expressions are evaluated. break
and continue
are statements. A statement isn't evaluated, it's executed. Statements cannot be used as an expression, we can use an expression in shorthand code but not a statement.
Statements must be separated by newlines or semicolons. So you can write
while <something>:
if <something> is <something>:<do this> ; break