Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,490 --> 00:00:03,860
We can use for loops to run code many times.
2
00:00:05,110 --> 00:00:10,240
So far, you learn to store that information using variables, control how your code runs, using a
3
00:00:10,240 --> 00:00:16,120
false and switch and organize your code into functions, every step that you take is a step closer to
4
00:00:16,120 --> 00:00:18,190
becoming a professional Java developer.
5
00:00:18,880 --> 00:00:25,060
And your next step to Java greatness is to control how many times a piece of code needs to run, essentially
6
00:00:25,060 --> 00:00:26,470
by running your code in a loop.
7
00:00:30,440 --> 00:00:33,200
So in this lesson, I'm going to teach you how to use a for loop.
8
00:00:36,210 --> 00:00:41,910
First thing you need to do is create a new Java project named Section five and wow, time really does
9
00:00:41,910 --> 00:00:42,660
fly, doesn't it?
10
00:00:42,690 --> 00:00:44,160
We're already at Section five.
11
00:00:44,640 --> 00:00:50,370
And inside the project, create a new file named for Leubsdorf, Java, and as always, make sure the
12
00:00:50,370 --> 00:00:51,630
class has a main method.
13
00:00:56,990 --> 00:01:01,910
Loops can control how many times a block of code needs to run, and there are two types of loops.
14
00:01:01,940 --> 00:01:04,340
There's the for loop and then there's the wire loop.
15
00:01:04,849 --> 00:01:07,940
As we dive deeper into the section, I want you to keep this in mind.
16
00:01:08,390 --> 00:01:12,500
Use a for loop when you know in advance how many times you need to run a piece of code.
17
00:01:13,370 --> 00:01:19,040
I'm going to repeat this one more time, use a for loop when you know in advance how many times you
18
00:01:19,040 --> 00:01:20,210
need to run a piece of code.
19
00:01:21,200 --> 00:01:26,930
This is really important information that I'm going to put in your cheat sheet, OK, and use a while
20
00:01:26,930 --> 00:01:32,150
loop when it's not clear how many times you need to run a piece of code, because unlike the for loop,
21
00:01:32,150 --> 00:01:36,770
the wire loop doesn't have any fancy counters to keep track of how many times a chunk of code needs
22
00:01:36,770 --> 00:01:37,240
to run.
23
00:01:37,850 --> 00:01:41,670
It's just going to keep running the code as long as some condition remains true.
24
00:01:42,890 --> 00:01:44,930
So we'll start with the for loop.
25
00:01:47,340 --> 00:01:52,950
The Furler can run code a specific number of times, it loops through code for an exact number of times
26
00:01:52,950 --> 00:01:54,240
and then the loop breaks.
27
00:01:59,150 --> 00:02:00,890
Now, the for Loop has three parts.
28
00:02:01,300 --> 00:02:07,790
There is the start, this loop starts running with I equalling one, and then there's this top condition.
29
00:02:08,389 --> 00:02:12,320
This loop is going to keep running as long as I is less than or equal to three.
30
00:02:12,320 --> 00:02:13,640
And then the loop stops.
31
00:02:15,320 --> 00:02:21,590
Then there's the step size I is going to increase by a step size of one every time the loop runs.
32
00:02:22,130 --> 00:02:26,090
Now, this might seem like a lot to digest, but it's going to become easier once we start using for
33
00:02:26,090 --> 00:02:26,900
loops in our code.
34
00:02:29,850 --> 00:02:34,440
So we're going to add this for a loop in Maine, the for loop is going to start running with equaling
35
00:02:34,440 --> 00:02:36,360
one and I equals one.
36
00:02:37,450 --> 00:02:43,330
And it's going to keep running as long as I is less than or equal to three, and every time it runs,
37
00:02:43,330 --> 00:02:45,520
we're going to increase the value by one.
38
00:02:47,680 --> 00:02:53,830
OK, so my stop condition is I is less than or equal to three because I want the code to run three times.
39
00:02:56,740 --> 00:03:01,510
You're going to see exactly how this plays out really soon, but every time this loop runs, we're going
40
00:03:01,510 --> 00:03:04,270
to print the counter I print line I.
41
00:03:07,050 --> 00:03:08,130
I'm going to run the code.
42
00:03:18,640 --> 00:03:23,860
And as I predicted, the code runs three times, the code keeps running as long as it is smaller than
43
00:03:23,860 --> 00:03:25,900
or equal to three and then the loop breaks.
44
00:03:30,780 --> 00:03:36,840
So the counter starts at 1:00, the condition is true, the code runs and after the code runs, AA plus
45
00:03:36,840 --> 00:03:38,610
plus increases the counter by one.
46
00:03:39,000 --> 00:03:44,070
Now we're at the second run and this is going to keep happening as long as I is less than or equal to
47
00:03:44,070 --> 00:03:44,430
three.
48
00:04:03,640 --> 00:04:08,320
Once I isn't smaller than or equal to three anymore, the condition is going to be false and the loop
49
00:04:08,320 --> 00:04:09,070
is going to break.
50
00:04:13,640 --> 00:04:17,510
Now, here's how it looks from a visual perspective, the for loop starts with a counter.
51
00:04:17,540 --> 00:04:18,200
I have one.
52
00:04:18,470 --> 00:04:24,230
So the condition is true and the code runs in after the code runs, I plus plus increases the counter
53
00:04:24,230 --> 00:04:25,070
value by one.
54
00:04:25,430 --> 00:04:26,840
The counter is now two.
55
00:04:27,170 --> 00:04:31,970
And this is just going to keep happening until I is no longer less than or equal to three.
56
00:04:41,770 --> 00:04:46,780
And once that happens, the condition is going to be false and the brakes were essentially designed
57
00:04:46,780 --> 00:04:48,910
iroko to only run three times.
58
00:04:53,040 --> 00:04:57,630
In this lesson, you coded a for loop, and it controls how many times a block of code needs to run.
59
00:05:00,690 --> 00:05:05,100
Philip is useful when you know in advance, when you know exactly how many times you want to run your
60
00:05:05,100 --> 00:05:05,490
code.
61
00:05:08,080 --> 00:05:13,150
We wrote a for loop and we designed it to run exactly three times the for loop starts with equaling
62
00:05:13,150 --> 00:05:17,050
one and the loop keeps running as long as I smaller than or equal to three.
63
00:05:23,670 --> 00:05:24,930
And then the loop breaks.
64
00:05:28,490 --> 00:05:31,400
And now that you know how to use for loops, the world is your oyster.
65
00:05:31,610 --> 00:05:36,600
There are so many apps that you can now build, so strap in and get ready for the workbook bonanza.
66
00:05:36,890 --> 00:05:37,610
It's coming up.
6807
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.