WEBVTT

00:00.120 --> 00:04.040
Another type of loop is called a while loop.

00:04.080 --> 00:09.200
A while loop runs as long as a condition is true.

00:09.240 --> 00:10.200
Be careful.

00:10.200 --> 00:15.520
If the condition never becomes false, it will run forever.

00:15.560 --> 00:18.120
Okay, so please pay attention to this.

00:18.160 --> 00:19.960
We have an example.

00:19.960 --> 00:24.120
Count equals to five while count greater than zero.

00:24.320 --> 00:26.440
Go and print count.

00:26.480 --> 00:30.360
And here we need to decrease count by one.

00:30.360 --> 00:35.800
Because if we remove this line it will run forever.

00:35.800 --> 00:38.720
Because five is always greater than zero.

00:38.720 --> 00:41.400
And this condition is always true.

00:41.600 --> 00:53.000
And we are going to loop for infinity and make our computer crashes, make, uh, and make the system

00:53.000 --> 00:57.240
blocks or uh, the application not responding.

00:57.240 --> 00:58.840
So please pay attention.

00:58.840 --> 01:06.640
This is a very important thing we need to pay attention for while count greater than zero.

01:06.800 --> 01:09.440
Go and execute print count.

01:09.480 --> 01:17.080
Go and print the count value and decrease with every iteration the count by one.

01:17.080 --> 01:24.760
Let's run the cell count five, four, three, two, one and blast off.

01:24.800 --> 01:33.200
Okay, so we exit this loop and execute this line whenever the loop finish executing okay.

01:33.240 --> 01:40.760
Also we can introduce the else statement with while so here else print.

01:40.760 --> 01:44.800
For example count is no longer than zero.

01:44.840 --> 01:48.200
Let me run 54321.

01:48.240 --> 01:57.800
And with the else statement we can run a block of code once when the condition no longer is true.

01:58.120 --> 02:06.720
So here zero greater than zero no go and execute the else statement by printing count is no longer than

02:06.760 --> 02:07.320
zero.

02:07.440 --> 02:14.790
Okay, so we can use the else statement with the for and with the while loop.

02:14.830 --> 02:17.590
Okay, this is a very good example.

02:17.630 --> 02:19.070
Another example.

02:19.110 --> 02:22.710
Keep asking until the user types quit.

02:22.710 --> 02:27.190
So we have this command which is a string variable.

02:27.230 --> 02:28.670
Empty string variable.

02:28.710 --> 02:36.350
While command dot lower doesn't equal to quit command input, enter a word or quit to exit.

02:36.510 --> 02:43.230
If command equals to quit, go and type for you typed command.

02:43.270 --> 02:46.670
Otherwise go and make goodbye.

02:46.710 --> 02:47.190
Okay.

02:47.390 --> 02:49.110
Enter a word or quit.

02:49.150 --> 02:51.630
To exit any word like this.

02:51.630 --> 02:52.590
Click enter.

02:52.750 --> 02:55.950
You typed this another way.

02:55.990 --> 02:56.630
Alice.

02:56.670 --> 02:57.710
Let me type here.

02:57.710 --> 02:59.670
Alice, you typed Alice.

02:59.710 --> 03:07.070
You keep, uh, printing the input until you put quit.

03:07.070 --> 03:14.430
Click quit, and you can notice that the program quits and display a goodbye message.
