While(condition)
How to make loop using the while syntax, its advantages and disadvantages vrs. For loop.
Video Transcript
00:00Although four and the four variations are the most popular and most used type of loop in most programming languages,
00:08there are a few other ones which are while and do-while that we want to look into.
00:13And it's important to know how to work with them, not because, personally, myself, I rarely, well, to be more accurate, I never really use them.
00:21Because you could probably do almost everything with a for loop, and it's so much more convenient and more structured.
00:28But there are developers that still use while, and it's a very common type of looping that some developers like using,
00:35and it's important to know how to use them so we'll know how to read their code.
00:38Or maybe you like the while more than you like the loop, or you'll find yourself in a scenario where whiles would make it more sense than using for loops.
00:50Alright, so let's see how we build a while structure.
00:52And now that we already know what loops do, it should be much quicker and easier.
00:58So I'm just going to create a variable which, again, is going to be our Boolean value that's going to tell us is loop.
01:06And I'm going to set it to true.
01:10And if we wanted to, we could have put in an operation.
01:13And let's start with that, actually.
01:15So I'm going to create a new variable, call it, we're going to be in an integer and set it to zero,
01:19the same way as we would do with a for loop.
01:21And now instead of doing a for loop, I'm going to do a while loop setting it asking, well, I is smaller than 10.
01:29And that's all I had to put into my while for my while to work.
01:33Now, if I would run the application, I would get an endless loop, right, because I'm never making my eye grow.
01:41And I would never reach a scenario where the while would not be true.
01:45Thus, I have to add my I plus plus, and then I could trace my eye.
01:51And it should work very similarly to the way our loop would work.
01:54And it would run all the way through 10.
01:56The reason why 10 and not 9, again, is because I added my I plus plus before I tested it.
02:05Though, if I would do it as the last thing I do in my while, then it would only run it nine times, okay?
02:12Because my eye did upgrade, but it wouldn't trace it again because it would test it before it would trace it again.
02:18So every time it traces it, it only then ups the number.
02:22Now, if you would notice, also, if I would run it as I ran it before, if you would scroll up, it started from 1, right?
02:28And up from 0 because I set my plus before I actually traced.
02:33And thus, it gives you a little bit more flexibility.
02:36But with that flexibility comes a lot more from you that demands me to make sure that you don't create endless loops,
02:43which makes it a little bit harder to operate.
02:47And that's why for loops are so easy because they kind of make it almost, you almost don't need to think too much
02:53because you just set all the rules in the beginning of your for loop and you go ahead.
02:58While a while is a bit more open-ended because you could change things around, okay?
03:03So, for example, if I wanted to, I could set here, I could reset my eye to be a different value, right?
03:09But we wouldn't want to do that because then you would start getting complicated.
03:14All right, so we've seen how to work with the while.
03:16And the same note, we didn't have to set our condition directly in the while.
03:21We could have set here a Boolean value.
03:23So if we wanted to, we could have set a new Boolean value is loop, which would be a Boolean.
03:30And initially, let's say, set it to true.
03:33And if we wanted to, we could have grabbed that value.
03:35And instead of setting it that way, we could have just set is loop.
03:42Now, again, I'm going to have an endless loop because it's always true, right?
03:45True.
03:46But I could then, again, in the end of my while, just set here its value to be equal to the same thing as my loop while loop.
03:55Now, if I wanted to, I could have, in the same type of way as I could have worked with for loop,
04:00as I could work with ifs, I could add end and or and so on.
04:05So it is much more flexible, but it could, all of the stuff that you could do with a while,
04:10you could do with a for only would start looking weird in a for because for is really built.
04:15The for loop is really built in the standard way of what you would 99% of the time need from a looping mechanism.
04:23Okay.
04:24And while a while, while a while, on the other hand, the while is more open ended, which gives you more flexibility,
04:31but with it a little bit more dangerous.
04:33If I would need to recommend something to beginning developer, I would say try to stick to for loops.
04:40Definitely play around with while get familiar with them.
04:43But you'll find that for loops are more forgiving and a little bit easier to manage and maintain.
04:50Now, before we complete this whole topic of while, we're going to move on soon to another topic, which is kind of related to it.
04:56And let's let's before we move on to let's see the difference.
04:59Let's talk about the difference between while and the next item, which is do while, which we're going to look at in the next video.
05:05While will not operate if it's if its initial condition doesn't meet.
05:11So if the initial condition is loop was set to false and I would run it, it would never run our loop.
05:18All right.
05:19And we're going to see in the next video, how could we actually make sure that our loop will run what will run at least one time, even if the condition does not meet.
