While Loop

Learn the Basic Java Concepts Loops, arrays and methods
14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.44
List Price:  €93.49
You save:  €28.05
£56.04
List Price:  £80.06
You save:  £24.02
CA$95.74
List Price:  CA$136.79
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 64.03
List Price:  CHF 91.47
You save:  CHF 27.44
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,474.37
List Price:  ₨27,821.72
You save:  ₨8,347.35
฿2,588.61
List Price:  ฿3,698.17
You save:  ฿1,109.56
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,312.98
List Price:  R1,875.77
You save:  R562.78
Лв127.91
List Price:  Лв182.73
You save:  Лв54.82
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.73
List Price:  ₪382.49
You save:  ₪114.75
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.05
List Price:  QR364.38
You save:  QR109.32
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,375.71
List Price:  CLP$94,826.51
You save:  CLP$28,450.80
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,392.53
List Price:  DA13,418.47
You save:  DA4,025.94
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q544.06
List Price:  Q777.27
You save:  Q233.20
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,837.79
List Price:  ISK kr14,054.59
You save:  ISK kr4,216.80
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,246.75
List Price:  L1,781.16
You save:  L534.40
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,720.62
List Price:  Ft36,745.32
You save:  Ft11,024.70
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,174.36
List Price:  ARS$87,395.69
You save:  ARS$26,221.33
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,528.94
List Price:  COP$396,486.91
You save:  COP$118,957.97
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲520,857.74
List Price:  ₲744,114.38
You save:  ₲223,256.64
$U2,703.34
List Price:  $U3,862.08
You save:  $U1,158.74
zł282.38
List Price:  zł403.41
You save:  zł121.03
Already have an account? Log In

Transcript

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

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.