1 00:00:00,000 --> 00:00:07,300 [No Audio] 2 00:00:07,301 --> 00:00:09,433 Loops are one of the fundamental programming 3 00:00:09,434 --> 00:00:11,466 concept, and is present in almost all the 4 00:00:11,467 --> 00:00:14,100 programming languages. They help us in 5 00:00:14,101 --> 00:00:16,600 executing one or more statements up to a 6 00:00:16,601 --> 00:00:19,433 desired number of times. It is a sequence of 7 00:00:19,434 --> 00:00:21,466 instructions or programming statements, which 8 00:00:21,467 --> 00:00:23,866 may be referred to as code blocks, that is 9 00:00:23,867 --> 00:00:25,966 continually repeated until a certain 10 00:00:25,967 --> 00:00:28,000 condition is reached. The condition may be 11 00:00:28,001 --> 00:00:30,500 based on some predetermined number or times 12 00:00:30,501 --> 00:00:33,000 of execution, or some expected result is being 13 00:00:33,001 --> 00:00:35,333 produced, or some expected behavior is being 14 00:00:35,334 --> 00:00:38,133 achieved or reached. So let's start learning the loops. 15 00:00:38,600 --> 00:00:40,433 The simplest form is one which has 16 00:00:40,434 --> 00:00:43,800 no associated condition. To have such a loop, 17 00:00:43,801 --> 00:00:45,900 we will write loop followed by parentheses. 18 00:00:46,433 --> 00:00:48,966 This means that, whatever we include inside 19 00:00:48,967 --> 00:00:51,866 the parentheses, it will be executed infinite times. 20 00:00:52,066 --> 00:00:53,800 Let me add a print statement to the 21 00:00:53,801 --> 00:00:56,577 body of the loop, let us execute now. 22 00:00:56,578 --> 00:01:05,500 [No Audio] 23 00:01:05,501 --> 00:01:07,300 You may note that, the print statement is 24 00:01:07,301 --> 00:01:10,500 executing infinite times, and it keeps on 25 00:01:10,501 --> 00:01:13,600 printing the print statement. Ctrl + C is 26 00:01:13,601 --> 00:01:15,700 used to stop an infinite loop, 27 00:01:15,866 --> 00:01:17,384 so let me stop it. 28 00:01:17,385 --> 00:01:19,530 [No Audio] 29 00:01:19,531 --> 00:01:20,966 Generally speaking, you may not 30 00:01:20,967 --> 00:01:23,600 frequently need this type of loop inside your code. 31 00:01:23,933 --> 00:01:26,366 Such kind of loops are rarely 32 00:01:26,367 --> 00:01:28,866 being used by programmers, even if they intend 33 00:01:28,867 --> 00:01:30,933 to use, they will typically have some 34 00:01:30,934 --> 00:01:34,233 statement inside the body of the loops, 35 00:01:34,466 --> 00:01:36,500 which quits from the body of the 36 00:01:36,501 --> 00:01:38,533 loop, once a certain condition is being reached. 37 00:01:39,000 --> 00:01:40,833 Now, let us look at some more useful 38 00:01:40,834 --> 00:01:43,833 kinds of loops. There are mainly two kinds of 39 00:01:43,834 --> 00:01:46,100 loops apart from the one that we have just 40 00:01:46,101 --> 00:01:48,566 seen, they are known as while loops and for 41 00:01:48,567 --> 00:01:50,800 loops. We will first look at the details of 42 00:01:50,801 --> 00:01:53,566 the while loop. A while loop is used to 43 00:01:53,567 --> 00:01:56,366 repeat a specific block of code in unknown 44 00:01:56,367 --> 00:01:58,766 number of times, until a condition is met. 45 00:01:58,933 --> 00:02:02,000 For example, if we want to ask the user to guess 46 00:02:02,033 --> 00:02:04,466 a number between 1 and 20, we don't know 47 00:02:04,500 --> 00:02:06,700 how many times the user may enter a number 48 00:02:06,701 --> 00:02:08,900 before he can guess the number correctly. 49 00:02:09,199 --> 00:02:11,266 He may do it in the first attempt, or in the 50 00:02:11,267 --> 00:02:13,633 second attempt, or he may take more than two 51 00:02:13,634 --> 00:02:16,233 attempts, the user will keep on guessing 52 00:02:16,234 --> 00:02:18,433 until he accurately guessed the number. 53 00:02:18,600 --> 00:02:20,866 Let us implement this guessing number using the 54 00:02:20,867 --> 00:02:23,366 while loops. I will start by having a 55 00:02:23,367 --> 00:02:25,266 variable of my_number, and we'll 56 00:02:25,267 --> 00:02:26,733 assign it a value of 5. 57 00:02:26,734 --> 00:02:30,266 [No Audio] 58 00:02:30,267 --> 00:02:33,033 Next, I will define a boolean variable and initialize 59 00:02:33,034 --> 00:02:34,700 it from a value of false. 60 00:02:34,701 --> 00:02:41,966 [No Audio] 61 00:02:41,967 --> 00:02:44,366 I will set this variable to true, whenever the 62 00:02:44,367 --> 00:02:46,300 user correctly guess the number, and at that 63 00:02:46,301 --> 00:02:49,133 point I will quit from the loop. I will ask 64 00:02:49,134 --> 00:02:50,733 the user to guess my number. 65 00:02:50,734 --> 00:02:57,766 [No Audio] 66 00:02:57,767 --> 00:03:00,300 Next, I will write a while loop. A while loop 67 00:03:00,301 --> 00:03:02,833 is indicated by the keyword of while followed 68 00:03:02,834 --> 00:03:04,633 by a suitable condition to check for 69 00:03:04,634 --> 00:03:06,866 terminating the loop. As long as the 70 00:03:06,867 --> 00:03:08,933 condition remains true, the loop body will 71 00:03:08,934 --> 00:03:11,100 execute, and as soon as the condition becomes 72 00:03:11,101 --> 00:03:13,233 false, so the loop body will terminate. 73 00:03:13,966 --> 00:03:16,333 In this case, I will terminate the loop when the 74 00:03:16,334 --> 00:03:17,600 guess becomes true. 75 00:03:17,601 --> 00:03:23,100 [No Audio] 76 00:03:23,101 --> 00:03:25,133 At the start of the while loop, this condition 77 00:03:25,134 --> 00:03:27,833 is true, because guess has a value of false, 78 00:03:27,866 --> 00:03:30,000 and false is not equal to true. So therefore, 79 00:03:30,001 --> 00:03:32,166 the condition evaluates to a value of true. 80 00:03:33,066 --> 00:03:35,100 Inside the loop, I will take the user input 81 00:03:35,101 --> 00:03:37,323 and we'll convert it to u8 type. 82 00:03:37,324 --> 00:03:49,833 [No Audio] 83 00:03:49,834 --> 00:03:52,700 If the user input is equal to my_number, 84 00:03:52,701 --> 00:03:53,900 so, I will change the boolean 85 00:03:53,901 --> 00:03:56,400 variable guess to that of true, and if that 86 00:03:56,401 --> 00:03:58,733 is not the case, so I will print a message to 87 00:03:58,734 --> 00:04:00,200 the user to try again. 88 00:04:00,201 --> 00:04:08,400 [No Audio] 89 00:04:08,401 --> 00:04:10,766 Now, what will happen that, the loop will be 90 00:04:10,767 --> 00:04:13,233 executed many times, each time the condition 91 00:04:13,234 --> 00:04:15,066 at the start of the loop will be checked. 92 00:04:15,500 --> 00:04:18,200 The loop will continue to run the code segment, as 93 00:04:18,201 --> 00:04:20,600 long as the condition remains true. When the 94 00:04:20,601 --> 00:04:22,400 condition becomes false, the loop will be 95 00:04:22,401 --> 00:04:24,642 terminated, so let us execute this. 96 00:04:24,643 --> 00:04:31,600 [No Audio] 97 00:04:31,601 --> 00:04:34,733 Ok so I will enter 15, it says try again. 98 00:04:35,066 --> 00:04:37,733 Let me enter 13, and it will say try again. 99 00:04:38,400 --> 00:04:40,633 And now I will enter the correct number, which 100 00:04:40,634 --> 00:04:43,566 is my guess, so I will enter 5. And now it 101 00:04:43,567 --> 00:04:45,700 says, that the guess is correct. And at this 102 00:04:45,701 --> 00:04:48,000 time the loop has also terminated, because the 103 00:04:48,001 --> 00:04:49,800 condition at the start of the while loop 104 00:04:49,801 --> 00:04:51,166 evaluates to false. 105 00:04:52,542 --> 00:04:54,033 Let us see some more examples. 106 00:04:55,033 --> 00:04:57,600 I want the user to enter a number, 107 00:04:57,601 --> 00:04:59,966 and I will tell the user the next number, 108 00:05:00,000 --> 00:05:02,400 following his number, which is divisible by both 109 00:05:02,401 --> 00:05:05,000 2 and 5. Let us implement a simple program. 110 00:05:06,066 --> 00:05:07,666 First I will display a useful 111 00:05:07,667 --> 00:05:09,800 message to the user, so that he knows what the 112 00:05:09,801 --> 00:05:11,630 program is expecting as an input. 113 00:05:11,631 --> 00:05:20,133 [No Audio] 114 00:05:20,134 --> 00:05:21,988 Next I will take the user input. 115 00:05:21,989 --> 00:05:31,566 [No Audio] 116 00:05:31,567 --> 00:05:33,633 I will declare a bool variable, which will be 117 00:05:33,634 --> 00:05:35,700 used to test the condition of the while loop, 118 00:05:35,800 --> 00:05:37,952 initially, it will be set to false value. 119 00:05:37,953 --> 00:05:48,266 [No Audio] 120 00:05:48,267 --> 00:05:50,466 I will next add the while loop, along with a 121 00:05:50,467 --> 00:05:53,319 bool variable for checking when to exit from the loop. 122 00:05:53,320 --> 00:05:59,533 [No Audio] 123 00:05:59,534 --> 00:06:01,500 Inside the body of the loop, I will increment 124 00:06:01,501 --> 00:06:02,969 the value of the number by one. 125 00:06:02,970 --> 00:06:10,500 [No Audio] 126 00:06:10,501 --> 00:06:12,466 Another way of writing the same statement is, 127 00:06:12,467 --> 00:06:15,833 to write number += 1 which 128 00:06:15,866 --> 00:06:19,166 essentially has the same meaning. I will next 129 00:06:19,167 --> 00:06:21,500 check if the number is, divisible_by_2 _5. 130 00:06:22,400 --> 00:06:24,033 The mod will be used for checking this 131 00:06:24,034 --> 00:06:26,066 condition, if the remainder of the number 132 00:06:26,067 --> 00:06:28,600 after dividing by 2 and 5 are 0, so 133 00:06:28,601 --> 00:06:31,503 this means that the number is divisible by both 2 and 5. 134 00:06:31,504 --> 00:06:38,900 [No Audio] 135 00:06:38,901 --> 00:06:42,053 Inside the if statement, I will add a suitable message. 136 00:06:42,054 --> 00:06:50,000 [No Audio] 137 00:06:50,001 --> 00:06:53,100 Next, I will also update the boolean variable value. 138 00:06:53,104 --> 00:06:59,500 [No Audio] 139 00:06:59,501 --> 00:07:01,500 This will enable termination from the while 140 00:07:01,501 --> 00:07:03,666 loop, when next time the condition is 141 00:07:03,667 --> 00:07:06,533 being checked. Okay we are done with the code, 142 00:07:06,534 --> 00:07:07,666 so let's execute. 143 00:07:07,667 --> 00:07:13,133 [No Audio] 144 00:07:13,134 --> 00:07:15,500 I will provide the input of, let's say 12, and 145 00:07:15,533 --> 00:07:18,100 it gives me the next number, which is 146 00:07:18,101 --> 00:07:21,366 divisible_by_2 _5. Please note 147 00:07:21,367 --> 00:07:23,633 that, we can further refine this code and get 148 00:07:23,634 --> 00:07:27,566 rid of the extra variable, that is divisible_by_2 _5 149 00:07:28,566 --> 00:07:29,533 In fact we may note that, 150 00:07:29,534 --> 00:07:31,033 the if condition is basically 151 00:07:31,034 --> 00:07:33,033 controlling the execution of the while loop. 152 00:07:33,333 --> 00:07:35,566 So instead of checking this with the if 153 00:07:35,567 --> 00:07:37,800 statement, we can check it at the top, in 154 00:07:37,801 --> 00:07:39,966 front of the while loop. I will cut this 155 00:07:39,967 --> 00:07:41,966 condition from here, and we'll paste it in 156 00:07:41,967 --> 00:07:43,026 front of the while loop. 157 00:07:43,027 --> 00:07:47,500 [No Audio] 158 00:07:47,501 --> 00:07:50,966 I will delete the variable divisible_by_2 _5, 159 00:07:52,033 --> 00:07:54,499 because we will not need it anymore. 160 00:07:54,500 --> 00:07:56,833 [No Audio] 161 00:07:56,834 --> 00:07:58,300 Finally, I will bring the print 162 00:07:58,301 --> 00:08:00,100 statement outside the body of the while loop, 163 00:08:00,101 --> 00:08:02,833 because I do not want to execute it many times, 164 00:08:03,000 --> 00:08:04,900 rather I would like to execute it when 165 00:08:04,901 --> 00:08:06,540 we have found the desired number. 166 00:08:06,541 --> 00:08:09,200 [No Audio] 167 00:08:09,201 --> 00:08:12,266 Now you may note that, our code is more compact 168 00:08:12,267 --> 00:08:14,466 and short. In summary, when the execution of 169 00:08:14,467 --> 00:08:16,300 the loop is dependent on some condition 170 00:08:16,301 --> 00:08:19,166 inside the loop, then it may be sometimes added 171 00:08:19,167 --> 00:08:21,000 to the condition of the loop itself. 172 00:08:22,033 --> 00:08:24,300 Let us execute to check, if everything has been 173 00:08:24,301 --> 00:08:25,615 coded correctly or not. 174 00:08:25,616 --> 00:08:32,933 [No Audio] 175 00:08:32,934 --> 00:08:35,633 I will use the input of let's say 11, and 176 00:08:35,634 --> 00:08:38,265 it gave me the correct result. There is 177 00:08:38,266 --> 00:08:40,566 however a small computation error in this code. 178 00:08:40,732 --> 00:08:43,133 If I run again this code, then it will 179 00:08:43,134 --> 00:08:45,466 return to me the same number which is not, 180 00:08:46,100 --> 00:08:48,466 which is not a number, after my number is 181 00:08:48,467 --> 00:08:50,666 indicated by the description of the program, 182 00:08:50,667 --> 00:08:53,166 and indicated in the print statement also. 183 00:08:53,500 --> 00:08:56,100 To import this program to always check for the 184 00:08:56,101 --> 00:08:58,966 number after my number, I will add 1 to the 185 00:08:58,967 --> 00:09:00,953 number when while loop starts. 186 00:09:00,954 --> 00:09:06,733 [No Audio] 187 00:09:06,734 --> 00:09:08,400 Now it will give me the correct answer. 188 00:09:08,666 --> 00:09:10,933 Please note that, in the previous code, this 189 00:09:10,934 --> 00:09:13,133 computation error was not there. Because at 190 00:09:13,134 --> 00:09:14,800 the start of the loop, before checking the 191 00:09:14,801 --> 00:09:17,100 condition, we updated the variable number and 192 00:09:17,133 --> 00:09:21,066 adds 1 to its value. A key point to notice 193 00:09:21,067 --> 00:09:23,166 that, we should pay special attention to the 194 00:09:23,167 --> 00:09:25,566 variable values at the start and ending parts 195 00:09:25,567 --> 00:09:28,333 of the loop. That is when the loop starts and 196 00:09:28,334 --> 00:09:29,600 when the loop terminates. 197 00:09:30,593 --> 00:09:31,700 Majority of the times the 198 00:09:31,701 --> 00:09:33,466 error in our code happens at these 199 00:09:33,467 --> 00:09:36,433 point, and we have to be very careful about these. 200 00:09:36,766 --> 00:09:40,066 Okay, one final note before we end this tutorial, 201 00:09:40,566 --> 00:09:42,466 we have pointed out that the 202 00:09:42,467 --> 00:09:44,133 while loop is generally being used in 203 00:09:44,134 --> 00:09:46,266 situations, where we do not know in advance 204 00:09:46,267 --> 00:09:48,900 the number of times we want to execute a code block. 205 00:09:49,133 --> 00:09:51,300 This however, does not mean that, we do 206 00:09:51,301 --> 00:09:53,833 not, that we cannot use it in situations 207 00:09:53,834 --> 00:09:55,700 where we know the actual number of times we 208 00:09:55,701 --> 00:09:57,233 want to execute a code block. 209 00:09:58,066 --> 00:10:00,633 However, for that, we should typically use the for loops, 210 00:10:00,666 --> 00:10:02,533 which will be covered in the next tutorial. 211 00:10:03,600 --> 00:10:05,233 Okay, that brings us to the end of this 212 00:10:05,234 --> 00:10:07,266 tutorial. In the upcoming tutorial, we will be 213 00:10:07,267 --> 00:10:10,133 looking at a for loop with some interesting examples. 214 00:10:10,833 --> 00:10:12,233 Do come back for covering that and 215 00:10:12,234 --> 00:10:14,968 until next tutorial. Happy Rust programming. 216 00:10:14,969 --> 00:10:19,466 [No Audio]