DEV Community

Cover image for Flow for Grandmas
coding4grandmas
coding4grandmas

Posted on

Flow for Grandmas

Alt Text

Normally Ruby and most other programming languages process instructions in a linear way, top to bottom. But what if we needed something more? What if our logic requires us to evaluate certain conditions and then operate upon that choice?

Today we will be covering conditionals, comparison and logical operators, and we will even throw in some ternary operators.

Conditionals

Quoting Russ Olsen from Eloquent Ruby:

"If indention, comments, and the form of your variables are the things that give you program its look, it's the control structures - the ifs and elses and whiles - that bring it to life".

Conditionals play a very important role in our code as it tells the program how to proceed based on defined parameters. The statements that we use are

  • If / else
  • if / elsif
  • unless
  • case
  • while / until

IF/ELSE: this is the most important flow control. If a condition is met (true), then it will do something; otherwise, it will do something else - a classic one.

age = 14

if age >= 18
  puts "You can vote"
else
  puts "You can't vote"
end

# => "You can't vote
Enter fullscreen mode Exit fullscreen mode

In plain English, we are telling Ruby - "If the age is equal to or above 18, then print 'You can vote', otherwise print 'You can't vote'".

In programming, we assign 14 to our age variable. When Ruby runs the expression, it checks whether the age variable is greater than or equal to 18. Since the age (14) is less than 18, it does not implement the logic (i.e. "You can vote"). Because the first part of the if expression was not met, Ruby executes the else expression, which means the expression(s) above were false.

ELSEIF: is used when you have more than one condition to evaluate. For example:

digit = 1

if digit == 0
  puts "Zero"
elsif digit == 1
  puts "One"
else
  puts "I don't know this digit,sorry!"
end

# => "One"
Enter fullscreen mode Exit fullscreen mode

In plain English, we are telling Ruby - "If the digit is equal to 0 print "Zero", else if digit equals to 1 print "One", otherwise if none of these are true print "I don't know this digit, sorry!"

Ruby ran the statement, and it evaluated the first expression as false since the value of our digit is 1. Then it checked the second expression, and it was true since 1 equals 1. In this statement, we never reached the else expression. This is perfectly fine.

Unless: is the exact opposite of 'if'. 'If' will execute when the statement is true. 'unless' on the other hand will execute when the statement is false:

age = 14

unless age >= 18
  puts "You can't vote"
else
  puts "You can vote"
end

# => "You can vote
Enter fullscreen mode Exit fullscreen mode

In plain English, we are telling Ruby - "If the age is not equal to or above 18, then print 'You can vote', otherwise print 'You can't vote'". In this instance, we will execute the unless statement since 14 is less than 18, meaning we have a false value. It takes some time wrapping your head around this particular expression.

CASE/WHEN: is very similar to if / else expression. The premise is very similar:

digit = 1

case 
when digit == 0
  puts "Zero"
when digit == 1
  puts "One"
else
  puts "I don't know this digit,sorry!"
end

# => 1 
Enter fullscreen mode Exit fullscreen mode

As we said, it is very similar to if-else. In this instance, we are telling ruby to execute the statement when the digit variables match 1.

Comparison Operators

They are used to check values between strings and numbers.

== Equal

!= Not equal

> Greater than

< Less than

>= Greater than or equal to 

<= Less than or equal to
Enter fullscreen mode Exit fullscreen mode

Don't worry; we are not going back to solving math problems. Comparison operators allow the programmer to build a much more complex conditional statement.

For example, the equal (==) comparison will return true if the value on the left is equal to the value on the right

1 == 1
# => true

1 == 2
# => false 
Enter fullscreen mode Exit fullscreen mode

Logical Operators

Think of logical operators as Boolean operators.

and and &&

or and ||

not and !
Enter fullscreen mode Exit fullscreen mode

&& returns true when both conditions are satisfied for example

a = 1
b = 2

if a == 1 && b == 2
    puts "True"
end

# => "True"
Enter fullscreen mode Exit fullscreen mode

For the && (and) operator to evaluate true, both conditions need to be true. For example:

a = 1
b = 2

if a == 1 && b == 1
    puts "True"
else 
    puts "False"
end

# => "False"
Enter fullscreen mode Exit fullscreen mode

a == 1 was evaluated as true; however, b was evaluated as false. Hence the output we got was false.

The || (or) works a little bit different. Only when one of the conditions is true, it will return true.

a = 1
b = 2

if a == 1 || b == 1
    puts "True"
else 
    puts "False"
end

# => "True"
Enter fullscreen mode Exit fullscreen mode

Lastly the ! (not) is an interesting one as it returns true when the condition is not satisfied.

1 == 1
!(1 == 1)

# => true
# => false 
Enter fullscreen mode Exit fullscreen mode

In this instance, Ruby evaluates the numbers in the parenthesis which is true. However, when the ! (not) operator is used, the value returned is false.

Ternary Operator

The ternary operators is used when you want to make a quick if / else statement.

a = 1

a == 1 ? true : false 

# => true Let's break this down. First, Ruby evaluates whether the statement a == 1 is true or false. If it is true, the code to the left of : gets executed. If the statement is false, the code to the right of : gets executed. You can see it is very similar to an if / else statement, and it is much easier to implement and read.
Enter fullscreen mode Exit fullscreen mode

Lets break this down. First, Ruby evaluates whether the statement a == 1 is true or false. If it is true the code to the left of : gets executed. If the statement is false the code to the right of : gets executed. You can see it is very similar to an if / else statement, also it is much easier to implement/read and quite common in other languages such as Java script.

Summary

We are finally at the end. Today we've covered a few important concepts, conditionals, comparison, logical and ternary operators. All of them play an important role in the program. It does take some time to wrap your head around the conditionals. But once you have a good understanding of it, your code can become much more sophisticated and complex.

Top comments (0)