1
00:00:00,180 --> 00:00:05,040
In this lecture, we are going to discuss control flow in rust applications.

2
00:00:05,370 --> 00:00:09,840
There are two options for controlling the flow of an application's logic.

3
00:00:10,110 --> 00:00:13,380
We have, if else, statements and match expressions.

4
00:00:13,770 --> 00:00:16,379
Both options will be explored in this course.

5
00:00:16,650 --> 00:00:18,980
We'll start with full statements.

6
00:00:19,230 --> 00:00:20,970
They're easier to understand.

7
00:00:21,690 --> 00:00:26,250
Previously, we left off with macros to print a message to the terminal.

8
00:00:26,550 --> 00:00:30,420
What if we want to display different messages based on a condition?

9
00:00:30,780 --> 00:00:34,440
Let's say we're creating a program for an online shop.

10
00:00:34,830 --> 00:00:40,080
Businesses love offering free shipping if a purchase exceeds a minimum threshold.

11
00:00:40,440 --> 00:00:48,000
For example, let's say we want to offer free shipping on purchases over $50 before applying free shipping

12
00:00:48,030 --> 00:00:49,380
to a user's purchase.

13
00:00:49,500 --> 00:00:50,970
We should check the total.

14
00:00:51,480 --> 00:00:58,260
We can use, if else, statements they function similarly to, if else statements in other languages.

15
00:00:58,590 --> 00:01:02,040
First, let's rename the few variable to total.

16
00:01:04,670 --> 00:01:11,750
Above the print line macro, let's add the if keyword an if statement is written with the if keyword

17
00:01:11,750 --> 00:01:17,360
followed by the condition in some languages, conditions are wrapped with parentheses.

18
00:01:17,720 --> 00:01:22,610
Parentheses are not required with rust, we can immediately write the condition.

19
00:01:23,330 --> 00:01:27,710
The condition must be an expression that evaluates to a Boolean value.

20
00:01:27,950 --> 00:01:32,210
For example, we can't set the condition to the total variable.

21
00:01:35,010 --> 00:01:42,000
The compiler will complain about the value, taking a closer look at the error, the compiler is expecting

22
00:01:42,000 --> 00:01:45,510
the Boolean type, but received an integer type.

23
00:01:45,840 --> 00:01:51,600
We should update the condition to return a Boolean value by using a comparison operator.

24
00:01:51,930 --> 00:01:57,180
In the resource section of this lecture, I provide a link to a list of operators.

25
00:01:59,820 --> 00:02:04,320
This table will present an overview of available operators in rust.

26
00:02:04,650 --> 00:02:08,940
Unfortunately, these operators are not grouped by category.

27
00:02:09,240 --> 00:02:13,890
You will need to do some digging to find what you're looking for from this list.

28
00:02:14,010 --> 00:02:17,550
We are going to use an operator for comparing two values.

29
00:02:17,820 --> 00:02:19,440
Let's go back to the editor.

30
00:02:22,070 --> 00:02:28,130
In the condition, we will compare the full variable to 50 with a greater than operator.

31
00:02:30,780 --> 00:02:35,100
If the food variable is greater than 50, we will run a block of code.

32
00:02:35,460 --> 00:02:39,810
We can add the block of code with curly brackets for this scenario.

33
00:02:39,840 --> 00:02:43,980
We are going to tell the user they are qualified for a free shipping.

34
00:02:48,090 --> 00:02:51,220
Russ supports Els and elseif statements.

35
00:02:51,510 --> 00:02:57,690
If a user's total is greater than 20, we should nudge them to add more items to their cart for free

36
00:02:57,690 --> 00:02:59,400
shipping at the moment.

37
00:02:59,670 --> 00:03:05,940
This message will never get printed because the total variable will never store a number above 50.

38
00:03:06,330 --> 00:03:09,840
Therefore, the terminal will not output a message.

39
00:03:10,170 --> 00:03:13,290
Typically, we should provide feedback to the user.

40
00:03:13,590 --> 00:03:15,900
Let's add and else if statements.

41
00:03:16,230 --> 00:03:21,210
The condition for this statement will check if the full variable is greater than 20.

42
00:03:23,810 --> 00:03:30,290
In the else, if block, we will print the following message if you add more items, you can qualify

43
00:03:30,290 --> 00:03:31,400
for free shipping.

44
00:03:34,040 --> 00:03:40,670
And else, if Block will run, if the condition fails for the flock, it must be changed after an if

45
00:03:40,670 --> 00:03:43,240
statement or another LCF statement.

46
00:03:43,550 --> 00:03:46,280
Otherwise, the compiler will throw an error.

47
00:03:46,610 --> 00:03:49,820
We can expect this condition to evaluate to true.

48
00:03:50,150 --> 00:03:53,000
Lastly, we will add an L statement.

49
00:03:54,280 --> 00:04:01,390
The bloc runs when the other conditions fail, it's seen as a last resort if a user's purchase falls

50
00:04:01,390 --> 00:04:02,680
below $20.

51
00:04:02,800 --> 00:04:05,530
It's unlikely they will want to add more items.

52
00:04:05,770 --> 00:04:09,120
We should output a message that says no free shipping.

53
00:04:11,670 --> 00:04:15,660
By chaining conditional statements, we can control the flow of logic.

54
00:04:15,960 --> 00:04:20,010
Let's give our application a test by running the cargo run command.

55
00:04:22,510 --> 00:04:26,110
The program outputs the message from the LCF bloc.

56
00:04:26,470 --> 00:04:30,430
Feel free to modify the numbers we passed into the add function.

57
00:04:30,760 --> 00:04:34,780
The message should change based on the sum in the next lecture.

58
00:04:34,810 --> 00:04:37,120
Let's look at an alternative solution.

