1 00:00:05,890 --> 00:00:13,450 Control flow is all about deciding where to run code based on if a condition is true or not, or deciding 2 00:00:13,450 --> 00:00:17,080 if to repeatedly continue running some code while the condition is true. 3 00:00:17,970 --> 00:00:21,690 This concept is a fundamental building block in programming. 4 00:00:21,900 --> 00:00:27,510 The most common control flow statements in rust are if statements, and then there are three types of 5 00:00:27,510 --> 00:00:28,230 loops. 6 00:00:28,710 --> 00:00:32,220 So let's go ahead and look at what an if statement is. 7 00:00:32,670 --> 00:00:40,080 So we'll start out by saying Let one equal one, and then we'll we can start an if statement with the 8 00:00:40,080 --> 00:00:40,920 if keyword. 9 00:00:40,920 --> 00:00:43,530 So we'll say if one. 10 00:00:44,320 --> 00:00:46,000 It's greater than zero. 11 00:00:47,930 --> 00:00:49,880 Print out. 12 00:00:51,590 --> 00:00:52,200 True. 13 00:00:54,380 --> 00:00:59,710 So we are checking to see if the condition one greater than zero is true. 14 00:00:59,720 --> 00:01:06,620 But if it's not, then we want to print out false. 15 00:01:06,620 --> 00:01:09,650 And that's what the else keyword does. 16 00:01:10,010 --> 00:01:13,510 Else is going to be our fallback plan. 17 00:01:13,520 --> 00:01:20,900 If none of these other conditions are true, then this is the block of code that we want to evaluate. 18 00:01:20,900 --> 00:01:23,900 So we know that one is greater than zero. 19 00:01:23,900 --> 00:01:25,010 So we got true. 20 00:01:26,160 --> 00:01:34,260 But if we said is one greater than ten and we ran it, now we fall back to our fallback plan a false. 21 00:01:34,290 --> 00:01:39,450 That way we have logic for if any of our conditions are not true. 22 00:01:40,110 --> 00:01:44,700 Another thing that we can do is we can chain in multiple conditions. 23 00:01:44,700 --> 00:01:47,250 So with the else if keyword. 24 00:01:47,250 --> 00:01:51,960 So now we can say is one equal to one. 25 00:01:52,260 --> 00:01:57,420 And if it is, we want to print out equal. 26 00:01:59,440 --> 00:02:03,940 So now if we run this, we expect equal to print out and that's what we get. 27 00:02:03,940 --> 00:02:11,680 And you can have as many else if chained together as you want, in order to accommodate for all possible 28 00:02:11,680 --> 00:02:14,530 conditions that your program might have. 29 00:02:15,590 --> 00:02:19,300 And that's just the basics to the if statements. 30 00:02:19,310 --> 00:02:22,970 So now let's go ahead and start looking at some of the loops. 31 00:02:23,330 --> 00:02:25,100 So we'll comment that out. 32 00:02:25,100 --> 00:02:35,270 And the first loop that we're going to look at is a while or correction is a loop, the key word being 33 00:02:35,270 --> 00:02:35,960 loop. 34 00:02:36,230 --> 00:02:46,190 So to show you what a very simple loop is, we'll say, hey, I just want you to print out loop. 35 00:02:47,760 --> 00:02:51,570 And this is going to be an infinite loop of. 36 00:02:52,540 --> 00:02:54,490 The word loop printing out. 37 00:02:55,540 --> 00:02:57,520 And as you can see, that is what we're getting. 38 00:02:57,520 --> 00:03:01,660 So we can do control C at the same time. 39 00:03:01,660 --> 00:03:07,330 And that's going to send an interrupt to our program so that we can exit out of our infinite loop. 40 00:03:08,420 --> 00:03:11,750 So now let's get a little bit more sophisticated with this loop. 41 00:03:11,750 --> 00:03:19,250 And what we can do is actually we can give our loop a name. 42 00:03:19,250 --> 00:03:21,830 So in this case, I'm going to name a loop. 43 00:03:22,760 --> 00:03:24,830 Called Counter. 44 00:03:26,000 --> 00:03:34,400 And then inside this loop, I'm going to print out the count 45 00:03:38,570 --> 00:03:40,160 and I'm going to set it to numb. 46 00:03:40,160 --> 00:03:42,710 And I need to create the numb variable. 47 00:03:42,830 --> 00:03:46,850 So we'll say let mutable numb equal to zero. 48 00:03:48,980 --> 00:03:52,940 And then what we'll do is we'll create another variable. 49 00:03:55,120 --> 00:04:02,140 On the inside let mutable decrease equal to five. 50 00:04:03,220 --> 00:04:06,340 And then we're going to create another loop inside of our loop. 51 00:04:06,340 --> 00:04:11,140 So now we have a nested loop and you can have as many nested loops as you want. 52 00:04:14,750 --> 00:04:26,150 The more loops you typically have will start affecting your big notation, which is the spatial and 53 00:04:26,150 --> 00:04:29,540 time complexity that your program may have. 54 00:04:30,470 --> 00:04:32,270 So inside this loop. 55 00:04:34,980 --> 00:04:38,460 We are going to print out decreasing. 56 00:04:40,290 --> 00:04:43,470 And point that to decrease. 57 00:04:44,970 --> 00:04:47,250 And then we're going to add in some statements. 58 00:04:47,250 --> 00:04:51,920 So if decrease is equal to four break. 59 00:04:51,930 --> 00:04:55,740 So this is our first key word inside a loop. 60 00:04:55,740 --> 00:05:02,430 So what break is going to do is if this condition is true, then we're going to break out of the loop. 61 00:05:02,610 --> 00:05:08,220 And the loop that this break is referring to is the loop that we're currently running in. 62 00:05:08,220 --> 00:05:09,750 So the innermost loop. 63 00:05:10,200 --> 00:05:12,480 But now we're going to create another if statement. 64 00:05:13,360 --> 00:05:18,550 That says if num is equal to two. 65 00:05:18,940 --> 00:05:25,630 So our variable up here then we want to break out of counter. 66 00:05:27,910 --> 00:05:32,620 So this break is actually referring to our outer loop which we. 67 00:05:33,450 --> 00:05:34,800 Named Counter. 68 00:05:35,190 --> 00:05:40,800 So what we're going to do now is we're going to decrease by one. 69 00:05:41,370 --> 00:05:48,510 And what this statement is saying is essentially decrease equals decrease minus one is just shorthand 70 00:05:48,510 --> 00:05:49,170 for it. 71 00:05:49,770 --> 00:05:55,260 And then outside of our innermost loop, we want to add one to none. 72 00:05:55,260 --> 00:06:00,690 And this is the same thing as saying numb equals numb plus one. 73 00:06:02,350 --> 00:06:04,240 So now if we run this. 74 00:06:05,080 --> 00:06:07,060 We see that down here. 75 00:06:07,060 --> 00:06:09,130 We started out with our count at zero. 76 00:06:09,130 --> 00:06:10,090 That's correct. 77 00:06:10,090 --> 00:06:12,610 And then we decreased from five. 78 00:06:13,740 --> 00:06:15,030 Four, which is correct. 79 00:06:15,030 --> 00:06:18,540 So we enter this loop with our decrease at five. 80 00:06:20,770 --> 00:06:25,960 Decrease is not equal to four so we don't execute the brake num does not equal to two. 81 00:06:25,960 --> 00:06:31,510 So we do not execute this brake, but now we decrease by one and we stay inside this loop. 82 00:06:31,510 --> 00:06:33,550 So now our decrease is four. 83 00:06:35,160 --> 00:06:37,380 Now we can break out. 84 00:06:38,700 --> 00:06:47,190 So we break out, we increase our count or numb by one, which is what caused this count to now print 85 00:06:47,190 --> 00:06:54,660 out one and we repeat the loop again all the way until our count equals to two, and then when our count 86 00:06:54,660 --> 00:07:02,370 equals to two, we satisfy this condition which lets us break out of our outermost loop, which we named 87 00:07:02,370 --> 00:07:03,120 counter. 88 00:07:03,590 --> 00:07:08,970 So again, you can have as many loops as you want inside one another. 89 00:07:09,840 --> 00:07:14,820 And then based on your logic, you can stay in a loop as long as you want, and then you can set up 90 00:07:14,820 --> 00:07:19,260 logic on the inside to be able to break out of loops when conditions are met. 91 00:07:20,370 --> 00:07:26,310 So another type of loop that we're going to look at is called a while loop and a while loop will run 92 00:07:26,310 --> 00:07:29,280 while a condition is true. 93 00:07:29,790 --> 00:07:40,140 So to very simply show this one, we're going to say while well, first off, let's create a mutable 94 00:07:40,140 --> 00:07:49,260 num equal to zero again, and then we'll say while num is less than five. 95 00:07:50,430 --> 00:07:51,540 Print out. 96 00:07:53,000 --> 00:07:53,720 Num. 97 00:07:54,560 --> 00:07:55,760 I want the value. 98 00:07:55,790 --> 00:08:02,210 We'll reference our number and then we will add one. 99 00:08:04,700 --> 00:08:05,840 So now we'll run it out. 100 00:08:05,840 --> 00:08:10,910 And you can see that we printed out and got zero, one, two, three and four and. 101 00:08:11,930 --> 00:08:18,360 Once we hit five with numb, our condition was no longer satisfied because five is not less than five. 102 00:08:18,380 --> 00:08:21,160 So we broke out of the loop. 103 00:08:21,170 --> 00:08:27,560 But now if we said less than or equal to five and then ran it, now we also included five. 104 00:08:27,560 --> 00:08:29,600 And that's how a while loop works. 105 00:08:30,680 --> 00:08:33,590 So now let's look at a for loop. 106 00:08:34,820 --> 00:08:36,080 In a for loop. 107 00:08:37,810 --> 00:08:41,260 It's a great way to loop over elements of a collection. 108 00:08:41,260 --> 00:08:43,690 So think of a vector. 109 00:08:43,690 --> 00:09:00,340 So let's do vec vec of i eight equals to 0 to 10 and then we want to collect those values and put them 110 00:09:00,340 --> 00:09:01,000 in there. 111 00:09:01,000 --> 00:09:03,220 So then four element. 112 00:09:03,460 --> 00:09:12,940 So to start a for loop we use the four key word element is going to be our variable for accessing all 113 00:09:12,940 --> 00:09:14,770 the values inside. 114 00:09:14,770 --> 00:09:22,870 So for element aka all the values inside our vector in and then what's our actual vectors name that 115 00:09:22,870 --> 00:09:24,520 we want to reference. 116 00:09:24,520 --> 00:09:25,210 Well it's vec. 117 00:09:25,210 --> 00:09:27,700 So for all the values and vec. 118 00:09:30,540 --> 00:09:31,620 Let's print them out. 119 00:09:32,460 --> 00:09:33,530 Print one. 120 00:09:36,600 --> 00:09:39,150 And then we want to print out what the element is. 121 00:09:42,470 --> 00:09:47,660 And as you can see, we printed out 0 to 9, which is what we expected. 122 00:09:47,900 --> 00:09:51,260 So now a fun, little cool one that I can show you. 123 00:09:52,840 --> 00:10:02,080 Is we can create a four number in 1 to 4 and then. 124 00:10:03,400 --> 00:10:04,360 Reverse it. 125 00:10:13,680 --> 00:10:14,790 Current line. 126 00:10:19,990 --> 00:10:22,060 And then we want number. 127 00:10:25,500 --> 00:10:31,830 And then afterwards, when our countdown is done, we have liftoff. 128 00:10:35,770 --> 00:10:36,490 Clear. 129 00:10:36,940 --> 00:10:37,810 Run it. 130 00:10:38,080 --> 00:10:39,100 Three, two, one. 131 00:10:39,100 --> 00:10:39,610 Lift off. 132 00:10:39,610 --> 00:10:42,150 So we created one, two, four. 133 00:10:42,160 --> 00:10:52,450 Not including for reverse it, print it out and then exit the loop and provide or print out, lift off. 134 00:10:52,630 --> 00:10:54,250 And that's all it is to loops. 135 00:10:54,250 --> 00:10:56,260 So loops are very powerful. 136 00:10:56,260 --> 00:10:59,950 You're going to use them all the time throughout your code. 137 00:11:01,490 --> 00:11:07,070 But now we have successfully gone over all the basics of programming.