La Boina Roja

Linux, the struggles are real!


Leave a comment

If, elif and else explained

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.