Hello, in this video we are going to look at the while loop. So as the name suggests, a loop is something that basically keeps looping it just keeps running until a certain condition is no longer met. So the overall general format is like this, I put dollar i, this is just a variable, a equals zero, and the format equals wild bracket and then you put a condition here and this condition can be the exact same sort of condition, you would put in a if statement, for example. So when you checking for value is equal to something, if you're checking if a value is less than something, you know, not equal to something along those lines. Don't say if dollar i is less than 10. So what this means is, while this is true, keep running the loop and some curly braces and you'll just keep running whatever codes in here.
So we are going to say echo And I'm actually going to put a break as well at the end. So Rhonda, as you can see, it is basically just it just keeps on running. The reason is this condition condition is always true. So you don't want an infinite loop, which is the technical term for it. So we want to modify this value. So at the end we'll say dollar i plus plus this just add one to it.
So this is the equivalent of dollar i equals itself plus one. So if I do that, cause so trapped in the loop, now it's printed out 123456789 and 10. So what it's done essentially equal zero. is zero less than 10. Yes, fine. It says loop.
Adds one to it is one less than 10 Yes, keeps doing that until he hits 10 is 10 less than 10? No. So it runs the 0123456789. So we actually run the 10 times because he runs it from zero. Let's do something else. That's a new dollar, it is basically saying know what loop it is.
So it says 0123456789. Okay, so this is a great way of executing code multiple times, instead of writing that code, again, and again, in a web code you want in the air, you can have function calls, can have as many lines of code as you want. Let me show you where this could be of some use, you know, a bit more than that. So if I have an array, for example, so if I say dollar of equal array, and this could be on a multi dimensional array, well, the principle is the same and I'm going to say BMW on going to say, find out, let's say, yo, let me do one mole or think of a call to mind has gone blank. Okay, so these are four variables, um, you know, four calls in an array, usually we would want to print them out, or you know, access something or do something about the variable, I mean the array name, zero, which is the first one, so 0123.
And if I run that, we get BMW. And if I want to, you know, do all of them you we will have to do this. And as you can see, this is very cumbersome, especially if we have like 456 maybe 1020, even 100 in our right. An alternative to this is using something like the wall Oops, I'm gonna comment this out. And what we want here is the length of the array, which is four. So that's how many times we want it to run.
And we want to add one to it, that's fine. But instead of a queen data, what we wanted to his echo dollar call. And in here we're going to have dollar eye. So what that does is it gets the call, right? He gets the value that is at this particular index, and remember, you'll be 0123. And if we save that, as you can see, I printed out all of them.
And what happens if I add a nother car? Let's say if I add a Ferrari, Ferrari, nothing happens is because this is hard coded. What we can do is a Can we count how many values are in the array? Now, no matter how many we add, if we was to DeLorean of industries called DeLorean it just keeps printing them out. So that is a real world example of why you might use a loop. There are actually other loops that are better suited to this, but I'll be covering them later on in this series.
Exactly the next few videos. If you have any questions, feel free to shoot me a message. And as usual, I look forward to seeing you in the next video.