DEV Community

loading...

Conditional statement usage.

Kim Nguyen
She/her. Inspire to Aspire :).
Updated on ・4 min read

An overall look of ways to use if statement to control your program flow as you conduct your code!

Syntax

The if statement essentially checks if your condition is true or not. In Ruby, the syntax begins with an if keyword, followed by a condition, it is optional to include then before the first code being executed if the condition is true.

Alt Text

You can also add extra conditions by using a keyword called else if, written as elsif (without an "e"), followed by a code being executed if it is true. And the else keyword is only being checked when both of the conditions above are false. Our code should look something like this:

Alt Text

Conditions, for example comparing variables' values to each other, can be conducted using "comparison/conditional" operators:
== : is equal to (or comparison operator).
!= : is not equal to.
> : is more than.
>= : is more than or equal to.
< : is less than.
<= : is less than or equal to.
||: the "or" operator (read as double-pipe).
&&: the "and" operator.

Now let's try and put these operators in action to make sure we get a thorough look at these new shiny operators we just learned!

Examples

Alt Text

In this example, = symbol is used to assign a value to a specific variable, in this case, the variable is muffin_breed. However, in our if statement, a == symbol is being used instead. Kinda odd if you try to read the code out loud, it's gonna sound like "If Muffin's breed equal to munchkin, then we print out 'Muffin is a cute lil shortie!'". You would probably be thinking "isn't = symbol equal. Then what is ==?"
We used the == operator because we are comparing a variable to a value (not assigning it). In this case, we are looking into Muffin's breed and the result can only be true or false. Because Muffin is truly a munchkin cat (with short lil legs), the output is "Muffin is a cute lil shortie!" because the comparison returns true.

Alt Text

This example looks a little more complex, but do not be discouraged! The code makes more sense when we read it out loud:
"If Muffin's age is greater or equal to 3, the output is 'Muffin is a wise kitty.'. If the primary condition is false, we continue to compare Muffin's age to see if she is 2 years old. If true, then its output would be 'Muffin is 2 years old now!'. If none of the above conditions match Muffin's age, we output 'Muffin is still a baby kitten.' Since she is neither 2 years old or 3 years old and above."
Ahh, our code makes more sense now. So in this example, the output is....
Yes, "Muffin is still a baby kitten."

Common expression

1. && AKA the "and" operator:

Alt Text

True if the condition to the left and right of this operator are both true. Otherwise, return False.

2. || AKA the "or" operator:

Alt Text

True if either the condition to the left or right of this operator is considered true. If both are considered False, return False.

3. == AKA the comparison operator:

Alt Text

Besides the = operator to assign value to a variable. Ruby also has == to compare values and return boolean expression. True if both sides are equal, otherwise, return false.

Note: we can only compare values with the same datatype (e.g: strings to strings, integer to integer, etc).

Next up, let's jump right into ternary operator and see these babies in action!

Ternary Operator

Do you still remember the Muffin's breed example above? What if I say, instead of writing 5 lines of code, we can easily conduct only 1 line with the same outputs? "We enjoy typing long operator" - said no programmer ever. SO RUBY FOR THE WIN! Ruby has another interesting way of shortening basic comparison code called ternary operator!

We write a ternary operator in this syntax:

condition ? true_expression : false_expression

Apply ternary operator syntax to our example above, our code should look something like this:

Alt Text

Yes! We did it. Same condition, same outputs, but so much shorter and easier to track!

Let's break it down step by step.

Alt Text

• Section 1: this is the condition we want to check if it is true or not.
• Section 2: code being executed if the condition is true.
• Section 3: code being executed if the condition is false.

Either of the code will be printed out to the terminal.

Bonus Ternary Example

What if you need to put more than 2 expressions for your code? We can still use Ternary Operator, even though it is not recommended because our code can get messy pretty quick and hard for other programmers to read . But in a challenge, going from 6 lines of code down to 1 line feels like a major win. We can easily shorten our example above, it should look like this:

Alt Text

Discussion (0)