Hello, in this Java video, we are going to take a look at the for loop. So this is the first loop that we're going to take a look at. So why is the lip I think you might be able to guess it when of loops, and Li which will run code again and again for multiple times without having to duplicate the code. So this can be extremely useful if, for example, you are maybe going over some sort of data type like an array, and you want to print everything out, or you go in over a database, you want to print everything out, or if you want to get input from the user, and maybe they provide wrong input. So you want to continue trying to get input to provide the correct input. For that sort of scenario.
The while loop will be the better one, but the same principle still applies. So the syntax of the for loop is like this. You put keyword for open close brackets, and in here there are three main parts. So the first part is where you set up and declare any variables that you want to use exclusively for the for loop. So for int i is a very common variable name and equals zero. The middle part is the condition for while the actual loop will run.
So if this condition is met, then the loop will continue running. When the condition is no longer met, then the loop will stop running. So we're going to say while i is less than 100. And now the last part is a bit of code that is run at the end of the loop after every single pass. So if for example, we did i plus plus what this would ignore the red line that's just because I'm per curly brace, there we go. So this would declare a variable called I set it to zero Let's say what I left it up on good run the code within these curly braces, which you will run because zero is less than 100.
But if we put i plus plus, what that will do is increment i by one every single time you call it enough also do something like this or increments it by 10. What are the normal oil? So you're gonna do a simple system, dot out dot print line. And we are going to put, we're going to say, Okay, I'm just gonna literally print out the value of our printer. I think they printed out zero to 100 because notice the condition is while i is less than 100, not while i is less than or equal to. So if you want to include 100 in this, you do like so so it ends at a footprint.
If for example you wanted it to say between one on 100 You said I initially to one and then you set the conditions. to less than or equal to 100. k doesn't have to be less than or equal to you could do something like y is greater than 100 and maybe start at 1000. And maybe you take away instead. Then you go. So obviously, a lot more value than printed out. We started at 1000.
And he went down. Remember, it's wild is greater than 100. So if you wanted to include 100, in the printed, you would do it like, so. I'm going to switch it back. So what you can also do is use the shorthand, and maybe we add 10 every single time. So it prints out 111 21.
But you want another one in the chain that 200 1020 3040 and by the time it gets to 90, after it's done that path, you're incremented by 10. So it's 100. He checks the condition. So he says is it less than Hundred or 100 less than 100 No, it isn't they are not less than they are equal to, therefore it doesn't run the print code. You could even use the original thought of long conversion like this. Wow.
So that's it honestly for for loops, pretty much any sort of code that you want like if statements, variable creation and other stuff that we'll cover later on in this series. You can put within here you can nest loops within other loops, just the way you can nest if statements in if statements, some really, really cool stuff. So thanks for watching. Now look forward to seeing union in the next awesome Java video.