 Java loop challenge two. Just a quick reminder, if you're coming from, if you're not coming from loop challenge one, I'm not my normal recording environment, so you might hear some background noise. I'm actually sitting here looking out at some very lovely Sakura. So let's take a look at Java loop challenge two. Yeah, so we're basically using some for loops and we're gonna be doing some stuff with looping. So let's take a look here. So number one says create a loop to print the numbers one to 10 inclusive. Now, I should have added here, what I wanna do is I wanna print the numbers horizontally and I wanna put a tab between the numbers. So let's just work through this problem step-by-step. So it says create a loop to print the numbers one to 10 inclusive. So it means I'm starting at one. I'm ending at 10. Inclusive means that I want to also print 10. I'm including 10 in that list. And although it doesn't really say it explicitly, I wanna print all the numbers. So one, two, three, four, five, six, seven, eight, nine, and 10. So four, I'm just gonna use I because that's what I've been using in my other programs. This could be X, might make a little more sense in this case. I wanna use I. Again, I'm starting at one. And it says inclusive of 10. So that means while I is less than or equal to 10 or I could do less than 11. Doesn't matter which one I do. Whichever makes more sense to you. Now if you're here because you're an AP student or planning to be an AP student, this is one of those things that you'll see a lot on AP tests and they like to confuse you with less than and less than or equal to. Thanks, that's very educational, I guess. Anyway, so we are incrementing by one. I'm gonna go ahead and put my closing brace in here just to get that in there. And again, it doesn't show it here. It doesn't say it, it should say it. I wanna print horizontally. So I'm gonna do system.out.print and I'm gonna print I. Now this isn't quite correct, but let's go ahead and test it out. So I'm gonna compile it. I'm using the Genie editor here. And I'm gonna go ahead and execute it. And let's see how the result is. So we get one, two, three, four, five, six, seven, eight, nine, and 10. So I know that part's working. The loop is correct. I started at one, ended at 10, incremented by one. I'm happy. But what I wanna do is I want to also add a tab as I mentioned earlier. But if you haven't done that before, let's just start with adding a space. Okay, so I'm gonna compile it, add the space and now I've got a space between each of my numbers. I could stop there. I don't have to add a tab, but adding a tab I think makes it look a little more graphically lined up. So to add a tab, I think that's a forward slash or is it a backslash, maybe a backslash, and a T. So that tells the computer to print a tab. So I'm gonna go ahead and compile it. I'm gonna go ahead and run it. And you can see now there's a tab between each of these. Now depending on your computer setup, this tab might be a little bit smaller. It might be a little bit bigger. I think it's a computer setting. I don't really know much about it, but it lets us line up our numbers in nice neat columns. Now, number two is a different type of problem. But similar, it says create a loop to print the sum, key word there, sum of the numbers one to 10 inclusive. Now notice it says one to 10 inclusive. This is the same loop issue or loop problem as we had in the first example. So I already know how to do that. I'm just gonna go ahead and copy this. So I don't really like typing again. I'm gonna take that out because it's a different problem. So see what it says, create a loop to print the sum of the numbers one to 10 inclusive. So because I'm printing a sum, I need to make a variable to hold that information. So I'm gonna say int sum equals zero. So when I start, the sum's gonna be zero. So it says the sum of the numbers one to 10 inclusive. So I'm gonna do it inside the loop. I'm gonna say sum plus equals i. And I'm gonna do system, system.out.println and I'm gonna print i, oops, plus, I'm gonna do t again and do a tab plus sum. So let's compile that and run it and here we go. So we got one, because zero plus one is one. One plus two is three, three plus three is six, six plus four is 10, 10 plus five is 15. I did this on the board the other day and my students were like, no, that's not right, you can't count. And I was like, oh my gosh, you're right, I can't count. Anyway, so 10 plus five is 15 and so on and so forth. And the sum up to 10 is 55, okay? So again, we started at one, we ended at 10 and 10 was inclusive. So notice here, I'm using another variable to hold the sum. So I need two pieces of information. I need my iterator i and I need my variable sum to hold that information and to keep changing along with the iterator i. Now, the next one is a slightly different problem. So it says create a loop to print the even numbers only from one to 20 inclusive. Okay, so I've got my starting point, I've got my ending point, I've got my increment. So let's go ahead and do that. So for int i equals one, because it says to start at one, not zero. And while i is less than or equal to 20, i plus plus. So no matter what else we do, this is the structure of our loop. Starting at one, we're going to 20 and 20 is inclusive. That's why I have the equal sign here. If it was less than 20, it would stop at 19. Less than or equal 20 stops at 20. Less than, oops, cancel, sorry, I don't wanna enable Siri. Less than 21 will stop at 20. Again, this is a very common little gotcha. Even though they'll do this on the AP quiz questions. And because they think it makes you a better programmer apparently. So create a print only even numbers. Now here's a question. How do I determine if a number is even? Now, some people would say, okay, well if I just change this to a two and I do i plus equals two, this will give me the correct answer. But I don't want you to do that for this one. I wanna start at one. I wanna go to 20 because what we're doing is we're learning to use conditions to determine what we do inside the loop. So it says print only even numbers. So I have to determine if, terming, I always do that, if the number is even. To do that, if I percent two equals equals zero. So percent is called the modulus. And what it does is gives you the remainder. So for example, one divided by two is zero with one leftover. Two divided by two is one with zero leftover. So it's even. Three, or sorry, three divided by two is one with one leftover. Four divided by two is two with zero leftover. So that's how that works. So if it's even, print ln, well, let's print it across. Print, I should have specified that. i plus t plus, so we're only printing the even numbers. So I'm gonna go ahead and compile that. Because I didn't follow my own rule about always adding that extra brace before filling it in, okay? And again, some people will point out that in Java, if you only have one line of code, you don't need the braces. I strongly recommend putting those braces in. It'll help you keep your code organized. And later, if you have to add some code in here and do something extra, it's a lot easier. I just think it's gonna help you to keep your code organized. That would be my advice. So compile it, run it, and you can see here, we got two, four, six, eight, 10, 12, 14, 16, 18, and 20 inclusive. Moving right along. Number four, create a loop to print the sum of the even numbers from only from, should be only from one to 20 inclusive. So this is clearly a combination of this problem and this problem. Now just a quick Java thing. I've already declared sum as an integer here. So I don't need to declare it again. But I do need to reset the value to zero. Now I've already got this code. So I'm just gonna go ahead and copy that. It's the same basic problem. So print the sum of the even numbers only from one to 20 inclusive. So I'm gonna do the same thing I did. If it's even, the only difference is that I'm counting from two, four, six, eight, 10, et cetera. And then if it's even, I add it to the sum, not dumb. Sum plus equals I. And then I'm gonna go ahead and print that plus the sum. So I reset the sum to zero. And I didn't put in there because I already did that way up here. And then it says only the even numbers. So I first have to find out, is this number even? If it is, add it to the sum. And then I'm gonna print that out. Again, notice how I didn't have to add these because I already had them from the previous question. So I'm gonna go ahead and run that. And you can see something has gone horribly wrong. What the sum plus equals I. It should be two plus four plus six. And what did I do wrong there? I can put it correctly. I have a sum equals two. Sum plus equals I, system out. Oh, duh, I'm gonna do print LN. That's pretty horizontally. Okay, so let's go ahead and do that. And now I've got two, zero plus two is two. Two plus four is six. Six plus six is 12. 12 plus 18 is 20, et cetera, et cetera. Down to 90 plus 20 is 110. Which is interesting because that's also, that's double 55. So that's kind of an interesting mathematical thing. I'm sure there's some mathematical explanation for that. Okay, now, so that's that. So this again, this was a combination of the previous two exercises. And this is why I say that these exercises do them in order because they build on each other. Okay, number five, create a loop that prints the two times table from zero to 12. So this is zero times zero equals, or zero times two equals zero. Zero times one equals, sorry, one times two equals two. Two times two equals four. Three times two equals six, et cetera, et cetera. So what we're iterating through here, we're starting at zero and we're ending at 12. So I'm gonna do four int i equals zero. i is less than or equal to 12. And I'm going up by one. Let me go ahead and close that down. And what it says is I want to print the two times table. So I'm gonna do system.out.println and I want to print i times, I use an x here, I use an asterisk, it doesn't matter. Times two equals, plus, this is where it gets a little confusing, i times two. Now what you might want to do here, you want to say int product, product is what's the result of a multiplication. So this makes it a little more clearer. So product equals i times two. And then here I would just put product. Maybe that makes this one a little bit clearer. So we're printing the two times table from zero to 12. So zero to 12, up by one. So zero times two is zero. One times two is two. Two times two is four. And we're just printing it out row by row. So we go ahead and run that, or compile it, run it. And there is our two times two. Zero times two equals zero. One times two equals two. And if you're having trouble figuring these types of things out, you want to look at the pattern. So I got a zero, one, two, three, four, five, six, seven, eight, nine, all the way down to 12. And then the mathematical relationship here is this times two is zero. This times two is two, et cetera, et cetera. Now some of my clever students are like, well, couldn't I do zero, two, four, six, eight, 10? You could do that. And then divide to get this number. But usually we would be going the other way. Especially since we're printing the two times table. Now, this last one's a little bit more difficult. And what we're doing here is a little bit different. What I'm gonna be doing is I'm gonna be printing one, two, three, four, five. And then the next row, we'll have one, let's see, two, sorry, there should be two, sorry. Two times two is four, three times two is six. Four times two is eight. And then five times 10. And then this will be three. And then we'll have three times two is six. Three times three is nine. Four times three is 12. Five times three is 15. And 12 by 12. So this is an interesting challenge. It's a bit like this, but it's also a little bit different. This may be the first time you've done something called a nested loop, which is where we have a loop inside of a loop. So I'm still doing 12 and 12. So I'm gonna do four int i equals one. i is less than or equal to 12. i plus plus. And I'm gonna go ahead and put my other brace in so I don't forget it. Now I need a second loop. Again, this loop is also going from one to 12. I'm gonna use j. This is just kind of an old kind of programming habit. I think from Pascal or something. It's something that's very common. i and j were often used as iterators. You'll see that as well in the AP stuff. So what I wanna do is I'm gonna do system.out.print. Not print, print, not print line, but print. And I'm gonna do i times j. Then at the end of each row, I'm gonna do system.out.println. I think I have to put quotes in there. I think this will do it. Let's try it. I'm not sure. Okay, yeah, it did do it. Okay, but it doesn't look very good. So we need to add, okay, so let's go ahead and compile that. And there we've got our times tables, okay? Because this is i. i goes up to 12. This is i times j. So i is one. So one times one is one times two is two. One times three is three. One times four is four. Okay, so i times j. So one times one is one. One times two is two. One times three is three. We go all the way through to 12. We come down, i is now two. So two times one is two. Two times two is four. Two times three is six, et cetera, et cetera. And it goes all the way down to 12 times 12. Pretty nifty. Wonder what happens if I add zero? I'm just kind of curious if it's gonna do what we want it to or not. Let's compile, I'm kind of curious about this one. Okay, it does basically the same thing. Because you got your zero row and you got your zero column. But I don't really think that helps our case at all. So we go ahead and put that back and compile it. Make sure it's working. Okay, again, this is a tough one, especially if you've never done this type of thing before, but I think up to number five, it should be fairly straightforward. This one you gotta do a little bit more thinking. And if you're one of my students, we'll do more problems like this later. And yeah, we'll go over that bit more step by step. Okay, that's it. Thanks for watching and keep on coding.