A Rookie's Guide to Java Part 3 - Loops | Harry Wainwright | Skillshare

A Rookie's Guide to Java Part 3 - Loops

Harry Wainwright, BSc Software Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
4 Lessons (20m)
    • 1. Class Introduction: Loops

      0:44
    • 2. While Loops

      4:24
    • 3. For Loops

      4:57
    • 4. Arrays

      9:31

About This Class

The third class in A Rookie's Guide to Java, we learn how to repeat a section of code a specified number of times, until we get the desired outcome. 

A Rookie's Guide to Java, previously released on Udemy currently holds a 5-star rating and boasts hundreds of happy students. Now I'm bringing it to Skillshare! A Rookie's Guide to Java assumes no prior programming knowledge. We start at the very basics and work our way up to some fairly advanced topics, including Java Swing. This course comes with written material to recap what you've learned at predetermined points. This truly is one of the best ways to learn.

I often see courses claiming that you can "Learn To Make Video Games (No Programming Needed!)" But unfortunately if you want to make anything more advanced than flappy bird you'll need a programming language under your belt. Learning programming is fundamental to any career in the software industry, games or otherwise, and Java is a great starting point.

Java is the most widely used programming language currently so you cannot go wrong by getting to grips with it. It holds a heavy focus on the "Object Orientated" part of "Object Orientated Programming" and as such it is a subject that features heavily in this course. 

Transcripts

