1 00:00:00,510 --> 00:00:01,540 Hello and welcome. 2 00:00:01,560 --> 00:00:10,310 In this lecture I'm going to explain how the while loop works in the Ruby programming language. 3 00:00:10,860 --> 00:00:14,110 What does a a while loop do. 4 00:00:14,550 --> 00:00:24,490 Basically the one on loop statement will execute repeatedly as long as the condition is true. 5 00:00:24,540 --> 00:00:32,310 So it will execute a block of code as long as specified condition is true. 6 00:00:32,310 --> 00:00:42,220 So the syntax for a while loop is stuck with the key word while thing is specify the condition. 7 00:00:42,330 --> 00:00:50,400 Then you've got the do word which is the reserve key word in Ruby that is optional. 8 00:00:50,480 --> 00:00:54,820 But while loop will cease to work even if you know you do. 9 00:00:55,150 --> 00:01:03,660 And there was you specify the condition then the block of code will execute and now be the day you specify 10 00:01:03,660 --> 00:01:09,030 an end for Ruby to know that you have recently finished this statement. 11 00:01:09,030 --> 00:01:12,090 So we're going to see how this works. 12 00:01:12,120 --> 00:01:20,930 I have got the Ruby interpreter so I was going to declare a few variables first variable is x. 13 00:01:21,030 --> 00:01:29,070 I was sent to a value of one present to create and unknown a variable. 14 00:01:29,070 --> 00:01:30,110 Now why. 15 00:01:30,840 --> 00:01:36,230 And when I send this to 10 I press enter. 16 00:01:36,750 --> 00:01:40,850 I'm going to implement the while loop now and when to say Wow. 17 00:01:42,110 --> 00:01:44,770 So I'm going to specify the condition now. 18 00:01:44,940 --> 00:01:52,240 While X is less down and y. 19 00:01:53,820 --> 00:01:57,530 I want this to happen. 20 00:01:57,900 --> 00:02:00,440 So now I have to specify what I want. 21 00:02:00,480 --> 00:02:03,710 Specify the condition in the while. 22 00:02:03,870 --> 00:02:08,710 Now I need to specify the could block that will execute. 23 00:02:08,760 --> 00:02:12,320 So I'm going to say print. 24 00:02:12,390 --> 00:02:14,660 This is what I want to happen. 25 00:02:14,700 --> 00:02:19,360 Print X comma. 26 00:02:21,760 --> 00:02:32,560 Quotes because I want to be a full stop after each of the line or statement and then I'll say show me 27 00:02:32,590 --> 00:02:34,860 the money. 28 00:02:35,010 --> 00:02:37,540 Sure mean the money 29 00:02:40,280 --> 00:02:51,980 and I'm going to skip that so that he can print it from the statement in separate lines where you use 30 00:02:51,980 --> 00:02:54,150 in the print command. 31 00:02:54,310 --> 00:02:55,990 It doesn't leave spaces. 32 00:02:56,230 --> 00:03:05,110 So if I was there and want it from the execution to be on a separate line I asked why escaping in the 33 00:03:05,240 --> 00:03:06,370 n bear. 34 00:03:06,720 --> 00:03:12,610 Okay now I need to specify the loop condition. 35 00:03:12,610 --> 00:03:17,710 How many times I wanted to loop the big code here. 36 00:03:17,710 --> 00:03:19,250 I wanted to print this. 37 00:03:19,270 --> 00:03:22,810 Now how meta specify if this condition is true. 38 00:03:22,900 --> 00:03:29,640 I wanted to print this X amount of time so one I'll see X.. 39 00:03:29,800 --> 00:03:37,460 So when you use in a loop you need to specify a counter so waste that count him from. 40 00:03:37,780 --> 00:03:39,890 So I'll say a while x. 41 00:03:41,640 --> 00:03:45,280 Plus thought it was 2 1. 42 00:03:45,450 --> 00:03:49,830 What that means is that his time loops true. 43 00:03:50,090 --> 00:03:52,080 This 13 count is one. 44 00:03:52,110 --> 00:04:01,430 Well each time he goes true it will had one each time until it gets to until he becomes less than 10. 45 00:04:01,470 --> 00:04:01,970 Less time. 46 00:04:01,980 --> 00:04:02,690 Why. 47 00:04:02,700 --> 00:04:09,980 Why is Ted was he becomes less and why the loop will stop next to I need to do it. 48 00:04:10,090 --> 00:04:11,690 Typing the word. 49 00:04:12,990 --> 00:04:24,260 And so what's going to happen here is that the cord is going to print numbers from zero to nine. 50 00:04:24,290 --> 00:04:33,770 The condition I have said is that as long as X is less than Y it would check that condition and that 51 00:04:33,770 --> 00:04:35,170 condition is true. 52 00:04:35,450 --> 00:04:40,690 He checks it before the loop is started before he starts in the loop. 53 00:04:40,970 --> 00:04:49,790 Then the body of the loop execute which is this bit here bit will execute them if condition is checked 54 00:04:50,030 --> 00:04:54,360 again when the condition resolves is false. 55 00:04:54,410 --> 00:05:00,260 The loop terminates and this time it goes to the loop. 56 00:05:00,350 --> 00:05:04,260 That's why this makes this plot a quasi means one. 57 00:05:04,480 --> 00:05:06,000 It's time it goes around. 58 00:05:06,040 --> 00:05:12,060 It will and one add one each time until it gets to less than 10. 59 00:05:12,440 --> 00:05:14,770 So you can see his printed out. 60 00:05:14,780 --> 00:05:22,590 Show me the money spent at it nine times because I've said here the condition as long as X is less help 61 00:05:22,650 --> 00:05:25,090 why I want it to print. 62 00:05:25,090 --> 00:05:27,120 MS. Okay. 63 00:05:27,140 --> 00:05:32,860 And each time it is true to check they could have told it to add one. 64 00:05:32,930 --> 00:05:36,640 So that is the first time he goes round it checks the condition. 65 00:05:36,770 --> 00:05:40,160 That's one in Gore's run again plus one. 66 00:05:40,160 --> 00:05:41,710 He has another one. 67 00:05:41,780 --> 00:05:45,660 Gore's run again unsworn until he gets to nine. 68 00:05:45,800 --> 00:05:52,650 It doesn't show 10 because by 10 it will have been force for as long as a condition this true. 69 00:05:52,670 --> 00:05:55,070 It will keep execute in a court. 70 00:05:55,310 --> 00:05:59,260 So that's basically how the while loop works. 71 00:05:59,270 --> 00:06:00,450 Thanks for watching. 72 00:06:00,500 --> 00:06:01,720 And by phone.