Ruby Conditional Branching : the 'if' statement
You can use Ruby's Boolean Expressions to specifiy conditions using the usual if..else
construct.
Let us take a look at a simple example:
When you run the above example, it will tell you that 0 is negative
. But we know
that zero is neither positive nor negative. How do we fix this program so that zero is treated separately?
Ruby gives you the elsif
keyword that helps you check for
multiple possibilities inside an if..else
construct. Try using elsif
to fix the code below so that when the number is zero, it just prints 0
.
Ruby also has an unless
keyword that can be used in places where you want to check for a negative condition. unless x
is equivalent to if !x
. Here is an example: