Hello there, and welcome back to this Java development course. So, last time we went over the switch statement, and you had some homework to do. So your job was to create a little bit of a quiz. So in this case, our quiz was, what is the correct way of defining a variable in Java. And we had a couple of options, we have int a equals to a equals to one, a equals two, and int two equals a. So in this case, we have an integer A equals st dot next int, which is just the number that the person selected.
Then we have a switch statement, we input a if it's one, it's right. If it's two, wrong, three, wrong four, wrong. And default. If it's none of those, it'll just print invalid selection. So let's test this out. We can now do what is the correct way of defining a variable in Java.
Now, at this point, you should know that it's one. So too, we didn't assign a data type to it. Three, we can't our name can start with though with a letter with a number as well as we didn't assign a data type and four we didn't. We can't our name can't be a letter. So it can only it has to start with a letter. Yeah, our name can't be a number, our name has to be has to start with a letter.
There we go. got confused there for a second. Anyway. So now we have to input which one we think is correct. So one is correct. So we put one and there we go.
You got it right. Now, if we put to it, we got it wrong. And if we put 34 invalid selection. So that is essentially how we use a switch statement. So if you've got this, that's very good. If not, don't worry about it.
Go ahead and take a look at this code, download it, try to run it on your own machine and understand why you weren't able to figure out figure it out on your own. After that, you will be very well versed to switch and you can go ahead and move on to the rest of the course. So anyway, I wish you luck. I don't know I don't wish you well, why am I wishing you luck? We're about to get into today's lesson. So anyway, let's get into this lesson.
Okay. So today we're going to be going over something a little bit complex. So let me go and actually create was going to be calling the, let's just call it while loop. Okay, so what is a while loop? Well, a loop is as you can probably tell from the name something that loops. So in programming, what if we wanted to print every number from zero to 102?
There's the console. So how can we do it? Well, there's the obvious method System dot out dot print ln zero and then so on and so on. So we can do zero and then 12345 And so on and so on. So you can imagine how long this will take and how many lines of code this will take. Instead of that we have something called the while loop.
So wire loop we just do while parentheses and open square brackets. So the while loop will take a Boolean as input, so stay true. And this Boolean will determine whether or not the while loop loop. So while and here's where the name comes from, while the Boolean inside the parentheses is true. It'll loop. If that Boolean is false, it'll exit the loop.
So in this case, what do you think will happen? Well, we can actually test it out. Let's go and actually do System dot out dot print ln hello world, just so that we can see the effects. Hello, world. There we go. And so now let's go and run this and there we go.
As you can see, since our Boolean is true, it keeps on running, so it doesn't stop running ever. And perhaps you can actually see my computer in the background start to the fan starting to, you know, spin up since it's doing a lot of computing power at this point. It is, you know, it is it is, how can I say this? It's throwing everything it has at this wild. So it's trying to print hello world to the console, as fast as it can. It's doing everything in its power, you know, it's, it's, it's working, it's hustling.
So yeah, if you do this, this is actually called infinite loop. I think I don't think that's an official term or anything. I think I just heard it somewhere. Perhaps it actually isn't called that. But anyway, so as you can see here, it actually just infinitely loops. And I think if you let it run long enough, it'll actually just our operating system will, will force close our program.
But in this case, you know, it's it's it's doing the best it can. And we can actually just stop it right here by by by doing this set this button. Here we go to stop it. There we go. And so now it just stops executing. But if we didn't stop it, it would keep on keep on doing it.
So yeah, so that's how we can use the while loop. Okay, so what else can we do with a while loop? So obviously, this is great now. So if you put true, it'll keep on executing. If you put false, it just won't execute at all. In fact, it'll give us this unreachable code exception, which just means that it will never it will never execute.
So there's no point in putting it. So yeah, so what's the point? You know? I mean, so can we only either print forever or just put a false and it's pointless? Well, no, we could actually do int a equals zero. Since now print ln A A plus plus.
And then while a is less than 100. There we go. So now if we run it There we go, we get all the numbers from zero to 100. And we solved our original programming problem. Okay, so let's go over exactly what's happening here. Because it's very easy to get confused by the while loop, I notice a lot of students really try to overthink it, there's nothing difficult going on here.
Take a look. So first, we instantiate a variable of type int with the name a, and we assign to the value zero, then we declare all our while loop. So we say that while a is less than 100, we're going to print a system out print ln A and then we're going to do a plus plus which is going to add one to a flat try to become the compiler. In this case, we'll try to do it so we create an integer a with zero, we then have while a is less than 100. In this case, a zero so zero is less than 100. So we're going to execute this we're going to print a and then we're going to add one to a a is now equal to one, we go back here is one less than 100.
It is so we print 100 Want to go back here is a is a to less than 100 it is is three less than 100 is four is five, and then we go all the way up until finally we print 99 out one two A is now 100 is 100 less than 100 it's not this return is false and we exit the loop. So that is loops. So loops are actually pretty simple. So if you really think about it, they are pretty simple. Okay, so what can we do with a loop? Well, quite a bit actually.
For example, we can create new scanner scanner st equals new scanner. System whoops, nope system.in There we go. Let's import scanner. Alright, so in a will now be equal to sc dot next int. And now we'll all we actually have to do is do while true. In a equals SC dot next int and then do System dot out dot print ln A plus 12.
Okay, so there we go. So now if we run our application, as you can see, it now waits for us to enter a value. So in this case, we're going to enter, let's say three, three plus 12 is 15. And now instead of restarting our application, we can just insert another value. So 719-921-1224 5056 68. So there we go.
So as you can see, together we have actually created an application, which now interact with the user. And so instead of having to restart our application, whenever we want to enter a new value into the console, for the application to use, we could just put it in a while true loop and it will automatically work Wait for us to enter that new value. Although of course, this brings a problem, how do we stop this application? Well, we could actually do is use the break keyword. So the break keyword just exits a loop. So there's nothing really too difficult with it.
So we can actually do well we can actually do is do if A is equal to 34 actually goes to zero, if A is equal to zero break. So what will this break do this break will just stop the loop, that's all. So it'll just say that whatever happens this is now we stop it and then we go to this line and execute from this from the end of the loop. So let's, let's try this. So we can do four, we can do five, we can do six, but if we do zero, it stops, the application stops, we're done. So the break keyword just ends the loop and doesn't execute anything under the break keyword either.
Alright, so we have one last keyword that we're going to take a look at, we have the continue keyword So the continue keyword works a little bit like the break keyword but not not not exactly. So the continue will stop executing whatever is under it and just go back up to the while loop. So if we have zero as you can see, it won't actually do the system on print ln, since continue just goes back to the beginning of the while loop. So if we do 12 filtering for but if we have zero, it just won't do anything. In this case, we're gonna exception accidentally press the wrong thing. So anyway, you understand what the continue keyword does.
So really, if there's anything they need to understand from today's lesson, it's that we have a while loop a a while loop, which input which takes in a Boolean. And then while that Boolean is true, it will keep on executing whatever code is between the square brackets. The while loop also has two keywords. It has a break keyword And it has a continue keyword, the break keyword will just stop the loop and execute what's under it, while the continue keyword will go back to the beginning of the loop. So just it won't execute anything under the continue keyword. And we'll go back to check whether the Boolean is true and executed from the beginning.
That is essentially how we work with loops. Now, there are a couple more things that we need to go over. We also have a do while loop, a for loop, nested loops. Yeah, and nested loops. So we're going to go over exactly what all those are in the next lesson. But for now, this is really all you need to understand.
And if you can get this, then that's very good. So anyway, without further ado, let's assign some homework. Okay. So for homework, your job is to take four variables as input. We're going to take A, B, C and D, let's say ABC and D. And then your job is to multiply those variables together until you get a number that is divisible by 10. So let's just say that we have 123 and four as our input, we're going to multiply one by two, which is two by three, which is six, and then by four, which is 24.
Is 24, divisible by 10. No. So then we're going to multiply 24 by one by two by three and then by four and then check whether that number is divisible by 10. So in this case, it's going to be 24 by 24, times one which is 24. And then nine times two which is 48. Then times three, which is 134.
And then times four which is 400. And So it's going to be 536. I think I, I'm pretty sure I made an error with my math. But yeah, so you're going to do that. And then after we do that 10 times, so in this case, we did it twice. After we do that 10 times, we're just gonna, if we haven't found our divisible by 10 number yet, we're going to break and then stop the loop.
So yeah, that's your homework. It's a little bit more complex than we've been doing so far. But nevertheless, this is also a more useful program. And you're starting to get an idea about how we work with numbers in general. So this is starting to get a little bit more difficult, but it's starting to get more rewarding. give enough time it's not that complex and application, so give it enough time, you should be able to get it pretty soon.
One thing to note though, you may run out of space inside your integer, remember how integer only I think goes up to two point to something billion. So your job later on. You know, you can just go in and use long values if you you know, if you want to just lose use it use long values so that you don't get any errors with the whole. It can't fit into an integer thing. So yeah. Anyway, I wish you luck with homework and I'll see you soon