Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
The while loop is similar to the loop statement and it uses a conditional to perform the logic. However, the big difference is that the while loop continues to run as long as the conditional that is set up front continues to return true. The condition is also specified as part of the argument to the while loop.
Concepts
Iterator variable
Code Samples
Here is a simple while loop:
answer = ""
while answer != "n"
print "Do you want me to repeat this pointless loop again? (y/n) "
answer = gets.chomp.downcase
end
Example while loop with exit conditional as a number:
def print_hello(number_of_times)
i = 0
while i < number_of_times
puts "hello"
i += 1
end
end
answer = 0
while answer < 5
print "How many times do you want to print 'hello'? (Enter a number greater than 5 to exit) "
answer = gets.chomp.to_i
print_hello(answer)
end
There are a few different
variations of the loop in Ruby.
0:00
One of those is the while loop.
0:04
The while loop is similar
to the loop statement and
0:06
it uses a conditional
to perform the logic.
0:10
However, the big difference is that
the while loop continues to run
0:13
as long as the conditional that is
set up font continues to return true.
0:18
The condition is also specified as
part of the argument, the while loop.
0:24
Let's see how that works now,
using work spaces.
0:29
Using a Ruby work space,
let's go ahead and
0:33
create another file called while _loop.rb.
0:35
And let’s go ahead and
write a very simple while loop.
0:42
We’re going to say the answer
is an empty string and
0:45
while the answer does
not equal the letter n,
0:51
we’ll print out do you want me to
repeat this pointless loop again?
0:56
And then we'll get the answer
from standard input,
1:06
remove trailing spaces,
and make it lowercase.
1:09
We write a while loop similar to the way
we write a regular loop except we
1:13
use the while keyword, and then we set the
condition at the top of the while loop.
1:18
As soon as that condition is met
it will exit the while loop.
1:25
Let’s run this and see how it goes.
1:30
Do I want to repeat
the pointless loop again?
1:34
Yes.
1:36
Do I want to repeat
the pointless loop again?
1:37
No.
1:40
And we can see that will
exit the while loop.
1:41
When we use a while_loop, there's no
need to manually use the break key word.
1:44
Now let's create another
file called while_number.rb.
1:54
And clear my screen there.
2:00
And we're going to create a very
simple method that prints hello,
2:04
and takes one argument which is
the number_of_times, to print hello.
2:10
And what we're gonna do is create
a simple variable here called i.
2:17
When you're writing loops,
it's conventional to use the letter i,
2:22
j, and k to iterate over the loop.
2:27
The reason that we use a lower-case i
2:32
is because that's going to
means it's an iterator.
2:34
So now we'll say, while i is less than
the number_of_times, which is the argument
2:38
to the function, will print hello,
and then this is really important,
2:45
we increment i or
else we'll cause an infinite loop.
2:52
So, now that we've got this method,
let's go ahead and call it.
3:00
We'll create an empty answer string
3:05
And we'll say while
the answer is less than 5,
3:11
because we don't want to be
printing this out too much.
3:14
We'll print,
how many times do you want to print hello?
3:19
And add a little message,
enter number greater than five to exit.
3:27
Then we'll get the answer and
3:35
convert it to an integer and
then we can call that method.
3:39
With the answer.
3:46
Now let's go ahead and run this.
3:51
Oh, comparison of string with five
failed and that's on line 10.
3:55
Let's go ahead and change the answer
to zero instead of a string.
4:00
Run that again.
4:09
How many times do I want to print hello?
4:11
How about 3.
4:13
Okay, it printed three times.
4:14
It printed two times.
4:19
And now let me type the number 6.
4:21
And then it prints six times and exits.
4:25
The reason that prints hello six times and
then exits,
4:29
is because print_hello is
still part of the loop.
4:34
This block of code will not be
run again until after It exits.
4:39
The while loop will then
evaluate the condition
4:45
in the context of the end
of the block of code.
4:49
Try practicing writing while loops
on your own now using WorkSpaces.
4:54
You need to sign up for Treehouse in order to download course files.
Sign up