‫In this video.
‫We are going to have a look at the do while loop and it differs to the other loops by not checking the
‫condition first, but by running code first.
‫It's also called the due loop.
‫So let's go ahead and create a do while loop and then see what it does.
‫So first of all, I need an initial user or a counter variable and I'm going to call that counter.
‫Then I have the do keyword and in curly brackets I have my body, my code body, and then here I'm going
‫to use console dot right line and I'm just going to write the counter out or print the counter onto
‫the machine and I'm going to increase the counter by one.
‫So increment here and next.
‫What I need is to have the while so the condition and in that case I'm going to see if counter is lower
‫than five, then please go ahead and run my due while code.
‫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
‫and then we are zero one, two, three, four.
‫So this counter so far not that much different to the four loop that we have seen.
‫So now let's have a look at what the difference is.
‫So let's say the counter was 15 and not zero.
‫So now that wild condition down here is not met anymore.
‫Now let's still run the code and we get the result of 15.
‫So what that does is it checks after it ran the code once.
‫So it's always going to run this code first and then it's going to check its counter, which in our
‫case is 15 lower than five and 15 is not lower than five.
‫So let's not run the code again in case it is lower than five.
‫Then let's run the code again and run the increment and so forth.
‫So now something that can happen when you create do while loops that you maybe accidentally forget.
‫So let's assume we have a counter that starts as zero and then we forget to increment.
‫So now let's run the code and I get this infinite loop.
‫So it's constantly creating zeros, billions of zeros, and that shuts down pretty much my program because
‫it won't do anything else rather than printing zeros.
‫So that's an infinite loop, a no go.
‫It's really important to always have your increment, or at least a approach that leads to this condition
‫at one point in the future to not be true anymore.
‫So it's really important that this condition is only met for a specific amount of time.
‫So it's not endless because if counter is always zero, this condition will always be true.
‫And this do while loop will always run and run and run.
‫So that's something that you really, really have to be wary of.
‫And other than that, the do while loop is super handy when you, as I said, have to run the code once.
‫All right.
‫Now let's go ahead and have a little program that will take the user input and we'll execute code based
‫on that.
‫So I'm going to create an integer that I'm going to call length of text, and I'm going to set that
‫to zero.
‫Now, the length of text should be the length of the text that the user has entered.
‫So let's create a little entry here.
‫Console the right line, please enter the name of a friend and then the user should enter a name of
‫a friend, and that will be read a line.
‫And then next what I want to do is I want to check what the user has entered.
‫So this is going to be a string and that will be the name of a friend.
‫And now what I do is I add the length of the entry of the user to the length of the whole text.
‫So I'm going to use something like int current length is equal to name of friend dot length.
‫So this will give me the length of the text that the user has entered and that is going to be added
‫to the current length.
‫And then I finally take the length of text and I'm going to add itself plus the current length.
‫And I won't need to counter anymore, by the way, because what I want to check is the length of the
‫text as a condition.
‫So if the length of the text is bigger than 20, then I want to finish with a little statement to the
‫user.
‫So I'm going to finish with Thanks.
‫That was enough.
‫And by the way, we even could use another variable string.
‫Whole text is equal to empty string and we're going to use whole text plus equal the name of a friend.
‫This will give us the whole text and we can read the whole text of the user.
‫So all the friends that are in that whole text.
‫So let's have a look here what this does.
‫So let's run the code and please enter the name of a friend.
‫And I'm going to start with Michael.
‫Then please enter the name of friend.
‫I'm going to say Sissy, enter the name Frank Maria and it says Thanks.
‫That was enough.
‫Michael, Sissy, Frank, Maria.
‫So that's also an approach how you could use do while loops.
‫You can do the same thing with a wild loop, by the way, but that's something that you can do by checking.
‫Okay, no matter what the user has entered, well, he needs to enter something first before we can
‫do the check anyways.
‫That's why it makes sense to run a do while loop here.
‫So we check.
‫Okay.
‫What is the entry that the user gives us?
‫Then we check the length of the current length.
‫We add that to our integer, which knows about the length of the text, and we say we only want to have
‫20 characters, or at least at the point where we check.
‫We want to have maximum 20 characters.
‫So because this could be a bit more than 20 characters because we do the check after the entry, right?
‫So anyways, now you see another approach where to use do y loops and you also see how you can not only
‫run the code through and through as a counter for example, but also use actual user input in order
‫to finalize the request.
‫And that's something that we are going to use, for example, to create a little tool which allows you
‫to calculate, for example, the average score of your students.
‫So let's say you are a teacher and you have 20 students, then you can go ahead and create a little
‫tool where you simply enter the score of each student, and at the end it will tell you the average
‫score.
‫So you could already develop that yourself.
‫You know how to do that.
‫You've just seen a slightly different example, but you could transfer that knowledge and build that.
‫But we will do that anyways.
‫Right?
‫Good.
‫So I hope you could follow along in the next video.
‫We are going to have a look at the while loop.
‫So see you there.