‫-: Hi! Within this lecture, we are going to
‫cover another loop called while loop.
‫So this is while loop
‫and it continues as long as a condition holds.
‫While a condition, a statement is true.
‫In for loop, we continue to loop
‫as long as we are in range of a list or a range.
‫But in while loop, we continue as long as a condition holds.
‫So for example, let me create an integer over here
‫and give it a value of zero.
‫So while J is less than 10,
‫for example.
‫So I'm going to continue getting results out
‫of that loop as long as J is less than 10.
‫So as you see right now
‫if I print something out, like test over here,
‫and if I don't do anything else,
‫I will be in an infinite loop.
‫I will get infinite results
‫because J will be always less than 10.
‫But if I add something to J over here,
‫like if I add J1 in each loop.
‫Every time loop counts,
‫then it will add one to J each time,
‫and eventually it will reach to 10
‫and we will break out of that loop.
‫Or else I will be in an infinite loop,
‫which is not what we want in this case.
‫So make sure you add this J++ over here
‫to add one to J.
‫Now, if I run this, as you can see, I have tests showing up
‫in my log cat.
‫If you count this, 2, 3, 4, 5, 6, 7, 9, 8, 9, 10.
‫So it counted 10 times
‫because it started with zero.
‫It went up to the nine and there was 10,
‫and 10 was actually not less than 10.
‫And it didn't go into the loop.
‫It broke out of the loop.
‫Okay? So if you have a condition
‫and if you just want to continue,
‫as long as that condition holds, you can use while loop.
‫If you have a list, definitely you want to go for full loop
‫because it's very efficient, it's very convenient
‫for you to do.
‫But otherwise, while Loop may make your day better
‫if you have a condition to follow.
‫And of course, you can do whatever you want inside
‫of while Loop as well.
‫For example, you may want to create a new integer
‫like 10 times J, okay.
‫And you can just print M, and rather than test,
‫you will see 0, 10, 20, 30, 40, 50.
‫So it's very efficient as well.
‫So now you know how to work with loops.
‫We're gonna stop here and continue with other fundamentals
‫of Java programming language within the next lecture.