Ruby - Conditionals if - else - elsif

Greetings!!!

In this article, we are going to be talking about the control structures inside Ruby. Control structures are really going to provide the action that happens inside of our Ruby programs. It's what's going to decide what takes place under certain circumstances and the first way that we are going to determine those circumstances is using conditionals. So this is, if something is true, we are going to do one thing. If it's not true, do something else.
Right, that's the basic thing. We will run one set of code under one condition, another under different conditions. That's why they are called conditionals. To do that, we are going to use the syntax of conditionals, which is going to be if and then a boolean expression and then end at the end and then in between will be the code that we want to take place if that Boolean value evaluates to true. So if x is equal to 1 then do this bit of code. Otherwise, don't bother, just skip right past it. That's how conditionals will work. Now, we could write two conditionals. We could have if x is equal to 1, then do this.

If x is not equal to 1 then do that. But because that happens so common we have if and else that we can use. So if x is equal to 1, do something, else do the alternative. Right, it allows us just to branch it in two simple directions based on the true/false value of that first conditional. But what if we want in a branch in more than one direction. We don't want to just have a yes or no action. We want to have more complicated choices. Well, we have another option for that which is elsif and that's not a typo. It is e-l-s-i-f and that allows us to do another test before we get to else.

So if x is equal to 1, do something, elsif and let's say x equals to 2, then do something else. But if neither of those is true, then do this default else action. That will be the fallback choice So be careful that you spell elsif correctly. That does trip up a lot of beginners, especially because other programming languages do it differently. So an actual conditional statement might look something like this: if x < 10 puts "Below 10"elsif x > 20 puts "Over 20"else puts "10-20" and then end.

Now notice that we don't have any parenthesis or curly braces or anything like that, letting it know where each of these sections is. Different languages handle that differently and a lot of them require you to put either parenthesis or curly braces. Ruby does not. It's one of the nice things we can just leave them out. The Ruby interpreter knows that when we start a new line, we are ready to start the code in that if statement and it will just keep parsing away until it gets to either elsif, else, or end. And when it gets to one of those, it knows that those are magic keywords that ought to tell it to stop that block of code and now do another part of the conditional.

CHEERS!!!
Maitrey Patel

Comments

Popular posts from this blog

CRUD Basics

Installing MySQL with Ruby on Rails

How to solve migration issues with rails