Overclock.net - An Overclocking Community - View Single Post - Why is this correct? Looks to me like it shouldn't work.

View Single Post
post #27 of (permalink) Old 12-12-2017, 10:31 AM
herkalurk
Retired Staff
 
herkalurk's Avatar
 
Join Date: May 2010
Location: Portland, OR
Posts: 2,039
Rep: 69 (Unique: 58)
Quote:
Originally Posted by PhotonFanatic View Post

New python question. I'd like to hear what you guys have to say on this:

Why does elif exist?

The way I understand it, it goes like this: if (some function), do this.

Else, do this. Why would elif be needed? Wouldn't it make more sense to just be able to use an infinite number of elses?
Code:
if [ hour < 12 ]
then
   echo "good morning"
else if [ hour > 12 && hour < 18 ]
then
   echo "good afternoon"
else
   echo "good evening"
fi

Like the above code, sometimes you need more decision making. I made a workflow for a job and I had 5 else if statements in a row and finally an else. The reason was I needed to make sure that my final decision (the else) had passed a bunch of safety checks. If all of the else if statements returned false then I knew it was safe to proceed with the else, otherwise the else if statement would return a failure error.



herkalurk is offline