La Boina Roja

Linux, the struggles are real!

If, elif and else explained

Leave a comment

The credits for this explanation all go to zlozlozlozlozlozlo a user at reddit. I thought his explanation is worth sharing with you guys null

The if-if-else chain
if A:
>>>do_foo # this gest executed if A evaluates to True
if B:
>>>do_bar #this gest executed if B evaluates to True (regardless of A)
else:
>>>do_baz #this gets executed if B doesn't evaluate to True (again, regardless of A)

The if-elif-else chain:

if A:
>>>do_foo #this gest executed if A evaluates to True
elif B:
>>>do_bar #this gets executed if A doesn't evaluate to True but B does
else:
>>>do_baz # this gest executed if neiter A or B evaluaes to True

Only one of the three is executed in the above if-elif-else chain.

If you need more explanation null there are more people who also did a great job explaining this to me here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s