For Loop

Learn the Basic Java Concepts Loops, arrays and methods
12 minutes
Share the link to this page
Copied
  Completed

Transcript

Hello there, and welcome back to this Java development course. So, last time we went over the while loop, you had some homework to do. Your job was to create a program, which takes four integers as input, then it multiplies those integers together until it finds a number that is divisible by 10. So here's essentially what you should have done. So first of all, I create my scanner. Then I do declare for Long's, or in this case, when I said integers I'm in of course long.

So since as I said in the previous lesson, and it's you're probably won't be able to hold a number large enough to, you know, get to the double by 10. So anyway, so great for Long's so next long that I create a long number, which is the A times B times C times d. Then I create an integer I which is equal to one and then I just do while true, we print that one To print the number we're checking, and then we do if number percent 10. So if the remainder of number divided by 10 is equal to zero, then we print, we must multiply i times and the number is number, which just means so if the number divided by 10, the remainder of the number divided by 10 is equal to zero, then that means that it's divisible by 10. And then we just break, which just means that we're done.

So if you remember, the break keyword means that we just exit the loop. Okay, then we just do i plus plus, which you know, as one, two eyes, since we must have the correct multiply the amount of times you multiply, and then we just put number equals number times A times B times C times d. There he goes, there's a program so we can now test this out. We can do one times two, times three times four, and there we go. So it checks 24 536 so on and so on. gets this number which is divisible by 10. So we must multiply it 14 times.

So there we go. Now we can try also to two times two times two times two. And in that case, we actually get zero. So the number is zero. So what does this mean? Well, this is because our long also has a limit.

There's a limit as to which number or hour long can hold until finally it gets to zero. And then it just zeros. Technically, the remainder of zero divided by 10 is zero. So, yeah. Okay, so that is essentially how we do that. So now let's get into today's lesson, which is going to be the for loop.

So the for loop works a little bit like the while loop, except, instead of putting just a Boolean in there, or just a condition, we can actually put a lot of different things. So not a lot of different things. We actually, for loops were actually created originally to work with lists of things. So we're gonna take a look at Look at how that works and how we can use them with lists. So yeah. Anyway, without further ado, let's get into today's lesson.

Alright, so we're going to create a new class. A new class, this is going to be called for loop. There we go main as always. Let's finish up. And there we go. Alright.

So what are we going to be doing in this class? Well, we're going to be creating a for loop. So let's do the same exact thing where we're going to print hello world to the screen 100 100 times. So do that in a for loop. Let's let's do it in a while loop first. So while suppose we need to create an integer i equals zero, right?

Then we're going to do while i is less than 100. We're going to do System dot out dot print ln. Hello, world. There we go. Okay, and so now if we go ahead And run this application. There we go.

As you can see it printed 100 times. Actually no, here Actually, I completely forgot to add something. So this is going to print it forever. I forgot to add i plus plus right here, just like just like that. There we go. So now, it should print 100 times there we go, as you can see.

Alright, so this seems a little bit redundant, doesn't it, so we have int i equals zero, we have i plus plus, wouldn't it be makes sense just put it all into one place. So I mean, we're probably going to be doing this a lot. So we're going to probably going to be doing, you know, looping a certain amount quite a bit. So for that we actually have a loop. Different kind of loop we have a for loop. For now we have int i equals zero.

So just like we did up here we had int i equals zero. Now we just have a right here. Then we have separated by a semi colon. We have the condition so is less than 100 and then a semi colon, and then we just write them you know we do so i plus plus to remember how there we had i plus plus well here we also have it. Then we open up curly brackets just like in the wild. And we can do something we can print System dot out dot print ln.

Hello, world. There we go. Alright, so now let's go and run this. And there we go. So as you can see, we get Hello World 100 times it does the exact same thing as with the while loop, except here, we have a lot less code. Okay, so there's really nothing complicated going on here.

Essentially, the for loop just went through like this. So we have four, then inside parentheses, first we put some assignment, so we put an assignment. So this can be int i equals zero. It can be in a equals zero, it can be long, a equals zero, or it doesn't have to be an assignment at all. It could just be any code, we could actually do this dot out, dot print ln and then pi. There we go.

