1 00:00:01,530 --> 00:00:02,070 ‫In this video. 2 00:00:02,070 --> 00:00:08,850 ‫We are going to have a look at the do while loop and it differs to the other loops by not checking the 3 00:00:08,850 --> 00:00:11,640 ‫condition first, but by running code first. 4 00:00:11,640 --> 00:00:13,400 ‫It's also called the due loop. 5 00:00:13,410 --> 00:00:18,630 ‫So let's go ahead and create a do while loop and then see what it does. 6 00:00:18,630 --> 00:00:27,060 ‫So first of all, I need an initial user or a counter variable and I'm going to call that counter. 7 00:00:27,540 --> 00:00:36,930 ‫Then I have the do keyword and in curly brackets I have my body, my code body, and then here I'm going 8 00:00:36,930 --> 00:00:47,340 ‫to use console dot right line and I'm just going to write the counter out or print the counter onto 9 00:00:47,340 --> 00:00:51,840 ‫the machine and I'm going to increase the counter by one. 10 00:00:51,840 --> 00:00:55,260 ‫So increment here and next. 11 00:00:55,260 --> 00:01:03,600 ‫What I need is to have the while so the condition and in that case I'm going to see if counter is lower 12 00:01:03,630 --> 00:01:08,430 ‫than five, then please go ahead and run my due while code. 13 00:01:08,430 --> 00:01:17,490 ‫So let's add a console to read to it so that we can see the results on our console and let's run it 14 00:01:17,490 --> 00:01:20,610 ‫and then we are zero one, two, three, four. 15 00:01:20,610 --> 00:01:25,860 ‫So this counter so far not that much different to the four loop that we have seen. 16 00:01:25,860 --> 00:01:28,830 ‫So now let's have a look at what the difference is. 17 00:01:28,830 --> 00:01:33,840 ‫So let's say the counter was 15 and not zero. 18 00:01:33,840 --> 00:01:37,890 ‫So now that wild condition down here is not met anymore. 19 00:01:38,010 --> 00:01:41,970 ‫Now let's still run the code and we get the result of 15. 20 00:01:41,970 --> 00:01:46,710 ‫So what that does is it checks after it ran the code once. 21 00:01:46,710 --> 00:01:53,580 ‫So it's always going to run this code first and then it's going to check its counter, which in our 22 00:01:53,580 --> 00:01:57,600 ‫case is 15 lower than five and 15 is not lower than five. 23 00:01:57,600 --> 00:02:02,970 ‫So let's not run the code again in case it is lower than five. 24 00:02:03,000 --> 00:02:07,590 ‫Then let's run the code again and run the increment and so forth. 25 00:02:08,040 --> 00:02:14,340 ‫So now something that can happen when you create do while loops that you maybe accidentally forget. 26 00:02:14,340 --> 00:02:20,340 ‫So let's assume we have a counter that starts as zero and then we forget to increment. 27 00:02:20,880 --> 00:02:26,460 ‫So now let's run the code and I get this infinite loop. 28 00:02:26,460 --> 00:02:32,820 ‫So it's constantly creating zeros, billions of zeros, and that shuts down pretty much my program because 29 00:02:32,820 --> 00:02:36,360 ‫it won't do anything else rather than printing zeros. 30 00:02:36,360 --> 00:02:38,430 ‫So that's an infinite loop, a no go. 31 00:02:38,460 --> 00:02:47,820 ‫It's really important to always have your increment, or at least a approach that leads to this condition 32 00:02:47,820 --> 00:02:51,240 ‫at one point in the future to not be true anymore. 33 00:02:51,240 --> 00:02:56,400 ‫So it's really important that this condition is only met for a specific amount of time. 34 00:02:56,400 --> 00:03:01,620 ‫So it's not endless because if counter is always zero, this condition will always be true. 35 00:03:01,620 --> 00:03:05,610 ‫And this do while loop will always run and run and run. 36 00:03:05,910 --> 00:03:08,760 ‫So that's something that you really, really have to be wary of. 37 00:03:08,760 --> 00:03:16,350 ‫And other than that, the do while loop is super handy when you, as I said, have to run the code once. 38 00:03:17,200 --> 00:03:17,560 ‫All right. 39 00:03:17,560 --> 00:03:25,480 ‫Now let's go ahead and have a little program that will take the user input and we'll execute code based 40 00:03:25,480 --> 00:03:25,920 ‫on that. 41 00:03:25,930 --> 00:03:35,200 ‫So I'm going to create an integer that I'm going to call length of text, and I'm going to set that 42 00:03:35,200 --> 00:03:35,980 ‫to zero. 43 00:03:37,030 --> 00:03:42,480 ‫Now, the length of text should be the length of the text that the user has entered. 44 00:03:42,490 --> 00:03:45,300 ‫So let's create a little entry here. 45 00:03:45,310 --> 00:03:59,620 ‫Console the right line, please enter the name of a friend and then the user should enter a name of 46 00:03:59,620 --> 00:04:03,610 ‫a friend, and that will be read a line. 47 00:04:04,210 --> 00:04:09,610 ‫And then next what I want to do is I want to check what the user has entered. 48 00:04:09,610 --> 00:04:16,180 ‫So this is going to be a string and that will be the name of a friend. 49 00:04:17,380 --> 00:04:24,760 ‫And now what I do is I add the length of the entry of the user to the length of the whole text. 50 00:04:24,940 --> 00:04:35,680 ‫So I'm going to use something like int current length is equal to name of friend dot length. 51 00:04:37,420 --> 00:04:44,230 ‫So this will give me the length of the text that the user has entered and that is going to be added 52 00:04:44,230 --> 00:04:45,780 ‫to the current length. 53 00:04:45,790 --> 00:04:55,660 ‫And then I finally take the length of text and I'm going to add itself plus the current length. 54 00:04:57,610 --> 00:05:03,670 ‫And I won't need to counter anymore, by the way, because what I want to check is the length of the 55 00:05:03,670 --> 00:05:05,620 ‫text as a condition. 56 00:05:05,620 --> 00:05:17,290 ‫So if the length of the text is bigger than 20, then I want to finish with a little statement to the 57 00:05:17,290 --> 00:05:17,710 ‫user. 58 00:05:17,710 --> 00:05:20,890 ‫So I'm going to finish with Thanks. 59 00:05:20,890 --> 00:05:22,780 ‫That was enough. 60 00:05:24,250 --> 00:05:29,300 ‫And by the way, we even could use another variable string. 61 00:05:30,460 --> 00:05:43,330 ‫Whole text is equal to empty string and we're going to use whole text plus equal the name of a friend. 62 00:05:43,360 --> 00:05:47,680 ‫This will give us the whole text and we can read the whole text of the user. 63 00:05:47,680 --> 00:05:50,800 ‫So all the friends that are in that whole text. 64 00:05:51,550 --> 00:05:55,390 ‫So let's have a look here what this does. 65 00:05:55,390 --> 00:05:59,050 ‫So let's run the code and please enter the name of a friend. 66 00:05:59,050 --> 00:06:00,880 ‫And I'm going to start with Michael. 67 00:06:02,020 --> 00:06:03,760 ‫Then please enter the name of friend. 68 00:06:03,760 --> 00:06:12,940 ‫I'm going to say Sissy, enter the name Frank Maria and it says Thanks. 69 00:06:12,940 --> 00:06:13,660 ‫That was enough. 70 00:06:13,660 --> 00:06:15,490 ‫Michael, Sissy, Frank, Maria. 71 00:06:15,550 --> 00:06:19,480 ‫So that's also an approach how you could use do while loops. 72 00:06:19,480 --> 00:06:24,430 ‫You can do the same thing with a wild loop, by the way, but that's something that you can do by checking. 73 00:06:24,430 --> 00:06:29,680 ‫Okay, no matter what the user has entered, well, he needs to enter something first before we can 74 00:06:29,680 --> 00:06:31,150 ‫do the check anyways. 75 00:06:31,150 --> 00:06:34,360 ‫That's why it makes sense to run a do while loop here. 76 00:06:34,360 --> 00:06:35,410 ‫So we check. 77 00:06:35,410 --> 00:06:36,130 ‫Okay. 78 00:06:36,890 --> 00:06:39,000 ‫What is the entry that the user gives us? 79 00:06:39,020 --> 00:06:41,730 ‫Then we check the length of the current length. 80 00:06:41,750 --> 00:06:47,930 ‫We add that to our integer, which knows about the length of the text, and we say we only want to have 81 00:06:48,020 --> 00:06:51,560 ‫20 characters, or at least at the point where we check. 82 00:06:51,560 --> 00:06:54,200 ‫We want to have maximum 20 characters. 83 00:06:54,200 --> 00:06:59,870 ‫So because this could be a bit more than 20 characters because we do the check after the entry, right? 84 00:06:59,870 --> 00:07:06,560 ‫So anyways, now you see another approach where to use do y loops and you also see how you can not only 85 00:07:06,560 --> 00:07:13,730 ‫run the code through and through as a counter for example, but also use actual user input in order 86 00:07:13,730 --> 00:07:15,500 ‫to finalize the request. 87 00:07:15,500 --> 00:07:22,160 ‫And that's something that we are going to use, for example, to create a little tool which allows you 88 00:07:22,160 --> 00:07:26,900 ‫to calculate, for example, the average score of your students. 89 00:07:26,900 --> 00:07:32,690 ‫So let's say you are a teacher and you have 20 students, then you can go ahead and create a little 90 00:07:32,690 --> 00:07:39,050 ‫tool where you simply enter the score of each student, and at the end it will tell you the average 91 00:07:39,050 --> 00:07:39,650 ‫score. 92 00:07:39,950 --> 00:07:41,990 ‫So you could already develop that yourself. 93 00:07:41,990 --> 00:07:43,130 ‫You know how to do that. 94 00:07:43,130 --> 00:07:49,430 ‫You've just seen a slightly different example, but you could transfer that knowledge and build that. 95 00:07:49,670 --> 00:07:50,780 ‫But we will do that anyways. 96 00:07:50,780 --> 00:07:51,230 ‫Right? 97 00:07:51,680 --> 00:07:52,400 ‫Good. 98 00:07:52,400 --> 00:07:56,240 ‫So I hope you could follow along in the next video. 99 00:07:56,240 --> 00:07:58,340 ‫We are going to have a look at the while loop. 100 00:07:58,340 --> 00:07:59,690 ‫So see you there.