A control flow construct is a language feature which disrupts the normal progression to the next statement and conditionally or unconditionally branches to another location in source code. –– Robert Klemme
In other words, control flow lets you tell your program what code to execute conditionally. As humans, we actually enact flow control every day. For instance, if you are hungry, you will go and get a snack. Otherwise, you'll stay put and continue to read this awesome readme.
Control flow is an important part of Ruby programming and web development. In the context of a web application, for example, you can easily think of content or functionality on a website you've visited that is only available to a user if that user is logged in.
There are several ways to tell your program to conditionally execute certain code, the basic forms of which are:
In this reading, we're going to discuss the first group of these "conditional"
Use IRB, copying the provided code snippets, to follow along in this lesson.
One of the most common ways to enact control flow is the
Whatever block of code that follows the
if statement will get evaluated—i.e.
read and enacted by the computer. If this evaluation of the
true, then the code through to the associated
end statement will
Let's look at a few examples:
if 5 > 2 print "5 is greater than 2" end
ifstatement evaluates as
if 2 > 5 puts "2 is greater than 5" end
ifstatement evaluates as
So what if we want our program to print something else when the
To accomplish this, we can follow an
if statement with an
else statement. Take a look:
if false puts "This will never get printed because the above statement evaluates to false." else puts "This will get printed!" end
else statement sets a "default" condition for when your
conditional evaluates as
false. Every condition that doesn't evaluate as
true will instead pass through the
So far, we've seen
if statements that rely on the explicit use of the
false booleans. Let's look at some examples that require a little more
if 6 + 3 == 9 puts "Giraffes have no vocal cords." end # └── "Giraffes have no vocal cords."
Giraffes have no vocal cords.Since
6 + 3equals
9is equal to
ifstatement's conditional evaluates as
Top-tip: Remember that the comparative operator
== ("double-equals") is
used to check equality. This is distinct from the assignment operator
=("single-equals"), which is used to set the value of a variable.
if 6 + 3 < 5 puts "The hummingbird is the only animal that can fly backwards" end
6 + 3, which is equivalent to
9, is not less than
5, making the
ifstatement's conditional evaluate as
dog = "satisfied" if dog == "hungry" puts "Refilling food bowl." else puts "Reading newspaper." end # └── "Reading newspaper."
Sometimes, we want to control the flow of our program based on more than one condition. For example, if I am hungry, then I will get a snack. If I am thirsty, then I will get a drink of water. Otherwise, I will stay here and continue learning more about control flow.
We can add additional layers of complexity to our
else statements by
Let's add an
elsif statement to Example 3 from above:
dog = "thirsty" if dog == "hungry" puts "Refilling food bowl." elsif dog == "thirsty" puts "Refilling water bowl." else puts "Reading newspaper." end # └── "Refilling water bowl."
We can cascade as many
elsif statements as we wish, however
can only be used following an
if statement, and must precede the associated
else statement (if used).
dog = "cuddly" if dog == "hungry" puts "Refilling food bowl." elsif dog == "thirsty" puts "Refilling water bowl." elsif dog == "playful" puts "Playing tug-of-war." elsif dog == "cuddly" puts "Snuggling." else puts "Reading newspaper." end # └── "Snuggling."
That's all for now—we'll discuss
case statements and looping in upcoming
View Control Flow on Learn.co and start learning to code for free.