Hey what’s going on everybody, my name is Kenneth and we’re going to be talking about for loops today. So a for loop executes a block of code a limited amount of times. Compared to a while loop, which could continue infinitely depending on its condition, a for loop will execute a limited amount of times. So before we even begin, we already know how many times a for loop is going to iterate. So is this is how you create a for loop in Java. We first write the word for, followed by a set of parentheses which contains three components. We have our initialization component, that’s what initializes the for loop’s index or counter, which is what keeps track of the amount of loops. We have our condition component which determines how many times the for loop will execute, and finally we have our iteration component, which is what updates that counter that we initialized. We follow this by a set of curly braces, which contains the code to execute. So here we have a program that prints the numbers 0 through 4. Remember that a for loop has three components within its parentheses. We have our initialization component, which in our case we’re setting the counter or index to 0. We have our condition component, where we say we want the for loop to continue as long as i is less than five. And then we have our iteration component i plus plus. This just means that we want to add 1 to the current value of i, so after each loop, we’re going to update the value of i by 1. Finally, within our curly brackets, we have the code we want to execute during each loop, which is to print the current value of i to the console. Let’s first look at a flow chart to help visualize how our program will run. We start off by initializing i, our counter, to 0. Then we proceed to check if i is less than five. Since 0 is less than 5, right, our current value of i being 0, we proceed to print the current value of i. We then update our counter by 1, which updates i’s value from 0 to 1. We then continue to repeat the process until our condition fails to be met, which in turn will terminate the loop. So as you see, this loop will continue and it’s updating the counter and printing the values of i to the console. Finally, we update i to 5, we check if i is less than 5, and since i is not less than 5, i is equal to 5 in this case, it will execute false and terminate the loop. Alright, so i’ve gone ahead and written out our program. For reference, I am using the IntelliJ IDE not jGrasps’s, so if our screens look different, just know that’s why. So we have our program written out, let’s go ahead and run it to see what we get. Remember, in the flow chart, the console printed out the numbers 0 through 4, so we should be getting the same thing in our case, and as you can see we got the same exact thing, printing out numbers 0 through 4. Alright, so now let’s do the reverse. I want to countdown from 4 to 0. So first what we’re going to do is we’re going to set this i equal to 4, and then we’re going to change the condition to be i is greater than or equal to 0, and then we’re going to change this i plus plus to i minus minus, and then we don’t change the code inside our for loop since we still want the program to print the current value of i. And then finally I want to add a statement that lets us know our countdown is complete, so I’m just going to add that in real quick, and then if we run it, lets see what we get. Alright so there’s just three final things that I want to bring up to you guys. The first one regarding this i variable right here, our counter. We’re not limited to naming i, i, exclusively. For example, if we want to name i hello, and then if I update it in the rest of the code accordingly, if we run the program we’ll see that it still runs the same. However, most if not all programmers name this right here i out of convention, its just common practice. The second thing I want to bring up is regarding our initializer right here. So let’s just revert this back to i. We’re not limited to decrementing or incrementing the value of i by 1. Let’s say I want to decrease i by 2. We can set i is equal to i minus 2, or if we wanna write the shorthand version we can say i minus equal to 2, and then if we run this again, we’ll see that the program is now counting down by 2 instead of 1. The final thing I want to bring up is regarding the scope of i. So if you guys are unfamiliar with or forgot what scope is, scope is essentially the reach a variable has within our program. So in regards to i, we’re creating this variable within the for loop, so we don’t have access to i outside of the for loop. Let’s say I wanna call i inside this print statement, it’s already giving me an error, but I’m going to run it anyways just to show you what happens. You can see that java throws out the error cannot find symbol, and again, this is because we’re declaring i inside the for loop, not outside of it, so we won’t have access to i outside of the for loop, only within, which is why we’re able to call it right here. So let’s try this instead. Let’s move this outside of the for loop, and now let’s try running it. You can see that we got the same result as before, except now we have access to i in this print statement, and this value negative 2 this was the last value of i before the for loop terminated, so that’s why this is printing out negative 2. So that’s it for for loops, and I hope you guys found this helpful.