elsif and else if ?

Started by Zexion, April 04, 2013, 12:17:19 am

Previous topic - Next topic

Zexion

What exactly is the difference? I used elsif right now, and it worked fine, and then added some stuff and changed it to else if just for the sake of making it look nicer, imo. The first returned no errors, the second caused the script to hang, and a syntax error requiring an extra end.

So, like I said. What exactly is the difference of elsif and else if?

Blizzard

April 04, 2013, 02:47:05 am #1 Last Edit: April 04, 2013, 02:51:26 am by Blizzard
This statement doesn't work in Ruby:

if X
else if Y
else
end


That's because the second "else" is a new block and requires another end to close it. With proper indentation, it would look like this:

if X
else if Y
 else
 end
end


Or:

if X
else
 if Y
 else
 end
end


The difference is that you can easily chain multiple elsifs.

if X
elsif Y
elsif Z
elsif W
elsif U
end


It's easier to overview even though it is the same as this one:

if X
else
 if Y
 else
   if Z
   else
     if W
     else
       if U
       else
       end
     end
   end
 end
end
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Zexion

Oh okay, so it's just something to make scripting easier?

Blizzard

Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.