Control flow is all about deciding where to run code based on if a condition is true or not, or deciding
if to repeatedly continue running some code while the condition is true.
This concept is a fundamental building block in programming.
The most common control flow statements in rust are if statements, and then there are three types of
loops.
So let's go ahead and look at what an if statement is.
So we'll start out by saying Let one equal one, and then we'll we can start an if statement with the
if keyword.
So we'll say if one.
It's greater than zero.
Print out.
True.
So we are checking to see if the condition one greater than zero is true.
But if it's not, then we want to print out false.
And that's what the else keyword does.
Else is going to be our fallback plan.
If none of these other conditions are true, then this is the block of code that we want to evaluate.
So we know that one is greater than zero.
So we got true.
But if we said is one greater than ten and we ran it, now we fall back to our fallback plan a false.
That way we have logic for if any of our conditions are not true.
Another thing that we can do is we can chain in multiple conditions.
So with the else if keyword.
So now we can say is one equal to one.
And if it is, we want to print out equal.
So now if we run this, we expect equal to print out and that's what we get.
And you can have as many else if chained together as you want, in order to accommodate for all possible
conditions that your program might have.
And that's just the basics to the if statements.
So now let's go ahead and start looking at some of the loops.
So we'll comment that out.
And the first loop that we're going to look at is a while or correction is a loop, the key word being
loop.
So to show you what a very simple loop is, we'll say, hey, I just want you to print out loop.
And this is going to be an infinite loop of.
The word loop printing out.
And as you can see, that is what we're getting.
So we can do control C at the same time.
And that's going to send an interrupt to our program so that we can exit out of our infinite loop.
So now let's get a little bit more sophisticated with this loop.
And what we can do is actually we can give our loop a name.
So in this case, I'm going to name a loop.
Called Counter.
And then inside this loop, I'm going to print out the count
and I'm going to set it to numb.
And I need to create the numb variable.
So we'll say let mutable numb equal to zero.
And then what we'll do is we'll create another variable.
On the inside let mutable decrease equal to five.
And then we're going to create another loop inside of our loop.
So now we have a nested loop and you can have as many nested loops as you want.
The more loops you typically have will start affecting your big notation, which is the spatial and
time complexity that your program may have.
So inside this loop.
We are going to print out decreasing.
And point that to decrease.
And then we're going to add in some statements.
So if decrease is equal to four break.
So this is our first key word inside a loop.
So what break is going to do is if this condition is true, then we're going to break out of the loop.
And the loop that this break is referring to is the loop that we're currently running in.
So the innermost loop.
But now we're going to create another if statement.
That says if num is equal to two.
So our variable up here then we want to break out of counter.
So this break is actually referring to our outer loop which we.
Named Counter.
So what we're going to do now is we're going to decrease by one.
And what this statement is saying is essentially decrease equals decrease minus one is just shorthand
for it.
And then outside of our innermost loop, we want to add one to none.
And this is the same thing as saying numb equals numb plus one.
So now if we run this.
We see that down here.
We started out with our count at zero.
That's correct.
And then we decreased from five.
Four, which is correct.
So we enter this loop with our decrease at five.
Decrease is not equal to four so we don't execute the brake num does not equal to two.
So we do not execute this brake, but now we decrease by one and we stay inside this loop.
So now our decrease is four.
Now we can break out.
So we break out, we increase our count or numb by one, which is what caused this count to now print
out one and we repeat the loop again all the way until our count equals to two, and then when our count
equals to two, we satisfy this condition which lets us break out of our outermost loop, which we named
counter.
So again, you can have as many loops as you want inside one another.
And then based on your logic, you can stay in a loop as long as you want, and then you can set up
logic on the inside to be able to break out of loops when conditions are met.
So another type of loop that we're going to look at is called a while loop and a while loop will run
while a condition is true.
So to very simply show this one, we're going to say while well, first off, let's create a mutable
num equal to zero again, and then we'll say while num is less than five.
Print out.
Num.
I want the value.
We'll reference our number and then we will add one.
So now we'll run it out.
And you can see that we printed out and got zero, one, two, three and four and.
Once we hit five with numb, our condition was no longer satisfied because five is not less than five.
So we broke out of the loop.
But now if we said less than or equal to five and then ran it, now we also included five.
And that's how a while loop works.
So now let's look at a for loop.
In a for loop.
It's a great way to loop over elements of a collection.
So think of a vector.
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
in there.
So then four element.
So to start a for loop we use the four key word element is going to be our variable for accessing all
the values inside.
So for element aka all the values inside our vector in and then what's our actual vectors name that
we want to reference.
Well it's vec.
So for all the values and vec.
Let's print them out.
Print one.
And then we want to print out what the element is.
And as you can see, we printed out 0 to 9, which is what we expected.
So now a fun, little cool one that I can show you.
Is we can create a four number in 1 to 4 and then.
Reverse it.
Current line.
And then we want number.
And then afterwards, when our countdown is done, we have liftoff.
Clear.
Run it.
Three, two, one.
Lift off.
So we created one, two, four.
Not including for reverse it, print it out and then exit the loop and provide or print out, lift off.
And that's all it is to loops.
So loops are very powerful.
You're going to use them all the time throughout your code.
But now we have successfully gone over all the basics of programming.