Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A loop conditional will exit a loop. A loop will run forever until the condition to exit the loop returns the value of true. If the condition returns the value of false, the loop will not exit. If the condition never returns true, we'll create an infinite loop.
Links
Code Samples
Random number guessing program:
loop_conditional.rb
random_number = Random.new.rand(5)
loop do
print "Guess the number between 0 and 5 (e to exit): "
answer = gets.chomp
if answer == "e"
puts "The number was #{random_number}."
break
else
if answer.to_i == random_number
puts "You guessed correctly!"
break
else
puts "Try again."
end
end
end
Program to exit a loop when a number greater than 10 is entered:
loop_conditional_number.rb
loop do
print "Enter a number greater than 10 to exit: "
answer = gets.chomp.to_i
break if answer > 10
end
Program to loop through asking for someone's name and make sure it is formatted correctly:
def get_name
name = ""
loop do
print "Enter your name (minimum 2 characters, no numbers): "
name = gets.chomp
if name.length >= 2 && !name.index(/\d/)
break
else
puts "Name must be longer than 2 characters and not contain numbers."
end
end
return name
end
name = get_name()
puts "Hi #{name}."
-
0:00
As we just saw, a loop will run forever until a condition is met.
-
0:05
When we say that the condition is met,
-
0:08
that means that the condition to exit the loop returns the value of true.
-
0:14
If the condition returns the value of false, the loop will not exit.
-
0:19
If the condition never returns true, we'll create an infinite loop.
-
0:25
We've already learned about Conditionals and Complex Conditionals.
-
0:29
So feel free to brush up on those using the course linked below in the notes.
-
0:34
Let's do a quick review now on writing conditionals specifically to exit a loop
-
0:39
using workspaces.
-
0:41
Using a Ruby workspace, let's go ahead and create a new file, and
-
0:45
we're going to call this loop_conditional.rb.
-
0:52
So here we go.
-
0:52
Now let's go ahead and
-
0:54
make a quick little program to guess a number between zero and five.
-
1:00
Now we'll do this by generating a random number and
-
1:04
we can do that using the random class and we'll have a link
-
1:08
to the documentation right below the video here in the Teacher Notes section.
-
1:13
But the way we create a random number is by typing Random.new.
-
1:20
And then we can give it an argument which is the maximum that we want.
-
1:26
So let's go ahead and create this.
-
1:28
We'll do a random number here.
-
1:31
And we do that by saying, Random.new.rand(5).
-
1:37
We'll just have a number between zero and five.
-
1:40
And then let's create a loop to make somebody guess the number.
-
1:46
We'll say Guess the number between 0 and 5 (e to exit).
-
1:56
And then we will get the answer from standard input.
-
2:01
And let's first check to see if they have entered e to exit.
-
2:06
So if the answer is e.
-
2:10
We will break to exit the loop.
-
2:14
And if not, we'll see if they guessed correctly.
-
2:20
Then we'll save the answer and we'll make this an integer instead.
-
2:26
We will print to the screen, You guessed correctly.
-
2:31
And then we will break and if not,
-
2:36
we'll print out, Try again.
-
2:43
And actually, let's go ahead and
-
2:45
tell them what the number was if they just simply exit the program.
-
2:51
Okay, now let's go ahead and run this by
-
2:56
typing ruby loop_conditional.rb.
-
3:03
So let's guess the number.
-
3:04
How about two?
-
3:06
No.
-
3:06
Three?
-
3:08
Uh-oh.
-
3:09
Four?
-
3:10
I guessed correctly and it exited.
-
3:13
We have a bit of a complicated if statement here.
-
3:16
The first thing that we check is to make sure that the answer is not exit,
-
3:22
and if the answer is an exit, we'll print out the random number.
-
3:27
The important part is using the break statement.
-
3:30
If we don't break, then we get in an infinite loop.
-
3:34
And let's go ahead and just make sure that works again.
-
3:37
And e to exit, and the number was one, okay.
-
3:41
So, it looks like all of the different execution paths are working correctly.
-
3:47
And let's go ahead and
-
3:48
create another file and just make sure that it works with numbers too.
-
3:54
I'm calling this one loop_conditional_number.
-
4:00
And let's just ask the user to enter a number greater than ten to exit.
-
4:11
And we'll do the same thing, but
-
4:13
all in one go we'll get the answer from standard input.
-
4:17
And then we will exit the loop if the number is greater than ten.
-
4:23
And so we can say, if answer is greater than ten, we will break.
-
4:33
Let's run that.
-
4:36
And see what happens, enter a number greater than ten.
-
4:39
I entered two, nothing happens.
-
4:43
I enter 11 and it exits, which is what we wanted.
-
4:48
One neat thing about if statements is that they can be written on one line,
-
4:53
which can be a little bit more clear depending on how you're reading the code.
-
4:58
So if we run this again, We
-
5:02
can see that it properly exits when we keep the if statement all on one line.
-
5:07
Okay, now let's do one more, and we're gonna call this loop_name.rb.
-
5:14
I'm actually gonna paste the code in, and then we'll see how it works.
-
5:19
So, right here we have got a method called get_name.
-
5:24
And what this does is create a variable called name which is set to an empty
-
5:28
string.
-
5:29
Then we loop through, ask the user to enter their name, and
-
5:34
then get that from standard input.
-
5:37
From there, we check to make sure that the name meets the criteria
-
5:42
that we've set forth, which is that it's a minimum of two characters and
-
5:46
doesn't contain any numbers.
-
5:48
So if all that checks out, then we break out of the loop and
-
5:52
return the name variable from the get_name method.
-
5:58
And if that criteria is not met, we print out a message saying
-
6:03
that the name must be longer than two characters and not contain any numbers.
-
6:09
This right here is a method called index which uses
-
6:13
a regular expression to check whether or not there are any digits in the name.
-
6:19
We'll have a link below to more information on regular expressions.
-
6:23
You don't need to worry about them too much right now.
-
6:25
Just know that it checks the name for any digits.
-
6:30
Now let's go a head and run this and see what happens.
-
6:34
So I'll click down into the console and type ruby loop_name.rb.
-
6:41
Enter my name.
-
6:42
I'll just put J for Jason, and now it says that my name must be longer than two
-
6:47
characters and not contain numbers.
-
6:49
This is what I was expecting.
-
6:51
So let's try J2.
-
6:54
Same message.
-
6:55
Now if I typed in my name correctly, it says hi Jason.
-
7:03
You'll notice that we can use complex conditionals to break out of loops.
-
7:08
Try practicing writing some loop conditionals on your own now
-
7:12
using workspaces.
You need to sign up for Treehouse in order to download course files.
Sign up