1 00:00:00,180 --> 00:00:01,020 ‫Welcome back. 2 00:00:01,050 --> 00:00:04,220 ‫In this video, we are going to have a look at four loops. 3 00:00:04,230 --> 00:00:08,370 ‫Therefore, I'm going to show you the practice of using for loops. 4 00:00:08,370 --> 00:00:10,860 ‫And we are going to start with an example. 5 00:00:10,860 --> 00:00:13,950 ‫So let's go ahead and use the for keyword. 6 00:00:13,950 --> 00:00:19,080 ‫Then in parentheses you have three values or three parts. 7 00:00:19,080 --> 00:00:29,010 ‫So the first part is the counter itself and I use an integer here that's common practice and counter 8 00:00:29,280 --> 00:00:30,480 ‫could be a name. 9 00:00:30,480 --> 00:00:37,140 ‫Usually what you see is I so int I is something that you see quite often when seeing for loops. 10 00:00:37,140 --> 00:00:45,120 ‫So counter then the condition here you compare your counter to a specific value. 11 00:00:45,120 --> 00:00:52,440 ‫So let's say as long as counter is lower than ten, I want to go ahead and run my code. 12 00:00:52,470 --> 00:00:55,320 ‫But now my counter needs to be initialized as well. 13 00:00:55,320 --> 00:00:58,260 ‫So I need to set it, for example, to zero. 14 00:00:58,260 --> 00:01:05,430 ‫So now the counter will run until it's nine because at ten this condition is not met anymore. 15 00:01:06,300 --> 00:01:10,440 ‫So what I finally need is a increment. 16 00:01:10,440 --> 00:01:15,540 ‫So I'm going to increment my counter by one by using the double plus. 17 00:01:15,870 --> 00:01:23,370 ‫So by plus plus I say, okay, increase this counter by one as soon as this iteration of the for loop 18 00:01:23,370 --> 00:01:24,210 ‫is done. 19 00:01:24,660 --> 00:01:27,210 ‫And that's going to happen every single iteration. 20 00:01:27,210 --> 00:01:31,920 ‫So every single time the for loop is run, it will increase by one. 21 00:01:32,280 --> 00:01:37,890 ‫And we can simply see if that's true by using console dot right line. 22 00:01:38,400 --> 00:01:40,980 ‫And here I'm just going to print out the counter. 23 00:01:42,270 --> 00:01:48,390 ‫And now finally we need to use the console that read in order to see something on the console. 24 00:01:48,600 --> 00:01:53,160 ‫So I'm going to run that real quick and then I'm going to go through what we see here once again. 25 00:01:53,160 --> 00:01:59,070 ‫So let's look and we see 012344529. 26 00:01:59,070 --> 00:02:06,090 ‫So it has run ten times, it started at zero and then it went all the way to nine. 27 00:02:06,240 --> 00:02:10,410 ‫So that's what this counter does here or this for loop does here. 28 00:02:10,410 --> 00:02:16,380 ‫So we have a start value which is of type integer and which I call the counter. 29 00:02:16,410 --> 00:02:25,290 ‫Then after the semicolon we have the condition, so we check if counter is lower than ten and then we 30 00:02:25,290 --> 00:02:26,550 ‫increase by one. 31 00:02:26,550 --> 00:02:32,880 ‫So what the general flow here is we set a value to zero or to any other start value. 32 00:02:33,030 --> 00:02:35,280 ‫Then we check if the condition is true. 33 00:02:35,280 --> 00:02:37,320 ‫If that's true, then this code will be executed. 34 00:02:37,320 --> 00:02:44,280 ‫So anything that's inside of the code body or the for loop body will be executed. 35 00:02:44,280 --> 00:02:47,040 ‫Anything that is between those curly brackets. 36 00:02:47,460 --> 00:02:51,360 ‫And in our case, it's simply to write something onto the counter. 37 00:02:51,780 --> 00:02:56,610 ‫And then once that is done, once this part here is executed. 38 00:02:56,610 --> 00:03:03,300 ‫So everything in the body, the counter is increased by an amount, and in this case it's increased 39 00:03:03,300 --> 00:03:03,870 ‫by one. 40 00:03:03,870 --> 00:03:09,960 ‫So it's incremented by one because counter plus plus increments a value by one. 41 00:03:10,500 --> 00:03:14,070 ‫If you want it to decrease a value by one, you would use minus minus. 42 00:03:15,060 --> 00:03:18,210 ‫But in our case, we want to increase it by one. 43 00:03:18,510 --> 00:03:21,110 ‫So that's the general approach for for loop. 44 00:03:21,120 --> 00:03:28,020 ‫Now let's have a look at a for loop which starts at zero, but goes to 50 and increments by five each 45 00:03:28,020 --> 00:03:28,320 ‫time. 46 00:03:28,320 --> 00:03:31,050 ‫So counter plus equals five. 47 00:03:32,100 --> 00:03:37,620 ‫That means that it will increase its value by five each time it executed the code. 48 00:03:37,770 --> 00:03:39,270 ‫So let's run it again. 49 00:03:41,630 --> 00:03:46,100 ‫And that we are zero five, ten, 15 and so forth. 50 00:03:46,550 --> 00:03:49,700 ‫So what our code does here is do the following. 51 00:03:50,240 --> 00:03:51,590 ‫We start at zero. 52 00:03:51,950 --> 00:03:54,590 ‫It checks is zero lower than 50. 53 00:03:54,920 --> 00:03:55,830 ‫And that's true. 54 00:03:55,860 --> 00:04:01,520 ‫So let's run the code so it executes and it writes zero onto our console. 55 00:04:01,550 --> 00:04:05,690 ‫Then it increases itself by five, so it increments by five. 56 00:04:05,720 --> 00:04:09,690 ‫The next time it checks, OC is five lower than 50. 57 00:04:09,710 --> 00:04:10,460 ‫That is true. 58 00:04:10,490 --> 00:04:10,880 ‫All right. 59 00:04:10,880 --> 00:04:15,560 ‫So the run the code, then it's increased or incremented by five once again. 60 00:04:15,560 --> 00:04:16,550 ‫So now it's ten. 61 00:04:16,580 --> 00:04:18,170 ‫The counter checks is ten. 62 00:04:18,170 --> 00:04:18,980 ‫Lower 50. 63 00:04:19,010 --> 00:04:19,760 ‫Yes, it is. 64 00:04:19,790 --> 00:04:20,180 ‫All right. 65 00:04:20,180 --> 00:04:26,790 ‫Let's run the code and so forth until we are at the point where this condition is not met anymore. 66 00:04:26,810 --> 00:04:31,610 ‫So let's say we were at 45 and now we increase the number by five. 67 00:04:31,610 --> 00:04:34,260 ‫So we add another five, we increment by five. 68 00:04:34,280 --> 00:04:43,070 ‫Then the condition here is not met anymore because it asks here or checks is 50 lower than 50 and because 69 00:04:43,070 --> 00:04:47,840 ‫50 is not lower than 50 or smaller than 50, this condition is not met anymore. 70 00:04:47,840 --> 00:04:52,910 ‫So the code is not executed anymore and we get outside of this loop. 71 00:04:52,910 --> 00:04:57,470 ‫So we get to this code here, console read. 72 00:04:57,620 --> 00:05:06,140 ‫So what we also could do just to see how that is true, we can simply write something like for loop 73 00:05:06,170 --> 00:05:10,610 ‫is done and then we will see for loop is done. 74 00:05:11,420 --> 00:05:13,400 ‫So after all of that is done. 75 00:05:13,640 --> 00:05:17,390 ‫Condition is not valid anymore and it's done. 76 00:05:17,720 --> 00:05:24,770 ‫We could even write something like counter plus is lower than 50. 77 00:05:26,540 --> 00:05:34,080 ‫So now if we run it again, it says zero is lower than 50, five is lower than 50 and so forth. 78 00:05:34,100 --> 00:05:38,480 ‫So this is the condition and it's met all the time and then the for loop is done. 79 00:05:38,840 --> 00:05:41,580 ‫And that's generally how we for loop works. 80 00:05:41,600 --> 00:05:42,020 ‫All right. 81 00:05:42,020 --> 00:05:43,430 ‫Now a little challenge for you. 82 00:05:43,460 --> 00:05:50,660 ‫Please go ahead and write a for loop, which only prints out the odd numbers from 0 to 20. 83 00:05:51,170 --> 00:05:52,640 ‫So go ahead and build that. 84 00:05:52,640 --> 00:05:57,980 ‫Just to counter my for loop counter, which only prints out the odd numbers. 85 00:06:01,390 --> 00:06:14,560 ‫All right let's go ahead for and counter is equal to one counter lower than 20 counter plus plus and 86 00:06:14,560 --> 00:06:17,010 ‫equal actually plus equal to. 87 00:06:17,020 --> 00:06:19,680 ‫So we want to increase it by two each time. 88 00:06:19,690 --> 00:06:22,600 ‫And now let's write that onto the console. 89 00:06:22,600 --> 00:06:24,580 ‫So console right line. 90 00:06:25,420 --> 00:06:36,310 ‫And here I write the counter and here again console, dot read and now we can go ahead. 91 00:06:37,690 --> 00:06:38,050 ‫All right. 92 00:06:38,050 --> 00:06:39,940 ‫And then we are all the old numbers. 93 00:06:39,940 --> 00:06:43,030 ‫One, three, five, seven, nine and so forth. 94 00:06:43,690 --> 00:06:44,230 ‫Great. 95 00:06:44,230 --> 00:06:51,010 ‫So now you have seen how to create odd numbers and how to create for loops in general. 96 00:06:51,550 --> 00:06:55,840 ‫And the next video, we are going to have a look at the do while loop. 97 00:06:55,840 --> 00:06:56,950 ‫So see you there.