And then we have to have to double quote to semi colons and then just opening parentheses. So how does it work executes this once, and then it will execute whatever is here and it will take condition from here, a Boolean from here, and then it will execute whatever is here. So if you actually run this you'll see that it prints hi and then it will execute this for loop forever. Because we actually don't put any stopping condition here. Now, we can actually put System dot out dot print ln here as well, which can be any code any code at all. So we can do Hello for example.

And if you run this, so now as you can see, it only prints hi Once, but it prints Hello unlimited time so every single time we go back to the beginning of the code, we can actually do System dot out dot print ln a division here and you'll see if we actually run this and and stop it immediately. Perhaps it I stopped it No, no, but as you can see, every single time we go back here, it will execute whatever's here that says how that works. So here How does this work? So first of all here, we just print assignments This is executed once at the very beginning of the loop. Then this will be executed every single time that we re loop and so that is the general structure of a for loop. Now, it really isn't very good to put actual code here.

Well not actual code but you know system out println statements usually before the first semi colon We put some, you know assignment assignment to something so just a variable that we're going to use. So for example, int i equals zero, then in the middle we put a condition. So just some Boolean true or false or either than 100 and so on and so on. And then after and after the second semi colon, we just put an operation that we perform every single time for example, i plus plus is usually regarding the variable that we create. And that is usually how a for loop works. Now, the same exact, you know, things apply to for loops as they do with while loop so we can do break and that will just break the loop.

See, it prints hello world and it just breaks. And same thing with continue. So if I it's not equal to four continue And remember and remember that when we continue it just it just stops executing and goes back to the beginning of the loop. So if we run this, as you can see it will only print hello world when i is equal to four, otherwise it'll just continue and so in result, this doesn't get executed. Okay, so that is essentially how we work with for loops. Now there's gonna be one last thing that we're going to go over this is called the do while loop.

So it's kind of like a while loop but it doesn't actually have enough functionality to warrant its own separate video. So we're going to go over it together in this video. So the do while loop works kinda like the while loop. So we can do except start with the word do so do and then while and then something so we can actually put while false. And do here System dot out dot print ln Hello. Alright.

Now, in theory, this shouldn't actually run since we have a false in here. So this is short run, but what the do while loop is it will perform the operation. So hello. And then it'll check whether it should do the operation again. So if you run this, as you can see, even though we have false in here, and it shouldn't loop, it still prints Hello once, since that's actually what the do loop works for if know what the do loop does. Okay, so that's it for loops.

Loops are very important. And in the next lesson, we're actually gonna be going over arrays or lists on Java, they're called arrays, but they are essentially just lists. And in lists, that's where loops really come into, you know, come into their own sort of, they start to get useful since in order to search a list, for example, we would create a loop that would just look at every single item in the list, and then compare it to see if it's What we're searching for, and then if it is, it will just return that item. Same with inserting into a list, maybe we have a couple of some values that we need to insert, we're going to insert them with a loop, maybe we need to sort the list, and so on and so on. There's a lot of different things that we can do with loops that you know, have something to do with lists.

But even without lists, loops are still pretty useful and we can still use them. Okay. So now let's try some homework. Alright, so for homework first of all, you're gonna get a value from scanner. So scanner, SC equals a new scanner. system.in and let's import it as always.

Whoops, I clicked the wrong thing. There we go import scanner. Now, we're gonna take a variable from the console, so int a equals SC dot next int not line. There we go. And your job is to find the factorial of a. So if you don't know what a factorial is, for example, if we have a four factorial, then that's equal to four times three times two times one, that's a four factorial.

If we have five, then it would be five times four times three times two times one, and so on. So that's how it's two factorial works. And so your job was to use loops to find the factorial of a value that we get from the console. So it isn't too difficult, if you can understand loops, and you can do this and it's actually pretty good exercise to do if you want to understand loops. Personally, I remember when I did it when I was in college, so yeah, anyway, without further ado, I wish you luck and see you next time.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.