- explain the flow of execution through a chunk of code
ifstatements to control execution
- use an
elsestatement to create an alternative path
elseto create multiple branches
untilto repeat instructions
- apply the
timesmethod to repeat instructions
breakto repeat instructions
- break out of an infinite loop in both IRB and regular Ruby
- conditional branching
- flow control
- infinite loop
You’re going to learn different ways to accomplish the same thing in this lesson. Remember that these are tools, and as you learn to be a software developer, you’ll get a better idea of which tool to use for which job. For now, just try to understand how the tool works, and at least one use for that tool.
In programming, we refer to something that is either
false as a Boolean.
A condition is something that evaluates to a Boolean. This can be as simple as a variable that holds a Boolean value:
play_again = false
We can also use comparison operators to create a condition by comparing two values. The important comparison operators are:
- Be careful not to mix this up with
- Be careful not to mix this up with
>=greater than or equal to
<=less than or equal to
We can use them like so:
mood = "hungry" mood == "sleepy" #=> false mood.length > 5 #=> true mood != "grumpy" #=> true
You can also use the negation operator
! (also known as a “bang”) to reverse something from true to false.
play_again = true !play_again #=> false
There are also methods that can be used as conditions. Although it’s not a rule, typically these methods end in a
1.even? #=> false "hello".include? "h" #=> true
We can use the “or” operator
|| and the “and” operator
&& to combine two conditions into a single condition.
|| evaluates to true if one of the conditions is true.
&& evaluates to true if both are true:
breed = "Corgi" age = 2 breed == "Corgi" || age == 3 #=> true breed == "Corgi" && age == 3 #=> false
Be careful… a common mistake is to try to use
|| with two possible values. If we want to say “the count is either 0 or 10”, you may try something like this:
count = 5 count == 0 || 10
This won’t give you an error, but it isn’t working like you expect. For reasons we will discuss later, this condition will always evaluate to true, which might not be what you expect. If we read this as “count is equal to zero or ten”, it makes sense to us, but that’s not how Ruby reads it. Ruby evaluates each condition on the left and right independently and then combines them. So Ruby reads it as “Count is equal to zero; or ten.”. The important point here is that both sides of an
&& are valid conditions. This statement would be correctly written as:
count = 5 count == 0 || count == 10
In programming, branching refers to a choice that is made depending on whether or not a condition is true or false. Think of branching as “choose your own adventure”.
- If a student earns a 3.8 GPA or higher, then they are invited to the honor roll ceremony. (One branch)
- If you want to spend a lot of money for dinner, go to a fancy restaurant. Otherwise, cook at home. (Two branches)
All of our conditional branches will begin with an
if. The code following the
if will run if the condition is true.
if condition # code to execute if condition is true end
elsif to create more branches.
if condition1 # code to execute if above condition1 evaluates to true elsif condition2 # code to execute if above condition2 evaluates to true elsif condition3 # code to execute if above condition3 evaluates to true end
Code inside an
else will run when none of the previous conditions are true.
if condition1 # code to execute if above condition1 evaluates to true elsif condition2 # code to execute if above condition2 evaluates to true elsif condition3 # code to execute if above condition3 evaluates to true else # code to execute if all previous conditions evaluate to false end
- Conditional branches have exactly one
ifcan be following by any number of
- A conditional branch will have either zero or one
elsecomes after the
- The conditional branch always ends with an
- Only one branch can be taken.
- Conditions are evaluated in order.
Check for Understanding
What will the following code print to the screen?
play_again = true lives = 3 if lives == 0 puts "You Lose!" elsif !play_again puts "Game Over!" elsif play_again && lives > 0 puts "Welcome back!" else puts "invalid input" end
A loop is a set of instructions that is executed repeatedly until some condition is met. This condition may be a certain number of times that the loop is executed, for example:
- After baking cookies, you pull the cookie sheet out of the oven which holds 24 cookies. One by one, you remove each of the cookies from the sheet and place them on a cooling rack. (24.times do…) (Set of instructions that executes 24 times)
or it may be a question that returns a true/false (boolean) answer. For example:
- While looking for a parking spot at a crowded sporting event, a car continues to drive up and down the rows until an empty spot is found (full == false).
(Loop that executes until a question returns true or false)
times loop executes code an exact number of times.
5.times do # code to execute given number of times end
We can also include a Block Variable that tells us which iteration of the loop is running.
5.times do |number| puts number end
will print out
0 1 2 3 4
while condition # code to execute as long as condition evaluates to true end
while parking_spot.full? keep_driving end
until condition # code to execute if above condition evaluates to false, stop when condition evaluates to true end
until parking_spot.empty? keep_driving end
loop do allows you to run code in an infinite loop.
loop do # code will run forever end
You can use the
break keyword to end a
count = 0 loop do count += 1 if count == 3 break end end
If you accidentally get stuck in an infinite loop, use
control + c to stop it.