 Java loop challenge number one. So first let me go ahead and put a loop challenge one here. So let's see correctly. So in this particular tutorial, and again, sorry, I'm not at my usual space. You might hear some background noise. It's a beautiful spring day. I'm looking at some Sakura. And it's quite lovely here. So you might hear some background noise and maybe a little bit echo in the room. I apologize. I'm not in my usual recording environments. So the loop challenge one. So there are six basic loop challenges here in Java. And I've also done these in Python. I think there's a video somewhere on the channel if you check that out. But what I want to do here is talk about how to iterate through a string and access each individual letter of that string and just basically, you know, perform some actions on each individual component. This is a really fundamental and important skill for computer science. You'll be using this not with just with strings, but use it with arrays, array lists, 2d arrays, 2d array lists, etc, etc. Okay, let's get started. Notice I have a template file here. I'll put a link down below if you don't have access to that. And I've created two strings, one called text and one called letter. And I'll be using those throughout this particular challenge. So the first challenge is create a loop to print the string vertically. So what I want to do is I want to go through this entire string, one letter at a time. That's a key fact there is one letter at a time. Questions I have to ask myself are where am I starting? And in this case, I'm starting at the beginning. I'm going all the way to the end. I'm going one by one. And that's the key information we need to create our loop. So if you'll recall, hopefully from previous lessons or times you've studied, the index of the letter A, the first letter is actually zero. So we're starting at zero. And you may or may not be aware there are 26 letters in the English alphabet. And but since we started zero, Z or Z, depending where you're from, is actually index 25. So here's what I'm going to do. So for int I, and I is representing our index. So I is starting at zero. And while I is less than 26. So once it gets 26 done, it's less than not less than an equal. And I'm incrementing by one, I'm going up by one. And I'm just gonna go ahead and put my braces in there. I find a lot of students, especially new people, forget to put the closing brace and they get some weird errors, they don't know how to deal with. So you might want to try that. So this will get me through zero to 25, one at a time. Now, what I need to do is I need to pull each letter out of that. Now, if you're familiar with Java, you know there is a care type or char, I'm not sure how they pronounce it. But since I'm focusing on AP, which doesn't use that, I'm going to be using a string. And that's why I created the variable letter here. So let's say a letter equals, and it's going to be text dot substring, I and I plus one. So in the case of a, it's zero one, one is not inclusive, it doesn't include the B. So it only asks for one character. So in this case, it's gonna be a, so zero, one, one, two, two, three, three, four, all the way up to 2526. Notice there is no 26. So if I did right now, so if I did like system dot out dot print l n, this is what we want to do is print each letter says vertically. So it means one letter per line. Because of that, I'm using print l n or print line here. So I'm gonna go ahead and compile that. Hopefully we'll see compilation finish successfully. Notice this says loop challenge one. This is also loop challenge one. And they need to match. So whenever I run that, and I'll scroll up and you can see here, A, B, C, D, F, G, all the way down to Z. So that is how to solve that first challenge. Now, the only thing is I don't like about this is that I put 26 here because I know there are 26 letters in the English alphabet. But what if I decided to add Bob at the end of this? Now there are 26, 27, 28, 29 letters. So if I compile this, you know, Bob is ignored because I still have a 26 here. So what I'm going to put here instead is text dot length, parentheses, that will give me the length of this, which is now 27, which is now 29. Okay, so but because we started zero, the index of the second B is 28. We go ahead and compile it and run it. And we'll see if that worked. And you can see here, we've got the OB at the end, take that off to get back to where it was. Okay, so that's the first one. So again, just real quick, make sure don't this, this drives me nuts, students for some weird reason, they can't figure out the first ones, they go, okay, I'll try the second one. No, it's not a good way to do things. These are specifically designed to build up the skills that you need to solve this in a certain order. So now look at this one says create a loop to print the string horizontally. The only difference between these two problems is printing vertically and printing horizontally. We're still starting at a still going all the way to Z, we're still going one by one. I've already got the code to do that. I've already got the code that pulls out each individual letter. So the only change I need to make here is instead of print ln print line is print. So we go ahead and run it. We see now it prints a through Z horizontally because there's no ln here. ln automatically adds a line return. So moving on to number three, create a loop to print the string backwards. I probably should have added horizontally here, but we're going to be printing this string backwards in one line. So what I'm thinking about is first it's most similar to this one because I do want to print out horizontally. So I think all the rest of them are printing horizontally. I probably should have clarified that. But notice we're printing the string backwards. So what we need to do here is kind of play around with this for loop. So we're not starting at zero anymore. We're starting at 25 because that's the index of the last letter. Now, so instead of going up, we're going down. So 25, 24, 23, 22, 21, 20, blah, blah, blah, down to zero. So I want to keep doing this while i is greater than or equal to zero. So once it gets to negative one, I want to stop. And that's the only changes we need to make for this. Notice this and this is still i and i plus one. That's 25, 26, 24, 25, 23, 24, et cetera, et cetera. So the order here doesn't change. The only place I change is here. Let's go ahead and compile that. And I got an error. Did I do wrong there? accidentally? Oops. Let's go ahead and compile that. Right. And you see now we have z through a backwards. Now again, what if I added a bob here? I run that I still get z through a because now the length is different. So we'll leave Bob there. Sorry. And what, so what I do here is something similar. If I do text dot length, and this is what a lot of beginners do. They do text dot length, and then they run it and then they get an error because the length is now 29. But the index of this b is 28. So I just need to put minus one there. So when it was 26 letters, z was 25. So 26 minus one. So I can use a little bit of math there to you know, simplify it so that no matter what string I put, it's going to print it backwards. Let's get rid of Bob there. So of these problems, this is the only one where we're printing backwards. But what I hope you can realize from this one is the starting point has changed. The way we increment is changed. Instead of incrementing up by one, we're incrementing down by one. And the condition has also changed. Okay, up here, it was less than down here, it was greater than. So while it's greater than or equal to zero, keep doing up here, as long as we're going up, if it's less than the text length, keep going. And this condition is really, really important. Now, number four, create a loop to print every other letter horizontally. Now, I've already got code to print horizontally. And this is why I said to do these in order. So what I'm going to do is I'm going to come down here, I'm going to pop that in there. I'm going to compile it and run it, make sure it's working. Okay, so you can see, I've got this. Now you have to read the question. It says, create a loop to print every other letter. So what that means zero, two, four, six, eight, et cetera, et cetera. So the only change I need to make here is plus equals two. So instead of going zero, one, two, three, four, I'm going zero, two, four, six, et cetera. Let's compile that and run it. And you'll see here, it says I had a great time on excursion. I made this exercise after going on excursion with the students or my students, I should say. Yeah. Okay. Number four, that was number four, number five, this one's a little bit more complicated. It says, create a loop that prints only the capital letters. Again, we're printing horizontally. But notice we now have a condition. So when we have a condition, we're going to need an if statement. So I only want to print capital letters. So the question now is how do I determine if a letter is capital? And here's how about a simple way of doing it at least. So if letter dot equals letter dot two upper case, oops, capital C, parentheses, extra parentheses, parentheses, need a lot of parentheses there. Okay, so don't mess that up. See, there's a lot of them at the end. And that's it. Okay. So if, so for example, if capital, does capital H equal capital H to uppercase? Yes. Because uppercase transforms this to uppercase. Does lowercase r equal uppercase uppercase r? No, of course it doesn't. So it doesn't do anything. So if I compile that, print it, it says Hiroshima is a beautiful city. So I actually went on excursion to Hiroshima with my students. Hiroshima is a very beautiful city, by the way. If you visit, I definitely recommend it. So real quick, we're still iterating. We're still going through every single letter one at a time, starting at zero, going all the way to the end. But we're only printing letters that are uppercase. Now you might see here, you see how it prints an exclamation mark. There is no lowercase exclamation mark. There is no uppercase exclamation mark per se. They're the same character. So that's why they always equal each other. Spaces, the same thing. There's no uppercase space, lowercase space. They're the same thing. That's why spaces and punctuation are printed out. Only lowercase letters are filtered out because they don't equal their uppercase counter part. And the last one, very, very similar to this one. I'm just going to go ahead and copy this because we're still looking at capital letters and we're looking at not capital letters. We're still starting at the beginning. We're still going all the way to the end. We're still going by one. So this part doesn't change. This is a very standard type of thing that you see in computer science problems. I'm still pulling out the letter. I'm not printing capital letters. I'm saying count capital letters and count lowercase letters. So you're just reading the instructions. Count the number of capital letters and the number of lowercase letters. I've already created two variables, one called capitals and one called lowercase, to keep track of that information. So any time you need to keep track of the information, you're going to need a variable to keep track to store that information. Notice I put this outside of the loop because when it starts at zero, but I'm going to be changing it in here. So if it's a capital, I'm going to increment capitals by one. Kind of makes sense. Now it's either capital or lowercase in this example. So I'm going to use an else statement. And I'll put lowercase. And then a lot of, again, at beginners at this point, a lot of them will, okay, I got it. I'll compile it. And then they'll say, oh, it doesn't work. Okay, well, no, it's working perfectly. You just didn't print. Now notice we don't want to print, you know, every single time we only want to print once all of the letters have been counted. So I'm going to print this outside of the loop. So system.out.println capitals, plus a little space there, lowercase, colon plus lower. And I'm going to compile it, run it. And we get 19 capitals and 30 lowercase letters. The end. So the key thing you need to watch for these types of problems is which way are we iterating? Are we starting at the beginning and going to the end? Are we starting at the end and going to the beginning? Are we iterating one by one? So plus one, plus, you know, a to b, b to c, or zero to one, one to two, are we going down 25 to 24, 24 to 23? So that part gives you this here. In some cases, we were iterating by twos, okay, prints every other letter. In this case, we iterated by two. And then in some cases, we're only printing capital letters. So we have a condition that we need to satisfy. So we need to check for. And then type of question here is a question where we count something based on some condition. In this case, if it's uppercase, we count it as a capital. Otherwise, we count it as lowercase. And then finally, we print the result out after the loop. Again, this is a very, these are very basic, yet very common, you know, techniques that you'll be using, particularly in AP for taking AP. Okay, thanks for watching and keep on coding.