‫Welcome back.
‫In this video, we're going to have a look at the while loop.
‫So first of all, the syntax, we start off with the counter and I'm going to set that to zero.
‫Then we use the term while and in parentheses we have the condition.
‫And in this case, I'm just going to say counter lower than ten.
‫And if that's true, then run the code within the Y loop.
‫So first of all, increment the counter just to be sure.
‫And now we can go ahead and write our code.
‫So console dot right line and I'm just going to print out the counter.
‫So for now, nothing too fancy.
‫And finally, we need to use console or read here to see what was written onto the console so that we
‫are simple syntax pretty much the same as the do except for the while is now at the top and not at the
‫bottom.
‫And as you can see, zero through nine is printed onto the console.
‫So this one checks first different than the do while loop which didn't check first, just ran the code
‫to while loop checks first before it does anything.
‫Now as you see, this is very similar but you can go ahead and build your own while loops now.
‫All right now a little challenge for you.
‫Please go ahead and write a little program in which the user can press enter and the number is increased.
‫So it's written onto the console how high the number is.
‫So let's say we have a counter of people and we want to count how many people have entered the bus.
‫So let's say you are a teacher and now you want to make sure that you have all your students with you.
‫So if you're a little people counter and that's exactly what we want to have here.
‫So each time you press enter with no entry.
‫So when it's empty, then go ahead if something else is written in there.
‫So for example, an A or a letter or a anything else rather than an empty string, then go ahead and
‫run the code again.
‫So increase the number by one and until the user enters anything.
‫So pretty much just a simple people counter.
‫And once the user enter something, the people counter will just show, okay, everybody is in the bus
‫and we're good to go.
‫Okay.
‫I hope you try to build that.
‫What we need is a string and I'm going to call that one entered text and I'm going to set that to an
‫empty string for now.
‫And instead of checking the counter here, what I do is I check if enter text equals an empty string.
‫And if that's true, I want to write something like please press enter to increase amount by one and
‫anything else, plus enter if you want to finish counting.
‫All right.
‫So that's pretty much what's going to be printed onto the console.
‫And by the way, we also want to see how many students we have currently inside of our bus.
‫Current people count is.
‫And then here's something that we haven't seen yet or we haven't used yet.
‫And what that will do is it will simply replace this.
‫Curly brackets, zero with the value of the counter.
‫So we haven't used that yet, but that's how you can manipulate your strings a little bit.
‫And so far what we have done was to use concatenation.
‫So we used plus and then the counter, but that's actually a neater way to do that.
‫All right.
‫So let's check that actually, we need to read what the user has entered.
‫So console the read line and we need to store that in entered text.
‫All righty.
‫So now let's check if our little program does what it should.
‫So I'm going to press enter.
‫Current people count as one, enter, two, three, four, and so forth.
‫And once I press anything else, okay, or I enter anything else.
‫For example, K enter current people count is 21 and now I close my program.
‫By the way, what we can write here as well is to tell the user how many people there are.
‫So right line curly brackets, zero.
‫And here is the counter and a little text that I want to write is are inside the bus.
‫People are inside the bus.
‫Press enter to close the program.
‫All right.
‫And that's it.
‫So let's run it again.
‫And we have our little program here, our people counter.
‫Please press enter to increase the amount by one.
‫Let's do that.
‫All right.
‫And now let's.
‫Press anything else w you current people count is six.
‫Six people are inside the bus.
‫Please enter to close or press enter to close the program.
‫Pressing enter.
‫That's done.
‫So now you see how you can already build small programs which actually have a value.
‫I know you can't simply reproduce that into an app, for example, where it would make more sense.
‫But you have the core principles right here, so you just need user input and then you can use your
‫code to run that.
‫Great.
‫So in the next video, we are going to have a little different challenge.
‫So go ahead and try to solve that and see you in the next video.