1 00:00:00,410 --> 00:00:04,560 There are a few different variations of the loop in Ruby. 2 00:00:04,560 --> 00:00:06,390 One of those is the while loop. 3 00:00:06,390 --> 00:00:10,400 The while loop is similar to the loop statement and 4 00:00:10,400 --> 00:00:13,410 it uses a conditional to perform the logic. 5 00:00:13,410 --> 00:00:18,270 However, the big difference is that the while loop continues to run 6 00:00:18,270 --> 00:00:22,890 as long as the conditional that is set up top continues to return true. 7 00:00:24,060 --> 00:00:28,350 The condition is also specified as part of the argument, the while loop. 8 00:00:29,380 --> 00:00:31,870 Let's see how that works now, using work spaces. 9 00:00:33,120 --> 00:00:35,550 Using a Ruby work space, let's go ahead and 10 00:00:35,550 --> 00:00:40,999 create another file called while _loop.rb. 11 00:00:42,490 --> 00:00:45,690 And let’s go ahead and write a very simple while loop. 12 00:00:45,690 --> 00:00:51,221 We’re going to say the answer is an empty string and 13 00:00:51,221 --> 00:00:56,108 while the answer does not equal the letter n, 14 00:00:56,108 --> 00:01:03,456 we’ll print out do you want me to repeat this pointless loop again? 15 00:01:06,098 --> 00:01:09,763 And then we'll get the answer from standard input, 16 00:01:09,763 --> 00:01:13,269 remove trailing spaces, and make it lowercase. 17 00:01:13,269 --> 00:01:18,855 We write a while loop similar to the way we write a regular loop except we 18 00:01:18,855 --> 00:01:25,309 use the while keyword, and then we set the condition at the top of the while loop. 19 00:01:25,309 --> 00:01:30,909 As soon as that condition is met it will exit the while loop. 20 00:01:30,909 --> 00:01:34,731 Let’s run this and see how it goes. 21 00:01:34,731 --> 00:01:36,511 Do I want to repeat the pointless loop again? 22 00:01:36,511 --> 00:01:37,951 Yes. 23 00:01:37,951 --> 00:01:40,113 Do I want to repeat the pointless loop again? 24 00:01:40,113 --> 00:01:41,591 No. 25 00:01:41,591 --> 00:01:44,719 And we can see that will exit the while loop. 26 00:01:44,719 --> 00:01:49,468 When we use a while_loop, there's no need to manually use the break key word. 27 00:01:54,710 --> 00:02:00,736 Now let's create another file called while_number.rb. 28 00:02:00,736 --> 00:02:02,230 And clear my screen there. 29 00:02:04,910 --> 00:02:09,010 And we're going to create a very simple method that prints hello, 30 00:02:10,860 --> 00:02:16,460 and takes one argument which is the number_of_times, to print hello. 31 00:02:17,810 --> 00:02:22,810 And what we're gonna do is create a simple variable here called i. 32 00:02:22,810 --> 00:02:27,680 When you're writing loops, it's conventional to use the letter i, 33 00:02:27,680 --> 00:02:32,190 j, and k to iterate over the loop. 34 00:02:32,190 --> 00:02:34,860 The reason that we use a lower-case i 35 00:02:34,860 --> 00:02:37,640 is because that's going to means it's an iterator. 36 00:02:38,850 --> 00:02:45,360 So now we'll say, while i is less than the number_of_times, which is the argument 37 00:02:45,360 --> 00:02:51,190 to the function, will print hello, and then this is really important, 38 00:02:52,640 --> 00:02:57,770 we increment i or else we'll cause an infinite loop. 39 00:03:00,230 --> 00:03:02,850 So, now that we've got this method, let's go ahead and call it. 40 00:03:05,050 --> 00:03:08,379 We'll create an empty answer string 41 00:03:11,741 --> 00:03:14,461 And we'll say while the answer is less than 5, 42 00:03:14,461 --> 00:03:17,660 because we don't want to be printing this out too much. 43 00:03:19,830 --> 00:03:23,839 We'll print, how many times do you want to print hello? 44 00:03:27,132 --> 00:03:33,000 And add a little message, enter number greater than five to exit. 45 00:03:35,820 --> 00:03:39,490 Then we'll get the answer and 46 00:03:39,490 --> 00:03:45,160 convert it to an integer and then we can call that method. 47 00:03:46,720 --> 00:03:47,700 With the answer. 48 00:03:51,480 --> 00:03:52,670 Now let's go ahead and run this. 49 00:03:55,680 --> 00:04:00,650 Oh, comparison of string with five failed and that's on line 10. 50 00:04:00,650 --> 00:04:05,889 Let's go ahead and change the answer to zero instead of a string. 51 00:04:09,463 --> 00:04:10,410 Run that again. 52 00:04:11,730 --> 00:04:13,480 How many times do I want to print hello? 53 00:04:13,480 --> 00:04:14,170 How about 3. 54 00:04:14,170 --> 00:04:18,170 Okay, it printed three times. 55 00:04:19,230 --> 00:04:20,740 It printed two times. 56 00:04:21,800 --> 00:04:23,834 And now let me type the number 6. 57 00:04:25,320 --> 00:04:28,440 And then it prints six times and exits. 58 00:04:29,480 --> 00:04:34,108 The reason that prints hello six times and then exits, 59 00:04:34,108 --> 00:04:39,610 is because print_hello is still part of the loop. 60 00:04:39,610 --> 00:04:45,820 This block of code will not be run again until after It exits. 61 00:04:45,820 --> 00:04:49,440 The while loop will then evaluate the condition 62 00:04:49,440 --> 00:04:53,280 in the context of the end of the block of code. 63 00:04:54,610 --> 00:04:58,640 Try practicing writing while loops on your own now using WorkSpaces.