1. Class Introduction: Loops: I can't so moving on section three, week of a loops more specifically while and four loops. And so I'm assuming you've done conditional statements by now because the code follows very similar format, you know? I mean, if you've done the section, if you've skips around two loops, then you may know the idea of loops is that we can reuse a section of code over and over again instead of, say, typing out a line 100 times. If you wanted to count 100 I typed five or six on get the computer to count for you. That's really why loops prove useful because we want Teoh Lupus section of code until we're satisfied that the condition has been met. 2. While Loops: So today we're going to be looking at while loops. Um, a loop is a way to reuse a section of code over and over again. Uh, this is going to save you, typing out a line so you won't to print hello, world 10 times on a terrible example. Really boring. But say you want a printed 10 times. Ah, you probably think in the after this type, you know, system print. Hello, world Overnight when I over and over and over again. But using a while, we can loop through it 10 times and just print out the same line over and over again. Uh, so that's what we gonna do today. First, I just wanted to coming out all this code here. We're gonna use it late Rome, but I don't want it's keep running. So yeah, we just surrounded it with thes false lashes on the asterisks kids, it's gonna lie shared a gray. That means it's not gonna be a problem. It's not gonna be picked up by the compiler, so it won't be run anymore. So we just ignore all that for now. So let's just stop by wild loop. You don't actually have to type loop while I like there. Count is less than 10 going to print out. Hello, World statement. So Count hasn't been initialized. 20 things I was gonna create. Count, count, count. What kind of less than 10 It's gonna print out. How? Well, so we've covered this in the If statement. This is just gonna run while this is true. Eso interstate. Well, anyone If you know, computer age is less than a job here or this wild representing in a running count is less than 10. Obviously, we've just initialized count to zero, so it's gonna room weapons. If we do run this, I wonder. Stop that real quick before it crushes the program. So you see, we've got a hell of hello worlds here. Way mold in 10. So why is this? We're not moving. Count up. We're not counting called variable count for a reason, but we're not actually counting, So the let's go through it line by line. By that, I mean one line we go into it count is less than 10. Okay, cool. That's true. Print. Hello, world. Go back to it. Cows. Lesson 10 True print. Hello, World Cater's lesson 10 true print. How will just out of interest what is counter to equal to, Oh, it's equal to zero is equal to zero because that's what we initialized it to. We're not actually changing that. So after this print line here, we have to actually count. And as the account is equal to count plus one now, we're actually telling the computer to count. Essentially now, for a cool we're Hello World. 10 times 3456789 10. Yep, I'm taking credit. I could do math. Ah, it's got a new lines, the end of the and start. Let's make it look a little pretty sweet. So that's a while leap. It'll run whatever's inside of it until these conditions become false. Super, super simple in theory. But it does allow us to do samo complicated things as we go, which will cover in future lectures. Do you have a play around with them? Test the boundaries. Avoid infinite loops. So remember to count when you have to count, uh, otherwise you'll be in a mess, Shall we say, 3. For Loops: So today I'm going to quickly introduce four loops. Loops are similar toe while loops. But while loops are generally used when you don't really have an end goal in mind, it's camera. Just a catchall. If you wanna leave 100 times, it could look to 1000 times etcetera, full. Oops. I like to think of them are more controlled. Um, baseline means you can do more within the the the statement that controls them. And I got this simple definition from the Java documentation. The first element will be the initialization for by a second, but you also include an increment inside the the of the conditions. In a while. Loop the increments down here where we have to manually count equals Campbell's one in a full loop. You couldn't do that within within the brackets, so that's right. One out real quick. First it was going to come in the South, same as I did with the last section of code. So four we have to set up our interview inside of the first section. Now I've said to name variables sensibly in the past. You don't want to variable name. That doesn't mean anything but while you're using loops or four loops more specifically, you can use them for a while. It's a swell You can use I, J and K two for account essentially. So where are account? They could have been. I j OK. Ah, it's It's arbitrary. It's completely arbitrary. There's no reason why it's done that way. But you can use I, j and K in loops, and it's completely fine. Programmers know what I j and K mean that No, it's account for a loop. Eso If you're not going to use count, make sure you use I j OK, Why? Those three letters are tonight. No idea what's just the way they are. So we set up our initialization. This is where it's going to start counting from going to se Teoh zero. I guess that determination eso this was gonna count up to. So it's gonna be while eyes less than 10. I don't have an increment. It says was gonna count open. We have I plus Plus, that's gonna add I to each time. You know, we could write I kind of nine also some of death like that. I'm not just that annoyed. I don't actually if you follow a variable with a plus Plus, it's just gonna add one to it. Ah, let's just print this out and serve printing out Hello, world. We're just gonna print out I Is that going to print out the number that we're on? So when we run it, we get 012345 609 It's on the first run through the loop where I'm in r equals zero. It's true. That is less than tens we're gonna add I want to It and I would print out I go again. So now wrong I equals one. It is less than 10. We print out one add 12 It's now Auntie, and he goes up to eyes less than 10. Obviously, we did less than if we wanted to have it count. 10. We could do less than or equal to use that sign. Ah, if you know, if you don't want to use that sign that will, that will always the counts 10. And you can always just count to 11 instead. Stan's personal preference, I'd say to use the equal sign. You don't wanna be adding 12 things in your head that you want the computer to do it. Let's count from one. It's just for the heck of it. So I want to Through flustered 78 on time. Save soup. Simple. Ah, just gets a bit. It takes a bit too. Robbie, head around. This a full loop line here. Just remember initialization termination increment. Get practicing. It'll become a lot easier as you go along. 4. Arrays: I get on. So I'm gonna get a quick click of Ah, um, a raise, which is what we're doing in this lecture. Raise a film off storage. Ah, within Java, which will allow you to essentially store information I'm gonna keep, like to short, because it's not really something you use a lot much in Java. Um, just coming this out. But we didn't allow comment, and I should really clean this open delete most of this, but I'm gonna keep it for that. So let's declare our ray we declare an array as so was caused my array for now. So we start off by declaring the type. If this was string, our array would store strings extort doubles, you know, bulls. Except for except we just want a story. It's for the purpose of this demo volleyed by the square brackets and then the name of the era have scored mind Mirer. He can call it whatever you want. Then we need to allocate memory for the array. Eso were essentially setting the size of the array. You have to do this for annoy. There are other storage types that you don't have to specifically set aside is full, but for a raise, you have to We set size as such. So they have to set the size of the rate 10 on were just filled it with new ones. So now we've got to do is set. Well, we don't have to this perfect functional, but I'm going to set this size of each element in the array. Founder Ray works is what we have 10 spaces. But instead of starting from one, we start from zero. That's pretty important to send element inside the right. It's a simple as typing the name of theory for by the square brackets and then the position in the rain I on the very first position. So I have type zero. I was gonna set this to 10. Now, if I want to print out what's in the rain, I do so as such. So yeah, real simple. Get head around that Well, uh, so let's from this real quick. So this is gonna do is print this one line here with settlement in position 0 10 Okay, so let's see what happens if we try to print out position one notice I haven't initialized while initialized it, but haven't set position Oneto anything. Um, listen, no Shell was gonna happen, I imagine. Yeah, it will come out with zero because we initialized it with a new intern here. Eso It's filled all 10 positions with zero on this line. And then on this line, we replace the first position with tech so we could do This is may times have we won. Ah, obviously fireplace positions there again. It's just gonna change the first elements us change position one. It would just make it 20. It's now, if we run, this is gonna print out 20 in position one. There we go. So let's incorporate our full loop point out. Eso four int I equals one. Well, first we have to make this down below our array code. As we know it works from top to bottom. So there we go. So that's just gonna count to 10 at the moment. Let's change that. So let's make it my array. Sorry. Equals Ah, I yes. Keep it locked up for now, and there's gonna print out. Was in each position. Looks a bit from Kittleman, but this should be okay. They got a little bit of an Arab probably just because I went over the boundaries off. Yeah, I went over the boundaries of the array, which can't do so this ever hear exception and thread main. Let me just run it again. I've got rid of those two lines. Code, Yes. Exception and thread Main. Ah, blah, blah, blah out Bounds 10. So that's because I started. I won instead of zero. You see, we specifically set the array size to 10 with a raise. This is really important. You cannot go out of bounds of the array. It's size 10. That means we've got positions 1 to 9 toe work with on. If we start trying to add something in position 10 as we did there, we're gonna get problems. Serious problems. So now with the room that again, with I being zero on the max being nine, See, we don't get any errors. So all we've done here is filled The array. We filled the array with numbers. 120 Pretty pretty simple. Let's replace this with 100. Andi, we'll do it. I equals by 10. So now we should be counting up in tens instead. No, Uh ah, yeah, because yes, because we're working with. I again says 200. 100 and one. Yeah, a bit fiddly. But see, now create a day Honore with 101 positions. So we filled the 10th element with 10 so I don't up intense opposition, Zahra. Zero such intent is 10 position twenties 20 all the way up to 100. Last time I got an error because I tried to fill space 19 9 Well, I tried spill fill space 100 obviously accounts from 0 to 99. Eso I'd recommend having a play around with a raise. The most important thing you remember is that you cannot go out of bounds at all. Um, in the slightest. If you do so, then you get errors. If you do get a narrow with an array is almost certainly because you've gone out of bounds . Um, so do have play around with them. And I reverted back to I plus plus just going up in ones. So I think it was cleaner. Uh, should be okay. Got infinite loop there. Stop. Just I plus plus no, I equals I plus plus Say it should be okay, se. See, we're just going out bounds gangs were trying to go up to 10. I really want to go to knowing, so raise our simple there just a way of storing information and it doesn't have. I don't have to be counting up in ones here. As I did a leak, you can put whatever you want in there. I just made it simple. Let's make it 50. Let's make him all 50 so you can have what you can store whatever interviewer we want in these arrays, as long as it isn't interview because we defined it as an interviewer. Array wants to make this a string array. Then I could do so. I'm not going takes US. Videos were a little little long, but they don't play around with them. A job with a no hugely instrumental, as they are in C plus plus or whatever, the the useful to know anyway.