 Hey everybody, this is Brian and welcome to the 7th C-Sharp video. We're just going to go to New Project and then you're going to do Console Application. Today we're going to be covering the For Loop. The For Loop is different than the Do Loop and the While Loop in the sense that you can do something x number of times. Let's kind of illustrate here. We'll say int i equal 0. And you know that you can do a do and then while and then some condition. You know, do while i is, you know, less than 10. Well, that gets kind of cumbersome. So why would you want to do that? Well, that's exactly why the For Loop exists. Let's just delete all this. We're just going to go for and we need our starting number and we'll say int i equal 0, semicolon. And now we want our condition. At what point should this check and we'll say, well, i is less than 10. And then we need to know what to do with i. So we're just going to say i++ meaning we're going to increment i. And this may look like a really, really funny syntax. And you're looking at this going, what is this? This looks like ancient Egyptian algebra, but it's actually very simple. There's three segments. There's the variable with the initial counter. There is the condition. What do you want to do? You know, as long as i is less than 10. And then what do you want to do to that variable? Well, we're just going to increment it. So we're going to say console, right line. And we're just going to spit out i. Whoops. There we go. I'm just going to do a simple console read that way the console will stay open. It's a five to run. And turn off. There it is. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Now where's 10? Remember how we did it to 10? Well, it starts at zero. Get used to a zero index counting. That's kind of universal across most languages. It starts at zero because zero is the first number for a computer, not one. So while you start counting at one, the computer always starts at zero. As well, zero is a number. So it says while i is less than 10. What happens is i keeps going. It gets to nine. And then it says i++ right here because this is the last thing that happens. You could have very easily written this as i++ right here, meaning you're incrementing i. But it does it for you right here. So when that hits 10, it goes back and reevaluates this and says, ah, i is not less than 10. Now if you wanted the 10, you could say less than or equal to 10, and it'll count from zero to 10 or 11 numbers. Let's test that just by f5. Sure enough, zero through 10. There's our 11 numbers. Yes, zero is a number. That's something you're going to have to get used to in the programming world. So that is the for loop. Now you can also use, let's just do a little copy and paste action here, the for loop as an infinite counter. That is a valid for statement. Now, don't really have an i in there, so we'll just say forever. This is the structure of the for statement right here. And we've got no variables, no counter, no nothing. So if we run this, f5, you see it just keeps spinning out forever, forever, forever, forever. That's because it's just going to keep going indefinitely. And then we would just simply add in int, and we'll say x equals zero. And actually, let's do the reverse. Let's say x equals 10. We want to say x greater than zero, or actually we'll say greater than or equal to zero. And then we'll say x minus minus. So what we're going to do is we're going to decrement the counter. And then we're just going to print out x right here. Save your work, run it. And now you notice we're doing the exact opposite here. We've got zero through 10 and 10 through zero. So we're counting up and we're counting down. Very simple statements here. All right. I knew that this may be new to some of you. Some of you are probably rolling your eyes going, oh my gosh, I've done the for statement. If you know more than one language, you know that almost every statement is in every language. But we're going to review really quickly here. This is your for statement. This is your code block. Here's your evaluation. This is what's, remember it's in the parentheses. It's going to be evaluated first. This is your variable. This is your condition. This is your action. In other words, you're saying i equals zero. So we're starting at zero. We're going to say while i is less than or equal to 10, increment i. And then each time it goes through here, it's going to run this code. So that in a nutshell is the for loop. Well, this is Brian. Thank you for watching. I hope you found this video educational and entertaining.