‫Okay, so we've learned about both conditionals and loops, and you'll be using both of those together
‫throughout your entire career.
‫So in this video I'll give you some simple examples.
‫And in the next video we'll we'll do the same within our little job listings UI.
‫All right.
‫So let's create a PHP tag.
‫And then I'm going to create a variable.
‫This is our initialization for a while loop.
‫So I'm going to set number equal to one.
‫And then let's create a while loop.
‫And the condition for that while loop is if number is less than or equal to ten.
‫Okay.
‫So we're going to basically run this ten times.
‫And in here what I want to do is have an if statement that checks if the number is even.
‫Okay.
‫Now we can do that using the modulus operator.
‫So if we take the number and then use modulo which is the percent sign, let's say two equals.
‫So if that equals zero what we're saying here is if that number divided by two has a zero remainder,
‫then we know that it's even.
‫So let's echo out here.
‫We'll say echo number and then I'll concatenate on is even.
‫And then we'll just put a line break.
‫Okay.
‫And then let's say else.
‫Then we can echo.
‫The number is odd.
‫Okay.
‫And don't forget this part.
‫You need to do your increment or it'll be an infinite loop.
‫So number plus plus.
‫Nope.
‫Forgot my money sign.
‫All right, so let's run that and we get one is odd.
‫Two is even, three is odd, four is even.
‫So it looks good.
‫All right.
‫So that's how we can use an if statement within a loop.
‫Now I want to talk about the break and continue statements.
‫So sometimes you might want to stop a loop before it's finished.
‫So if it if it reaches a certain condition or whatever it might be.
‫So let's for this example.
‫Actually I'm just going to comment this stuff out.
‫But for this example I'm going to use a for loop.
‫So let's put a comment here.
‫We'll say we're going to break break out of a loop.
‫And again, this could be any kind of loop.
‫But we're just going to use four.
‫So let's say four and we'll say I equals zero.
‫As long as I is let's say less than or equal to ten.
‫And then we have our update our increment.
‫So I'm going to check in here if the I value let's say if that is equal to five then I actually want
‫to break out of the loop.
‫So we can just say break.
‫And then just to show where we're at, let's go under the if statement and let's echo out the I value
‫and then just concatenate line break.
‫Okay, so if I reload that, we get 01234.
‫Actually, I wanted to start it at one, but really it doesn't matter.
‫The point is that once it once this condition was true, it broke out of the loop.
‫Okay.
‫It didn't echo five out because it broke before we were able to echo five out.
‫So I mean, that's just an example of breaking out of a loop.
‫Now, there might be times where you want to skip an iteration of a loop and have it keep going.
‫That's where we can use the continue keyword.
‫So let's say we'll say skip.
‫And.
‫Continue.
‫So again I'm going to use for loop.
‫And in fact I'll just copy this.
‫And what we're going to do here is instead of breaking, I'm just going to simply change that to continue
‫and let's see what that does.
‫We'll comment this one out.
‫So let's reload.
‫And now you'll see it starts at one.
‫We see one, 2346.
‫So it skipped five.
‫So basically we just said if we're if if I is equal to five then just continue.
‫So the last thing I want to do is show you how we can use a conditional within a for each loop where
‫we're looping through an array.
‫So let's create an array here we'll say student grades.
‫And I'm going to set that to I'll use this syntax just to switch things up and let's have names.
‫So we'll say John.
‫And then we'll have their grades.
‫So 75, we'll say Jack.
‫Say 92.
‫Jill.
‫100 and I don't know, Joan.
‫Let's say 80.
‫Okay.
‫So I want to use a for each loop here.
‫So I'll say for each.
‫And then student grades as and I want to get the name and the grade.
‫And we can do that by saying name and then using an arrow and then grade.
‫Okay.
‫So that will get us this as the name and then the grade for each one.
‫And I want to just echo out if they're above a 90, we'll say the name has an excellent grade.
‫So let's say if the grade is let's say greater than or equal to a 90, then let's echo out, we'll say
‫the name and then we'll just add a string, let's say has an excellent grade with a line break.
‫And then if we reload, we should see Jack and Jill both have an excellent grade because they're 90
‫or above.
‫All right.
‫Really simple stuff.
‫In the next video, we're going to take that job listings template that we created where we loop through
‫an array of listings and output a card.
‫So I just want to add a couple things in that and use conditionals within the HTML.