WEBVTT

1
00:00:00.000 --> 00:00:04.659
[MUSIC]

2
00:00:04.659 --> 00:00:07.500
Hi I'm Jason, a Ruby developer.

3
00:00:07.500 --> 00:00:11.840
In this course we're going to look
at loops and iteration in Ruby.

4
00:00:11.840 --> 00:00:14.310
So far when we've been
writing our programs and

5
00:00:14.310 --> 00:00:18.470
want to do something more than one
time we've written those things out.

6
00:00:19.520 --> 00:00:24.380
In the Ruby Collections course, when we
were adding items to our grocery list, we

7
00:00:24.380 --> 00:00:30.860
wrote a method called add list item, which
added a new item to our grocery list.

8
00:00:30.860 --> 00:00:35.010
But this tedious and computers
are really good at doing things for us.

9
00:00:36.070 --> 00:00:40.020
Another way we could have written that
program is using something called a Loop.

10
00:00:41.110 --> 00:00:45.520
A loop is a piece of code that
will run the statements inside

11
00:00:45.520 --> 00:00:47.120
until some condition is met.

12
00:00:48.340 --> 00:00:53.870
Using the grocery list example, we could
have a loop ask a user if they wanna keep

13
00:00:53.870 --> 00:00:58.490
entering grocery list items until they
type done, or no, or something similar.

14
00:01:00.230 --> 00:01:04.010
There are several different kinds
of loops available in Ruby.

15
00:01:04.010 --> 00:01:08.060
We can write a loop
using the loop keyword.

16
00:01:08.060 --> 00:01:12.049
Let's see how that works
now using workspaces.

17
00:01:12.049 --> 00:01:16.320
Okay, so
I have just launched a new Ruby workspace.

18
00:01:16.320 --> 00:01:19.720
If you see the welcome document,
you can go ahead and close it.

19
00:01:21.560 --> 00:01:24.140
Now let's go ahead and create a new file.

20
00:01:25.150 --> 00:01:30.549
And we'll call this loop.rb.

21
00:01:30.549 --> 00:01:37.710
In order to create a loop, we use
the loop keyword followed by the word do.

22
00:01:39.770 --> 00:01:43.480
And then, we close this, by typing end.

23
00:01:44.920 --> 00:01:48.630
Now, whatever is inside,
what is called a block here,

24
00:01:49.690 --> 00:01:54.825
between the do and end, will be run,
until we tell it to stop.

25
00:01:55.825 --> 00:01:58.994
So let's go ahead and
just print something out here.

26
00:02:01.054 --> 00:02:03.114
We'll just say, do you want to continue?

27
00:02:07.374 --> 00:02:08.814
And then we'll get the answer.

28
00:02:13.714 --> 00:02:18.821
Now I've saved the file and
let's click down here

29
00:02:18.821 --> 00:02:24.914
into the console area and
run this by typing ruby loop.rb.

30
00:02:27.414 --> 00:02:30.014
Now we see this asks do
we want to continue.

31
00:02:30.014 --> 00:02:33.114
And I will type y.

32
00:02:33.114 --> 00:02:36.401
Actually, we can type
anything we want here.

33
00:02:36.401 --> 00:02:39.120
And this is going to continue forever.

34
00:02:40.160 --> 00:02:45.780
Because we haven't told it to do anything,
or given it any way to exit the loop.

35
00:02:45.780 --> 00:02:50.920
We can stop this loop
by holding the Ctrl+C.

36
00:02:50.920 --> 00:02:56.160
That will give us this message
saying that it has been interrupted.

37
00:02:56.160 --> 00:02:57.985
Don't worry about that.

38
00:02:57.985 --> 00:03:02.270
Ctrl+C is going to cancel
the currently running program, or

39
00:03:02.270 --> 00:03:03.670
in this case, interrupt it.

40
00:03:05.225 --> 00:03:07.760
You'll notice that this
loop goes on forever.

41
00:03:08.860 --> 00:03:11.040
This is something called an Infinite Loop.

42
00:03:12.440 --> 00:03:17.500
An infinite loop occurs when there's
no condition to exit the loop,

43
00:03:17.500 --> 00:03:21.180
or the condition that would
exit the loop can never be met.

44
00:03:22.190 --> 00:03:25.870
Let's modify the loops so
that it has a way to exit.

45
00:03:25.870 --> 00:03:30.030
When we look back up here,
we see loop, and then do, and end.

46
00:03:31.110 --> 00:03:34.670
Do and
end is something called a block in Ruby.

47
00:03:35.890 --> 00:03:41.580
Blocks are written one of two ways,
using either the do and end keywords.

48
00:03:41.580 --> 00:03:43.939
You can also use curly braces.

49
00:03:47.339 --> 00:03:50.630
And if we run this again,
we should see the same thing.

50
00:03:50.630 --> 00:03:54.505
I'm gonna clear my screen
here by holding the Ctrl+L.

51
00:03:59.320 --> 00:04:03.780
And we see if we continue,
we get the same thing.

52
00:04:04.790 --> 00:04:09.580
And once again,
I'm going to press Ctrl+C to exit.

53
00:04:09.580 --> 00:04:16.600
And I'm going to change this back
from curly braces to do and end.

54
00:04:16.600 --> 00:04:19.780
Blocks can be written either way,
with curly braces or

55
00:04:19.780 --> 00:04:22.150
with the do and end key words.

56
00:04:22.150 --> 00:04:25.910
By convention,
most Ruby programmers will use do and

57
00:04:25.910 --> 00:04:30.730
end if the statements inside the block
take up more than one line of code.

58
00:04:30.730 --> 00:04:35.950
If it only takes up one line of code,
it's convention to use the curly braces.

59
00:04:37.200 --> 00:04:40.080
So let's go ahead and
fix this infinite loop here.

60
00:04:40.080 --> 00:04:42.220
And we'll actually do
something with the answer.

61
00:04:43.590 --> 00:04:49.340
We'll say if the answer is a lower case

62
00:04:49.340 --> 00:04:54.220
n, then we need to exit this loop somehow.

63
00:04:56.540 --> 00:04:59.570
We can do that using the break keyword,

64
00:04:59.570 --> 00:05:01.930
which will break us out
of the current loop.

65
00:05:04.300 --> 00:05:05.080
I'm gonna run this again.

66
00:05:05.080 --> 00:05:07.080
Let me clear my screen here.

67
00:05:07.080 --> 00:05:10.349
And if I type ruby loop.rb.

68
00:05:10.349 --> 00:05:11.329
Do you want to continue?

69
00:05:11.329 --> 00:05:12.449
Yes.

70
00:05:12.449 --> 00:05:14.170
Do you want to continue?

71
00:05:14.170 --> 00:05:14.670
No.

72
00:05:15.960 --> 00:05:18.980
And that exits the loop and
also the program.
