In this lecture, we are going to discuss control flow in rust applications.
There are two options for controlling the flow of an application's logic.
We have, if else, statements and match expressions.
Both options will be explored in this course.
We'll start with full statements.
They're easier to understand.
Previously, we left off with macros to print a message to the terminal.
What if we want to display different messages based on a condition?
Let's say we're creating a program for an online shop.
Businesses love offering free shipping if a purchase exceeds a minimum threshold.
For example, let's say we want to offer free shipping on purchases over $50 before applying free shipping
to a user's purchase.
We should check the total.
We can use, if else, statements they function similarly to, if else statements in other languages.
First, let's rename the few variable to total.
Above the print line macro, let's add the if keyword an if statement is written with the if keyword
followed by the condition in some languages, conditions are wrapped with parentheses.
Parentheses are not required with rust, we can immediately write the condition.
The condition must be an expression that evaluates to a Boolean value.
For example, we can't set the condition to the total variable.
The compiler will complain about the value, taking a closer look at the error, the compiler is expecting
the Boolean type, but received an integer type.
We should update the condition to return a Boolean value by using a comparison operator.
In the resource section of this lecture, I provide a link to a list of operators.
This table will present an overview of available operators in rust.
Unfortunately, these operators are not grouped by category.
You will need to do some digging to find what you're looking for from this list.
We are going to use an operator for comparing two values.
Let's go back to the editor.
In the condition, we will compare the full variable to 50 with a greater than operator.
If the food variable is greater than 50, we will run a block of code.
We can add the block of code with curly brackets for this scenario.
We are going to tell the user they are qualified for a free shipping.
Russ supports Els and elseif statements.
If a user's total is greater than 20, we should nudge them to add more items to their cart for free
shipping at the moment.
This message will never get printed because the total variable will never store a number above 50.
Therefore, the terminal will not output a message.
Typically, we should provide feedback to the user.
Let's add and else if statements.
The condition for this statement will check if the full variable is greater than 20.
In the else, if block, we will print the following message if you add more items, you can qualify
for free shipping.
And else, if Block will run, if the condition fails for the flock, it must be changed after an if
statement or another LCF statement.
Otherwise, the compiler will throw an error.
We can expect this condition to evaluate to true.
Lastly, we will add an L statement.
The bloc runs when the other conditions fail, it's seen as a last resort if a user's purchase falls
below $20.
It's unlikely they will want to add more items.
We should output a message that says no free shipping.
By chaining conditional statements, we can control the flow of logic.
Let's give our application a test by running the cargo run command.
The program outputs the message from the LCF bloc.
Feel free to modify the numbers we passed into the add function.
The message should change based on the sum in the next lecture.
Let's look at an alternative solution.