1

00:00:01,080  -->  00:00:07,260
You know understand how switch works and in what scenarios it cannot be used.

2

00:00:07,270  -->  00:00:10,360
So the next question is if it is feasible to use speech.

3

00:00:10,720  -->  00:00:14,280
Then when would you prefer to use it over an if statement.

4

00:00:14,710  -->  00:00:22,110
Let's look at few scenarios when you may want to consider using a switch one reason would be if readability

5

00:00:22,110  -->  00:00:23,400
improves.

6

00:00:23,400  -->  00:00:28,550
That is you feel if the code is more readable the switch statement than with an if statement.

7

00:00:30,350  -->  00:00:31,730
Next is intent.

8

00:00:32,180  -->  00:00:39,370
But using a switch program is deliberately making a statement that only one variable is in more of course

9

00:00:39,370  -->  00:00:42,900
you can use exhibitions like explicit by as a expression.

10

00:00:43,270  -->  00:00:47,080
But that would be very uncommon and probably a bad coding style too.

11

00:00:48,990  -->  00:00:55,010
This example demonstrates both the points readability and turned on the left we have a switch statement

12

00:00:55,140  -->  00:00:55,710
on that right.

13

00:00:55,710  -->  00:00:58,460
We have same code but explicit with an F stick.

14

00:00:58,480  -->  00:01:03,860
And as you can see the switch expression seems to be a little bit more readable in this example.

15

00:01:04,260  -->  00:01:08,080
So if readability is going to improve then you can go for a switch statement.

16

00:01:09,090  -->  00:01:14,830
Our next with regards to indent the switch statement tells us that there is only one variable called

17

00:01:14,870  -->  00:01:16,410
code involved here.

18

00:01:16,920  -->  00:01:21,930
So any future coming up is looking at the code will know that they cannot use any additional variables

19

00:01:22,110  -->  00:01:24,400
as part of the test condition.

20

00:01:24,420  -->  00:01:26,300
So intent is very clear.

21

00:01:26,620  -->  00:01:33,150
Whereas with an F statement Nothing stops a future developer from introducing both as part of his conditions

22

00:01:33,160  -->  00:01:33,610
.

23

00:01:34,050  -->  00:01:40,470
So whoever wrote this statement might have wanted to use only a single variable but that intent is never

24

00:01:40,470  -->  00:01:43,650
formally communicated due to the use of IF statement.

25

00:01:44,490  -->  00:01:48,260
So which can be preferred if readability and Dent are important.

26

00:01:50,060  -->  00:01:53,150
I think that third factor is also very important under dispute.

27

00:01:54,520  -->  00:02:00,910
Switches faster than if this is due to the fact that these liberals are known at compile time itself

28

00:02:01,810  -->  00:02:07,550
and model for each d'Este in most only a single condition there is a switch expression compared with

29

00:02:07,550  -->  00:02:08,720
the Kislev.

30

00:02:09,210  -->  00:02:15,490
This is the switch statement to be internally implemented in a way that makes it faster compared to

31

00:02:15,490  -->  00:02:24,090
an IF statement says which is implemented in an efficient way do this if there are any conditions to

32

00:02:24,090  -->  00:02:29,070
burst then an if statement in the first case will take linear time.

33

00:02:29,340  -->  00:02:35,250
Well a switch statement would take constant time that isn't the case on ornata and conditions would

34

00:02:35,250  -->  00:02:38,040
be tested when it comes to an IF statement.

35

00:02:38,520  -->  00:02:40,410
This should not be considered as an issue.

36

00:02:40,500  -->  00:02:47,010
If there are only a few conditions to test but if there are a large number of conditions are best say

37

00:02:47,040  -->  00:02:54,170
about hundred then converting from if to a speech statement can no longer be regarded as a micro-optimization

38

00:02:55,030  -->  00:03:01,670
time savings maybe very significant and a profiler can be very useful here a profile that helps you

39

00:03:01,670  -->  00:03:06,980
in analyzing the performance of your program and in understanding which parts are slowing down the program

40

00:03:06,980  -->  00:03:07,810
.

41

00:03:07,970  -->  00:03:13,370
If the profiler tells you that your chain of F's is taking a significant time then rewriting it as a

42

00:03:13,370  -->  00:03:16,540
switch may very well be a solution.

43

00:03:16,550  -->  00:03:21,950
One good profiler is a profiler which you can probably try at some point in future.

44

00:03:22,520  -->  00:03:28,220
Personally I did not use it very much but as we have just seen it's an important tool in certain scenarios

45

00:03:28,220  -->  00:03:28,880
.

46

00:03:29,150  -->  00:03:34,320
So we know also understand when a switch statement can be used in preference to an IF statement.

47

00:03:34,520  -->  00:03:35,200
Thank you.
