Hello, in this video, we are going to take a look at the wire loop. So we've already covered the for loop. And this is the full loop. So you have three parts to it, the initial part where you declare any variable set and if you're not even used for your loop, the condition for while the actual word and a piece of code that can run at the end of each part of the loop. This is usually something to make sure you know you don't get into an infinite loop that you are actually changing. The while loop actually is pretty similar.
So for right today, it's only three right this out in wild loop form. So it's wild. I so the only thing that goes in the bracket is the condition and you might actually mention this to James there just so you get a different letter. You might notice it's a it's not declared and that's right. Don't actually declare a set of any values, you know, inside the loop that's generally done beforehand. So if I do like int J equals zero, and if in here are the system, dot out dot print line and put j, that's fine.
But this piece of code does run at the end, if we run it now, this will just basically be a infinite loop where nothing is actually changing because j isn't, you know, be modified. So this piece of code does not. At the end of each path, you actually literally put it right here. So you put j plus plus, run it. Okay, so that's where I'm gonna literally go and comment that I made poxy exactly what's happening a quick death. Look why his services weird, this should not be happening.
So we didn't j plus plus, which is this value right here, or j is less than 100. And we are printing and this allows us to do five. Fine. Okay. What was wrong before then, must have just been that the code didn't compile for some reason. So what I'm gonna do now, if that seemed like an anomaly in the pro system, dot out dot logging and in here I'm simply going to say, and here I was done.
So we have the wall loop, then we have the for loop, so the actual result is exactly the same. So that is a difference in syntax. The reason why you might use a while loop over a for loop is this. If, for example, this variable, you know, like this one here, that you're using as part of the condition and a part of the incrementing as well. You may have this set up and created beforehand, and you may need to use it in the loop. And if that's the case, instead of you know, creating a new variable and assign in that variable to the VA You have the other variable that you want to use.
And then maybe keep keeping track of that for maybe afterwards you want you want j to be 100. And you want to keep using j as if he was 100. For that reason, you will use a while loop. while loops are really cool in that scenario mentioned the loop in the following video where you might be getting input from the user, and you might continue asking for important toe they provide a valid input. So that's it for the while loop very similar to the for loop. If you have any questions about its uses, feel free to reach out in the do while loop video.
We'll extend this and we'll look at define on loop type. Thanks for watching, and I look forward to seeing you in the next awesome Java video.