1 00:00:00,690 --> 00:00:01,500 ‫Welcome back. 2 00:00:01,530 --> 00:00:04,600 ‫In this video, we're going to have a look at the while loop. 3 00:00:04,620 --> 00:00:12,360 ‫So first of all, the syntax, we start off with the counter and I'm going to set that to zero. 4 00:00:12,390 --> 00:00:17,730 ‫Then we use the term while and in parentheses we have the condition. 5 00:00:17,730 --> 00:00:24,540 ‫And in this case, I'm just going to say counter lower than ten. 6 00:00:24,930 --> 00:00:28,650 ‫And if that's true, then run the code within the Y loop. 7 00:00:28,650 --> 00:00:32,550 ‫So first of all, increment the counter just to be sure. 8 00:00:32,550 --> 00:00:35,400 ‫And now we can go ahead and write our code. 9 00:00:35,400 --> 00:00:42,750 ‫So console dot right line and I'm just going to print out the counter. 10 00:00:42,990 --> 00:00:45,840 ‫So for now, nothing too fancy. 11 00:00:46,950 --> 00:00:55,590 ‫And finally, we need to use console or read here to see what was written onto the console so that we 12 00:00:55,590 --> 00:01:03,420 ‫are simple syntax pretty much the same as the do except for the while is now at the top and not at the 13 00:01:03,420 --> 00:01:04,020 ‫bottom. 14 00:01:04,020 --> 00:01:08,550 ‫And as you can see, zero through nine is printed onto the console. 15 00:01:08,550 --> 00:01:15,780 ‫So this one checks first different than the do while loop which didn't check first, just ran the code 16 00:01:15,780 --> 00:01:20,010 ‫to while loop checks first before it does anything. 17 00:01:20,640 --> 00:01:27,720 ‫Now as you see, this is very similar but you can go ahead and build your own while loops now. 18 00:01:27,990 --> 00:01:29,850 ‫All right now a little challenge for you. 19 00:01:29,880 --> 00:01:37,620 ‫Please go ahead and write a little program in which the user can press enter and the number is increased. 20 00:01:37,620 --> 00:01:41,670 ‫So it's written onto the console how high the number is. 21 00:01:41,670 --> 00:01:47,070 ‫So let's say we have a counter of people and we want to count how many people have entered the bus. 22 00:01:47,070 --> 00:01:51,660 ‫So let's say you are a teacher and now you want to make sure that you have all your students with you. 23 00:01:51,660 --> 00:01:55,500 ‫So if you're a little people counter and that's exactly what we want to have here. 24 00:01:55,500 --> 00:01:57,750 ‫So each time you press enter with no entry. 25 00:01:57,750 --> 00:02:02,040 ‫So when it's empty, then go ahead if something else is written in there. 26 00:02:02,040 --> 00:02:10,320 ‫So for example, an A or a letter or a anything else rather than an empty string, then go ahead and 27 00:02:10,320 --> 00:02:11,250 ‫run the code again. 28 00:02:11,250 --> 00:02:16,200 ‫So increase the number by one and until the user enters anything. 29 00:02:16,710 --> 00:02:19,530 ‫So pretty much just a simple people counter. 30 00:02:19,530 --> 00:02:24,570 ‫And once the user enter something, the people counter will just show, okay, everybody is in the bus 31 00:02:24,570 --> 00:02:25,770 ‫and we're good to go. 32 00:02:28,110 --> 00:02:28,310 ‫Okay. 33 00:02:28,440 --> 00:02:30,000 ‫I hope you try to build that. 34 00:02:30,000 --> 00:02:39,030 ‫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 35 00:02:39,030 --> 00:02:40,200 ‫empty string for now. 36 00:02:40,200 --> 00:02:47,280 ‫And instead of checking the counter here, what I do is I check if enter text equals an empty string. 37 00:02:48,090 --> 00:02:58,710 ‫And if that's true, I want to write something like please press enter to increase amount by one and 38 00:02:58,710 --> 00:03:05,670 ‫anything else, plus enter if you want to finish counting. 39 00:03:06,300 --> 00:03:06,810 ‫All right. 40 00:03:06,810 --> 00:03:10,020 ‫So that's pretty much what's going to be printed onto the console. 41 00:03:10,020 --> 00:03:17,400 ‫And by the way, we also want to see how many students we have currently inside of our bus. 42 00:03:17,850 --> 00:03:22,650 ‫Current people count is. 43 00:03:22,800 --> 00:03:29,010 ‫And then here's something that we haven't seen yet or we haven't used yet. 44 00:03:29,730 --> 00:03:36,120 ‫And what that will do is it will simply replace this. 45 00:03:36,960 --> 00:03:40,650 ‫Curly brackets, zero with the value of the counter. 46 00:03:40,890 --> 00:03:45,180 ‫So we haven't used that yet, but that's how you can manipulate your strings a little bit. 47 00:03:45,180 --> 00:03:48,600 ‫And so far what we have done was to use concatenation. 48 00:03:48,600 --> 00:03:53,970 ‫So we used plus and then the counter, but that's actually a neater way to do that. 49 00:03:53,970 --> 00:03:54,480 ‫All right. 50 00:03:54,480 --> 00:04:00,660 ‫So let's check that actually, we need to read what the user has entered. 51 00:04:00,660 --> 00:04:12,450 ‫So console the read line and we need to store that in entered text. 52 00:04:14,810 --> 00:04:15,430 ‫All righty. 53 00:04:15,440 --> 00:04:18,680 ‫So now let's check if our little program does what it should. 54 00:04:18,950 --> 00:04:20,450 ‫So I'm going to press enter. 55 00:04:20,490 --> 00:04:24,530 ‫Current people count as one, enter, two, three, four, and so forth. 56 00:04:25,010 --> 00:04:29,810 ‫And once I press anything else, okay, or I enter anything else. 57 00:04:29,810 --> 00:04:36,680 ‫For example, K enter current people count is 21 and now I close my program. 58 00:04:36,680 --> 00:04:43,070 ‫By the way, what we can write here as well is to tell the user how many people there are. 59 00:04:44,330 --> 00:04:47,930 ‫So right line curly brackets, zero. 60 00:04:48,890 --> 00:04:58,610 ‫And here is the counter and a little text that I want to write is are inside the bus. 61 00:05:00,630 --> 00:05:02,910 ‫People are inside the bus. 62 00:05:03,930 --> 00:05:07,980 ‫Press enter to close the program. 63 00:05:09,600 --> 00:05:09,990 ‫All right. 64 00:05:09,990 --> 00:05:10,710 ‫And that's it. 65 00:05:11,220 --> 00:05:12,870 ‫So let's run it again. 66 00:05:13,890 --> 00:05:16,440 ‫And we have our little program here, our people counter. 67 00:05:16,470 --> 00:05:19,020 ‫Please press enter to increase the amount by one. 68 00:05:19,440 --> 00:05:20,310 ‫Let's do that. 69 00:05:20,760 --> 00:05:21,590 ‫All right. 70 00:05:21,600 --> 00:05:23,290 ‫And now let's. 71 00:05:23,340 --> 00:05:27,300 ‫Press anything else w you current people count is six. 72 00:05:27,300 --> 00:05:28,890 ‫Six people are inside the bus. 73 00:05:28,890 --> 00:05:32,850 ‫Please enter to close or press enter to close the program. 74 00:05:32,850 --> 00:05:33,710 ‫Pressing enter. 75 00:05:33,720 --> 00:05:34,410 ‫That's done. 76 00:05:34,530 --> 00:05:39,690 ‫So now you see how you can already build small programs which actually have a value. 77 00:05:39,720 --> 00:05:44,730 ‫I know you can't simply reproduce that into an app, for example, where it would make more sense. 78 00:05:44,730 --> 00:05:50,970 ‫But you have the core principles right here, so you just need user input and then you can use your 79 00:05:50,970 --> 00:05:53,220 ‫code to run that. 80 00:05:53,730 --> 00:05:54,240 ‫Great. 81 00:05:54,240 --> 00:05:58,380 ‫So in the next video, we are going to have a little different challenge. 82 00:05:58,380 --> 00:06:02,850 ‫So go ahead and try to solve that and see you in the next video.