Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,330 --> 00:00:03,210
We learned that loops can control how many times your code runs.
2
00:00:05,290 --> 00:00:10,090
And there are two types of loops, the loop, which runs code a specific number of times, and the wire
3
00:00:10,090 --> 00:00:12,490
loop, which runs code as long as something is true.
4
00:00:13,150 --> 00:00:16,720
Now, the for loop is good if you know in advance how many times something should run.
5
00:00:17,170 --> 00:00:19,090
And if you don't, then use a while loop.
6
00:00:19,870 --> 00:00:24,340
So in this lesson, you're going to understand when to use for loops and when to use while loops on
7
00:00:24,340 --> 00:00:26,070
top of learning how a while loop works.
8
00:00:26,740 --> 00:00:27,580
First things first.
9
00:00:27,580 --> 00:00:31,120
Inside your Section five folder, make a new class name the while loop Java.
10
00:00:31,120 --> 00:00:32,800
And here it has the main method.
11
00:00:39,050 --> 00:00:42,920
The while loop keeps running code, as long as something is true, the while loop is good.
12
00:00:42,920 --> 00:00:47,220
If you don't know in advance how many times a loop should run, because unlike the for loop a while,
13
00:00:47,270 --> 00:00:51,290
Loop doesn't have any fancy counters to keep track of how many times the code ran.
14
00:00:51,860 --> 00:00:55,460
It's just going to keep running the code as long as some condition remains true.
15
00:00:58,350 --> 00:01:02,850
So it stands to reason that the only thing a while loop needs is a condition and it's going to keep
16
00:01:02,850 --> 00:01:05,160
running until that condition turns false.
17
00:01:06,060 --> 00:01:07,200
Let's see how this works.
18
00:01:10,250 --> 00:01:15,740
In the world of class, I'm going to define a variable that stores a random integer and number is going
19
00:01:15,740 --> 00:01:16,880
to equal twenty five.
20
00:01:19,580 --> 00:01:23,540
Then I'm going to make a while loop, and this loop is going to keep running as long as that number,
21
00:01:23,540 --> 00:01:30,020
which we just defined is less than or equal to 30, as long as this condition remains true, this while
22
00:01:30,020 --> 00:01:34,010
loop is going to keep running and every time it runs, we're going to print that number.
23
00:01:38,120 --> 00:01:39,710
OK, I'm going to run the code.
24
00:01:51,960 --> 00:01:54,400
And to no surprise, the code runs forever.
25
00:01:55,020 --> 00:02:00,300
Now, don't panic, we can start the program by pressing control, see the supplies, the both Mac and
26
00:02:00,300 --> 00:02:05,700
Windows, and if you're using a Mac, it's control C, not command C, so that's really important.
27
00:02:09,530 --> 00:02:13,610
Now, the reason why this happens is because this condition that you see here, a number is smaller
28
00:02:13,610 --> 00:02:14,640
than or equal to 30.
29
00:02:14,930 --> 00:02:20,030
It's always going to be true because the number variable equals twenty five, which is smaller than
30
00:02:20,030 --> 00:02:20,420
30.
31
00:02:20,420 --> 00:02:22,450
And we're not doing anything to change that.
32
00:02:23,150 --> 00:02:26,830
So this condition is always going to be true and the loop is going to keep running forever.
33
00:02:28,050 --> 00:02:33,360
OK, so we need to fix our loop in that every time it runs, it needs to update the number variable
34
00:02:33,360 --> 00:02:38,970
by one and then eventually the variable is going to increase to 31, which will cause this condition
35
00:02:38,970 --> 00:02:40,970
to turn false and break the loop.
36
00:02:41,910 --> 00:02:43,470
So I'll run my code.
37
00:02:49,410 --> 00:02:54,690
And as I predicted, the while loop keeps running as long as the number is smaller than or equal to
38
00:02:54,690 --> 00:02:59,040
30, beyond which the condition turns false and the loop breaks.
39
00:02:59,940 --> 00:03:03,420
So from a diagram perspective, the loop runs six times.
40
00:03:03,810 --> 00:03:05,390
First, the number is twenty five.
41
00:03:05,400 --> 00:03:06,420
The condition is true.
42
00:03:06,420 --> 00:03:07,920
So the loop runs in.
43
00:03:07,920 --> 00:03:13,410
During each run we're increasing the number by one in our condition is going to remain true until the
44
00:03:13,410 --> 00:03:14,190
sixth run.
45
00:03:15,230 --> 00:03:19,940
Because in the sixth run, the number is going to increase to thirty one, the condition turning false
46
00:03:19,940 --> 00:03:21,080
and the loop breaks.
47
00:03:23,600 --> 00:03:28,610
Now, you know me, I like to visualize things from a runtime perspective, so this is how Java runs
48
00:03:28,610 --> 00:03:29,800
the wire loop first.
49
00:03:29,810 --> 00:03:30,830
The number is twenty five.
50
00:03:30,830 --> 00:03:32,000
So the condition is true.
51
00:03:32,480 --> 00:03:36,600
Prince twenty five and then inside the wire, updating the number to twenty six.
52
00:03:36,920 --> 00:03:40,310
Now the code inside the wire loop is done, but the condition is still true.
53
00:03:40,310 --> 00:03:43,210
It's going to keep printing and updating the number again and again.
54
00:03:43,520 --> 00:03:46,370
Now I'm just going to let this play out until we get to the sixth run.
55
00:03:59,880 --> 00:04:04,680
And here the number increases to thirty one, the condition turns false and the while loop breaks.
56
00:04:09,730 --> 00:04:13,060
Now, you might be asking yourself, shouldn't I have used the for loop instead?
57
00:04:14,790 --> 00:04:16,380
Yes, yes, you should have.
58
00:04:18,570 --> 00:04:23,790
The while loop that you created is very specific, and when you do run code a specific number of times,
59
00:04:23,790 --> 00:04:25,060
you need to use a for loop.
60
00:04:25,680 --> 00:04:30,660
In other words, when you know in advance how many times a piece of code needs to run, then use a for
61
00:04:30,660 --> 00:04:35,700
loop because the for loop combines the counter condition and increment in one line.
62
00:04:36,510 --> 00:04:40,590
If you ask me, it's a lot cleaner and the for loop is more compact.
63
00:04:40,780 --> 00:04:45,200
It's much easier to read and it was designed specifically for this purpose.
64
00:04:45,960 --> 00:04:49,470
So back in our code, we're going to reshape this into a for loop.
65
00:04:49,920 --> 00:04:51,360
We can put the start here.
66
00:04:52,020 --> 00:04:53,760
We can put the stop condition.
67
00:04:56,740 --> 00:04:59,080
And here we can put the step, the increment.
68
00:05:01,700 --> 00:05:06,260
And the fact that we can rewrite this while loop is a for loop so easily means that we shouldn't have
69
00:05:06,260 --> 00:05:08,180
been using a while loop to begin with.
70
00:05:16,000 --> 00:05:17,440
So I'm going to rerun the code.
71
00:05:26,250 --> 00:05:29,340
And we get the same output, but our code looks much nicer.
72
00:05:34,190 --> 00:05:39,980
Counter starts at 25 minute increments by one after each run, and it's going to keep running until
73
00:05:39,980 --> 00:05:41,270
I reach thirty one.
74
00:05:51,490 --> 00:05:54,370
At this point, the condition is false and the loop breaks.
75
00:05:58,580 --> 00:06:03,350
So when do I use a while loop, use a while loop, if it's not clear how many times the code should
76
00:06:03,350 --> 00:06:04,910
run, it's as simple as that.
77
00:06:06,630 --> 00:06:07,790
Let me give you an example.
78
00:06:10,130 --> 00:06:15,110
Delete the fallout, because this is a lesson on wire loops and I'm going to make two decimal variables.
79
00:06:15,830 --> 00:06:18,620
The first variable is some random number that you choose.
80
00:06:18,920 --> 00:06:21,950
Double choice is equal to zero point zero one.
81
00:06:22,640 --> 00:06:27,770
And I'm going to make another variable guess which for now is a random guess that I'm going to set to
82
00:06:27,770 --> 00:06:29,150
zero point nine nine.
83
00:06:30,970 --> 00:06:35,530
Now, I'm going to define a while, this while loop is going to keep running as long as gas is bigger
84
00:06:35,530 --> 00:06:36,250
than choice.
85
00:06:44,450 --> 00:06:47,180
In which case, I'm going to print hi, I'm in the wire loop.
86
00:06:51,760 --> 00:06:52,930
Rerunning my code.
87
00:06:56,650 --> 00:07:02,260
And to no surprise, the code runs forever because guess is bigger than choice, so the condition is
88
00:07:02,260 --> 00:07:07,240
always going to be true and the code is always going to run because we're not modifying guests in any
89
00:07:07,240 --> 00:07:07,600
way.
90
00:07:08,640 --> 00:07:13,710
So what I'm going to do is press control, see the stop the output once again, this applies to both
91
00:07:13,710 --> 00:07:15,000
Mac and Windows.
92
00:07:15,330 --> 00:07:18,400
Mac users do not press command, see press control, see.
93
00:07:19,260 --> 00:07:24,750
Now, back in my code, what I can do is inside the wire loop set guests equal to Mathoura random.
94
00:07:26,740 --> 00:07:31,540
And instead of printing the string, I'm going to print the gas variable so that you can see exactly
95
00:07:31,540 --> 00:07:32,320
what's going on.
96
00:07:33,420 --> 00:07:38,760
And so basically what I'm doing is the computer is going to keep guessing random numbers until it comes
97
00:07:38,760 --> 00:07:42,810
up with a number that's smaller than the choice which will turn the looping condition false.
98
00:07:43,590 --> 00:07:45,040
And when is that going to happen?
99
00:07:45,060 --> 00:07:46,380
We don't really know.
100
00:07:46,770 --> 00:07:51,300
The while loop is going to have to keep running while this condition is true and the loop is only going
101
00:07:51,300 --> 00:07:57,120
to break in the event that it comes up with a guess that's smaller than the number that you chose and
102
00:07:57,120 --> 00:07:58,380
that's going to be random.
103
00:07:59,160 --> 00:08:02,400
So we don't know when this loop is going to break, but let's just run our code.
104
00:08:09,930 --> 00:08:16,260
And check it out, our code keeps guessing and no and after many, many loops, after many, many iterations,
105
00:08:16,530 --> 00:08:20,370
it finally guesses a number that smaller than zero point zero one.
106
00:08:21,630 --> 00:08:26,640
In this case, zero point zero zero seven and the loop breaks.
107
00:08:29,970 --> 00:08:34,710
I'd like for you to be able to visualize this, so once again, here we have our condition, which starts
108
00:08:34,710 --> 00:08:37,340
off true because the guess is bigger than choice.
109
00:08:37,679 --> 00:08:38,909
So it keeps running.
110
00:08:39,179 --> 00:08:44,490
And after many, many loops, eventually it's going to guess a number that's smaller than choice.
111
00:08:45,030 --> 00:08:49,320
As soon as it does that, the condition is going to turn false and the loop is going to break.
112
00:08:52,520 --> 00:08:56,510
Now, the one thing I want you to take away from this is that it's impossible to know in advance how
113
00:08:56,510 --> 00:09:00,810
many times this code needs to run before Java comes up with a good guess.
114
00:09:01,460 --> 00:09:07,760
So this is a perfect use case for while loops, because unlike the for loop, a wire loop doesn't use
115
00:09:07,760 --> 00:09:11,650
counters to keep track of how many times each chunk of code needs to run.
116
00:09:12,170 --> 00:09:15,600
It's just going to keep running the code as long as some condition remains true.
117
00:09:16,520 --> 00:09:19,580
So remember, these rules use a for loop.
118
00:09:19,580 --> 00:09:24,740
If you know in advance how many times you need to run a piece of code and use a while loop to run code
119
00:09:24,740 --> 00:09:26,120
as long as something is true.
120
00:09:29,690 --> 00:09:33,890
In this video, you learn how to use a while loop and the while loop will keep running as long as some
121
00:09:33,890 --> 00:09:34,800
condition is true.
122
00:09:35,600 --> 00:09:38,930
First, you call it a wire loop that runs code exactly six times.
123
00:09:54,220 --> 00:09:58,880
But when you need to run code a specific number of times, a for loop is much better.
124
00:09:59,410 --> 00:10:03,220
It combines the start, the stop and the step in one line.
125
00:10:03,880 --> 00:10:06,150
It's more compact and it's much easier to read.
126
00:10:06,760 --> 00:10:12,340
But when you don't know how many times a piece of code needs to run, using a for loop is impossible
127
00:10:12,550 --> 00:10:17,050
because inside of a for loop, you need to specify exactly how many times it needs to run.
128
00:10:19,040 --> 00:10:24,270
So you needed to find a while loop that keeps trying to guess a number that's smaller than yours.
129
00:10:24,620 --> 00:10:28,130
It's impossible to know in advance how many times this code needs to run.
130
00:10:28,400 --> 00:10:31,160
So this was a perfect use case for why loops?
131
00:10:34,460 --> 00:10:39,950
So remember, these rules use a for loop, if you know in advance how many times you need to run a piece
132
00:10:39,950 --> 00:10:45,830
of code and use a while loop to run code, as long as something is true, that is really it.
133
00:10:46,550 --> 00:10:50,080
As long as you keep those two rules in mind, you should be good to go.
134
00:10:50,660 --> 00:10:53,710
And I think now you're ready to tackle the next three workbooks.
135
00:10:53,850 --> 00:10:54,530
Good luck.
13995
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.