 Hey everybody it's Brian and welcome to the 6th C-Sharp tutorial. Go ahead and make a console application. Today we're going to do the do loop. Syntax is very simple. Do with two brackets and then while and then an expression. So we need to make a variable. We'll say int, call this counter. Say counter equals zero. And we're going to say counter plus plus. That increments the counter. And we're going to say console dot write line. And we're just going to print out the value of counter. Now we need the expression of counter less than 10. So let's break this down really quickly and see what's going on here. We have a variable we're calling this counter. It's set to zero. So this is like saying the number zero. And then we're incrementing the counter. That's what this plus plus is. It's the increment operator. And then we're writing counter out to the output stream. We're saying while counters less than 10. It appeared and disappeared because we forgot to do our console read. F5 to run. So you can see while it's less than 10 it prints it out. Now what would happen if you forgot this right here? Say while counter. Notice how the their build errors cannot implicitly convert type int to bool. Well, if you click on that it takes you exactly where the problem is. And you can actually highlight it with your mouse. And you'll say cannot explicitly convert type int to bool. Now what is that saying? Well if you remember an integer is a number and a bool is a true fault, the light switch on off. So what we're saying is you cannot convert a number to an on off. So that's why you have to say counter less than or you could say counter greater than something or less than or equal to 10. Now when you run it, you notice because we said less than or equal to, it went up to 11 that time. So that's how you determine when this counter is going to stop. Now to really understand what's going on here, we should switch these two around. Do loops are kind of confusing to newcomers because they don't really understand what's going on under the hood. You notice how all we did was flip this around. We're writing to the console then incrementing the counter. This gives you a better idea of what's actually happening here. It starts at zero and then it goes through nine. And you notice how it stops at nine because we're saying while it's less than 10. So once it hit 10, it just stopped. It said nine and then incremented it to 10. And when it hit this again, it just said nope, not running it. So that's how the do loop works. Now if you wanted to make an infinite loop, you could just say while true. And let's run that. And you can see it just keeps going and going. Eventually we will fill up the number space for an integer and this program will eventually crash. We don't want that. So you should always have a limit to your do loop. Unless you want an infinite loop, which is usually reserved for like video games or network monitoring, things of that nature. But those get into a synchronous operation, something we're not really even close to yet. So we're just going to say while counter is less than 10. And we're going to cover what's called the while loop. And I'm going to show you guys a little trick here. If you right click, you see this surround with, you get a handy little list here and you can just pick and choose from whatever you want. So if you forget something and you can't see it, but at the bottom of the screen is while, you just double click it. It puts the syntax out there for you. So it says while true. So this is an infinite loop right here. We don't want that. So what we're going to do is say counter and we're just going to set this back to zero. Notice how we are not saying int counter because we would declare the variable. That's bad. We're re-declaring the variable and it actually see how it gets bad underlines and says local variable name counter already exists in the current scope. What that means is you've re-declared it. You've already got something out there called counter. Remember a variable is something that'll change. We're just going to change the number here. Switch it back to zero and we're going to say while counter is less than 10. Then we're pretty much just going to do the exact same thing. F5 to run. And let's see what happens here. We have zero through nine and then zero through nine again. Now why would you want these? I mean why would you ever want this? Well let's switch this to 10 and see what happens here. You notice how one of them ran. So it says 10. That's the difference between the do and the while. Do will always run first and then evaluate the expression later. Well while we'll evaluate the expression first and then run the code. That's the difference between the do and the while loop. So very simple, very powerful. Why would you use a loop? Example, let's say you want to read a text file. You would start at the beginning of the file and then do while you're not at the end of the file read a line. And you're going to see that in future tutorials. So the do loop and the while loop are very simple. You have very powerful functions in your programs. This is Brian. Thank you for watching. I hope you found this tutorial educational and entertaining.