We can use if statements as a shorter way to write the equivalent of a match that only matches one case
optionally if it can have an alt containing code to run if the pattern and the if left does not match.
So as super simple example will say let dog two is equal to some pet that is a dog.
And then if let some pet.
That is a dog equals dog too.
We will print out the animal is a dog.
But if it's not a dog.
Then we'll print out.
Not a dog.
So what's going on in here?
So we're basically saying, hey, if some.
Pet is a dog.
Right.
So this is the type we're looking for.
And then we created a dog to the same type.
So if it matches, print out the animal is a dog.
Otherwise it's not a dog.
So we know that this is going to print out the animal is a dog.
But if we happen to make it a cat.
Well, then we would expect the match up here to not match and we would expect it to print, not a dog,
which is what happens.
So.
You can also extend this with elseif and elseif statements.
So it all works the same as if we were just doing regular control flow statements.
So we can also extend this or we can also use while loops.
So let's create a stack and we'll call it a new vector.
And then what we'll do is we'll put in a couple of values.
They will change these two different numbers and now we'll execute a while.
Let some top equals stack dot pop.
Print out.
Top.
So what's going on here?
So we created a new vector, pushed in three values of one, two and three.
And then we're saying, Hey, pop the stack.
And then while it is some number which in this case it will be some one sum to some three.
So while it is some number, we want to print out that number that is being taken off the stack.
And what do we get?
We get 3 to 1, and that is because stacks are last in, first out.
So I just wanted to show you these quick examples of other ways that we can use if.
Examples or wallet in order to match on single statements and then execute some code based on those
match statements.