Learn How To Program In C# Part 7 – Do While Loop


Hey, everyone. In this video, we’ll be learning about do
while loop. In the previous tutorial, we learned about
the while loop. Let’s create a project. We’ll call it LoopingDoWhile. This is the Main method where the execution
of our program will begin. Let’s create our do while loop statement. do
{ } while();
You can see that there are do and while reserved keywords. Then you have your block. And then you have parentheses where you would
put your Boolean expression. Let me also write out the while loop. while()
{ }
What is the difference? You already know how it works is. while checks the condition or checks the expression,
which gets evaluated down to Boolean value. And if it’s true, then this block will run. If it is false, then this block will never
be run. And in the very beginning, the very first
time this loop begins to run and it checks for this and sees that it’s false, then
this will never be run. However, with do while, what happens is that
the very first time it enters the loop, even if the condition here on the bottom is evaluated
to false, it will still run it at least once. So with do while, we’re guaranteed that
the first try will always run, no matter if the expression here evaluates to true or false. If it comes down to here after the first try
and it evaluates the expression to be false, then it will keep going, it will exit the
do while, and it will keep going with the rest of the program. However, if it’s true, it will come back
up and act just like a while loop at that point. So the difference is that with do while, we
get at least the first execution and it’s guaranteed regardless of the expression being
true or false. With while loop, you can do anything that
you can do with do while. Then the question comes up why do we even
need do while. That’s a great question. Let me answer you through an example:
string input=Console.ReadLine(); while(!input.Equals(string.Empty))
{ Console.WriteLine(“Your input is {0}.”, input);
input=Console.ReadLine(); }
We’re just getting an input from the user. Not (!) is a Boolean operator. We say while the input does not equal to an
empty string (while our input is not empty), we will continue on with the loop. And then we print out input. Then I say take the input again. What’s going to happen is first time we’re
going to get the input. Check if the input is empty. If it is empty, this will never be run. However, if the input is something besides
it being empty, then we’ll print that out, and we will get another input. And then come back here and check. If it is empty, then we will go with the rest
of the program. Otherwise, keep continuing on. But can you notice something? We have the same line of code both in the
beginning and inside the loop. It’s the same thing. So essentially, we’re repeating ourselves. In programming (or software development in
general), we do not want to repeat ourselves. Why? Because repeating ourselves means that we
create the same piece of code in two different places or three or four different places. It doesn’t matter, but we’re repeating
ourselves. What that means is we have to maintain that
in two or three or four or five different spots, however many times you repeat yourself. And that is not very effective because if
string input=Console.ReadLine(); wasn’t a line and this was an entire, ginormous,
complex expression and it was multiple lines and we were repeating ourselves before the
loop and inside the loop, we would have to maintain those two pieces of code separately. So if one changed, we also had to go and change
the other one so that they’re synchronized. So repeating ourselves is error prone, and
it is just not a good practice. So you want to avoid that. Let’s run through the program to see the
result. Click Start. Asking for an input, I will say something. And as you can see, something is not empty,
obviously, because there is something there literally. So it enters the loop. And it will print it and ask for another input. So it says:
Your input is something. Now what if I don’t put anything? It will check. It will see that it is empty and will get
out of the loop. Good, that’s what we want. However, once again, we have repeated ourselves. Now let’s rewrite this in the form of do
while. string input2=string.Empty;
do {
input2=Console.ReadLine(); Console.WriteLine(“Your input is {0}.”, input2);
} while(!input2.Equals(string.Empty)); I’m just declaring my input2 variable at
the top. And then whatever I want to do with the variable
is in the block here above the do. Why? Because we’re guaranteed that it will run
at least once. And if it runs at least once, then we know
the line input2=Console.ReadLine(); will be hit. It’s going to ask me for an input, and I
will give it an input. Then it’s going to print it out. And after it prints it, it’s going to come
here in the while and say, “Listen, if the input2 is not empty, then go all the way up
here and continue on. So ask input from the user and then print
it out, and so on and so forth. But as you can notice, we have not repeated
ourselves. The line input2=Console.ReadLine(); is only
inside the block. Of course, we have to declare input2. But it’s a different statement, and we’re
not repeating ourselves. In a more complex situation, this could’ve
been multiple lines. In
string input=Console.ReadLine(); while(!input.Equals(string.Empty))
{ Console.WriteLine(“Your input is {0}.”, input);
input=Console.ReadLine(); }
we had to repeat ourselves. However, here
string input2=string.Empty; do
{ input2=Console.ReadLine();
Console.WriteLine(“Your input is {0}.”, input2); } while(!input2.Equals(string.Empty));
we’re just having it written once, and that keeps our code clean, easy to understand,
and maintainable, which are very, very important things when you are dealing with much bigger
systems. Let’s run the program, only the do while
loop part. It declares a variable input2 as empty. Then it entered the loop asking for an input. Now I’m going to enter ‘something’ as
input. It’s going to print it. Then it’s going to check the condition. input2 is something, which is not empty. So it’s going to come here on the top and
then ask for an input again. We only have our first input. And then we’re going to input ‘something
else’. It checks that, goes to the top, asks for
another input. This time we input nothing. So it will exit the loop and continue on with
the rest of the program. The output of the program is:
Something Your input is something. something else
Your input is something else. That’s it. I hope it was helpful. Please don’t forget to subscribe so that
I can keep updating you with the latest content.

5 Replies to “Learn How To Program In C# Part 7 – Do While Loop”

  1. Correction in the video:

    I meant to make the example for the while to be like this:

    string input = Console.ReadLine();
    Console.WriteLine(input);

    and then inside of the while loop to have the same thing.

    This way it would have been a true code duplication and then the DO WHILE would clean it up. Thank you for +KazoWAR for catching this a year ago haha I was just looking at the video and suddenly noticed it. We all make mistakes. Learn from them, grow from them. Cheers 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *