1 00:00:00,270 --> 00:00:02,310 Hello everybody and welcome back. 2 00:00:02,320 --> 00:00:07,340 And in this video right now we will cover the while loop in our python programs. 3 00:00:07,860 --> 00:00:13,530 So we already covered two loops which is the for loop and if else loop statement right now what we want 4 00:00:13,530 --> 00:00:17,610 to do is cover the while loop which is a loop that we will use. 5 00:00:17,790 --> 00:00:21,510 It is mostly used in order to perform actions forever. 6 00:00:21,510 --> 00:00:29,040 For example if you just type here while x x equals one and then do some action it will perform those 7 00:00:29,040 --> 00:00:36,810 actions forever In Case X really is one in case it is not one it will not perform anything. 8 00:00:36,810 --> 00:00:43,890 So let us actually start by as always opening up our Python interpreter and these syntax is similar 9 00:00:43,900 --> 00:00:45,990 for the four and a half loop. 10 00:00:45,990 --> 00:00:52,100 So basically you just type while and then some statement and then two dots and then you basically present 11 00:00:52,100 --> 00:00:59,040 through then tab the next command and you specify what you want to do if or while that statement is 12 00:00:59,040 --> 00:01:00,080 true. 13 00:01:00,150 --> 00:01:04,110 So let's see simple use also a equals three 14 00:01:06,840 --> 00:01:10,780 while a equals three. 15 00:01:10,830 --> 00:01:19,710 So double times equals since that is actually changing the the checking the value of eight with debit 16 00:01:20,640 --> 00:01:28,450 print Hello And if you enter this you will see that this will print Hello forever. 17 00:01:28,460 --> 00:01:30,710 Now why this will print Hello forever. 18 00:01:30,710 --> 00:01:35,300 Well basically because a is always three we specified eight to be three. 19 00:01:35,290 --> 00:01:37,100 So let me just close this. 20 00:01:37,130 --> 00:01:44,750 So control C2 interrupts as we can see keyboard we interrupt what we actually type read here first of 21 00:01:44,750 --> 00:01:51,800 all a while three then we type here while a equals equals three and then we set print Hello. 22 00:01:52,010 --> 00:01:55,520 It printed Hello forever and it would basically go forever. 23 00:01:55,640 --> 00:02:05,010 In case I didn't stop it because A will always be three but if we just type here A equals 24 00:02:07,600 --> 00:02:07,970 Whoops 25 00:02:11,290 --> 00:02:22,930 whoops can find equal equals two and then we pressed once again you will see that this actually or wait 26 00:02:22,980 --> 00:02:24,030 I forgot to print. 27 00:02:24,120 --> 00:02:38,590 So a equals three while a equals three A equals two print eight and you can see it only printed a once 28 00:02:38,620 --> 00:02:43,560 in indeed it go forever as in the previous video or parliament as in the previous example. 29 00:02:43,810 --> 00:02:48,760 What this basically means is as we can see right here a set of three. 30 00:02:48,760 --> 00:02:57,490 So while a plus three we would print a but since at the at the In Our while loop we specified a to b 31 00:02:57,490 --> 00:03:04,150 to it didn't really print a forever since in the next loop when it came here it was checking if a equals 32 00:03:04,150 --> 00:03:07,480 three but eight wasn't three then it was two then. 33 00:03:07,510 --> 00:03:13,240 So it basically broke the loop and it only printed out a to b to one since it only went through this 34 00:03:13,240 --> 00:03:14,260 entire go once. 35 00:03:15,190 --> 00:03:20,530 So that is the simple use of the while loop you can also just type it like this. 36 00:03:20,560 --> 00:03:24,630 So set up boolean value for K which is either true or false. 37 00:03:24,640 --> 00:03:24,820 Right. 38 00:03:24,820 --> 00:03:30,610 Now let's say true and then while a Brent hello. 39 00:03:30,640 --> 00:03:37,480 Now you can guess that this will bring Hello forever since it basically says while true print pillow 40 00:03:37,870 --> 00:03:41,500 as we can see if I press your entry it will print Hello forever. 41 00:03:41,500 --> 00:03:44,740 You can also do it basically while true. 42 00:03:46,200 --> 00:03:48,700 Whoops I didn't type hear anything print. 43 00:03:49,270 --> 00:03:50,170 It's true. 44 00:03:50,830 --> 00:03:53,770 And they will also print it's true forever. 45 00:03:53,770 --> 00:03:56,040 You can also specify a to be false. 46 00:03:56,080 --> 00:04:05,170 And then while a which means while false print is false and you will see this won't print anything since 47 00:04:05,230 --> 00:04:09,290 A is false and this will not go through a loop it will only go through. 48 00:04:09,530 --> 00:04:15,870 It's weave this spaceflight statement is true if it is not true it will skip the loop. 49 00:04:15,880 --> 00:04:20,050 So that is a good thing to know it can be useful for many things. 50 00:04:21,400 --> 00:04:27,790 Now let's see a combination for example of this slope and a different slope that we used before which 51 00:04:27,790 --> 00:04:29,670 is the if else statement. 52 00:04:29,710 --> 00:04:37,190 So let's call this and write our next program and nano non nano Python for pi. 53 00:04:37,450 --> 00:04:40,760 We specified the path the python user in Python. 54 00:04:40,990 --> 00:04:42,850 And now we can write our program. 55 00:04:42,850 --> 00:04:46,580 So let's set some value x to be true. 56 00:04:48,020 --> 00:04:50,920 So we set the value x to be true. 57 00:04:50,930 --> 00:04:52,760 Nothing really there to explain. 58 00:04:52,760 --> 00:04:55,370 Now let's start our while loop while x. 59 00:04:55,380 --> 00:05:00,540 So while x then two dots we tab the next commander we will specify the right here. 60 00:05:01,010 --> 00:05:08,740 So basically already now we know that it will execute the commands in while loop since x is true. 61 00:05:08,750 --> 00:05:10,560 So it will not keep the X. 62 00:05:10,700 --> 00:05:12,010 The while loop. 63 00:05:12,020 --> 00:05:20,540 So right now let's specify something called user input for example or user name and then we equal that 64 00:05:20,570 --> 00:05:22,340 and use the raw input. 65 00:05:22,700 --> 00:05:24,820 We do not need to specify anything before. 66 00:05:25,070 --> 00:05:27,800 We only specify when we want to use an integer. 67 00:05:27,800 --> 00:05:36,140 So right now we want to get a string from the user so raw input enter name here and we close on the 68 00:05:36,170 --> 00:05:40,310 one time parentheses and let's use now. 69 00:05:40,460 --> 00:05:53,160 And if l slope so if it's user name equals equals to Q which basically in our case will stand for quit 70 00:05:53,190 --> 00:05:54,360 or let's actually. 71 00:05:54,400 --> 00:06:01,140 Well let's leave it on Q So and then we specify two dots break we will type your break. 72 00:06:01,350 --> 00:06:07,800 Now the break command basically breaks out of the loop equal in our case closed the entire program since 73 00:06:07,800 --> 00:06:10,340 our entire program is a simple one. 74 00:06:10,470 --> 00:06:20,010 While loop but e the use it or break as I said is only used to break out of a wall of a loop it can 75 00:06:20,010 --> 00:06:29,460 be used in our infinite loop so such as this one until you specify Q it will work so let's continue 76 00:06:29,460 --> 00:06:29,960 this program. 77 00:06:29,970 --> 00:06:41,700 So if user name equals Q You'll break out of this loop else we can just type here print shallow and 78 00:06:41,700 --> 00:06:49,770 then we specify the S for the string and then specify the percent S for user name and this will print 79 00:06:49,770 --> 00:06:57,690 out shallow and then the name for every name we type until we specify Q Now if we specify Q It will 80 00:06:57,690 --> 00:07:00,420 break out of the loop and close our program. 81 00:07:00,420 --> 00:07:02,700 So let's see how this will work. 82 00:07:02,730 --> 00:07:11,520 So we control 0 to save control X to exit we see h modded to be an executable and then we can run it 83 00:07:11,880 --> 00:07:18,300 as we can see pops as you can see it asks us for names so let's say first name John. 84 00:07:19,020 --> 00:07:19,790 Hello John. 85 00:07:19,800 --> 00:07:23,420 And it will also write after death asks us for another name. 86 00:07:23,670 --> 00:07:27,450 We can specify here Jack and it will say hello Jack. 87 00:07:27,450 --> 00:07:31,500 And now this will do basically this forever until we type here. 88 00:07:31,500 --> 00:07:39,690 Q And if we tap your Q You can see it will close our program since we said if the name equals Q You'll 89 00:07:39,690 --> 00:07:41,000 break out of the loop. 90 00:07:41,520 --> 00:07:44,280 So that is another useful thing to do. 91 00:07:44,370 --> 00:07:50,820 As you saw right here there wouldn't really be another way to close this program properly if there wasn't 92 00:07:50,970 --> 00:07:54,350 a statement for a simple command to break out of the loop. 93 00:07:54,360 --> 00:07:59,520 So that would be about it for the basics of while loop. 94 00:07:59,520 --> 00:08:06,570 For now we covered all of these three loops which is for if and a failed statement and while loop and 95 00:08:06,660 --> 00:08:12,090 in the next video we will go into something called lists which we already saw in the previous video 96 00:08:12,120 --> 00:08:14,160 but we didn't really explain it there. 97 00:08:14,150 --> 00:08:20,160 Well right now we want to explain this in a little bit more detailed so that would be about it. 98 00:08:20,160 --> 00:08:23,280 For this tutorial and I whole pursuing the next one by.