You know understand how switch works and in what scenarios it cannot be used. 2
So the next question is if it is feasible to use speech. 3
Then when would you prefer to use it over an if statement. 4
Let's look at few scenarios when you may want to consider using a switch one reason would be if readability 5
improves. 6
That is you feel if the code is more readable the switch statement than with an if statement. 7
Next is intent. 8
But using a switch program is deliberately making a statement that only one variable is in more of course 9
you can use exhibitions like explicit by as a expression. 10
But that would be very uncommon and probably a bad coding style too. 11
This example demonstrates both the points readability and turned on the left we have a switch statement 12
on that right. 13
We have same code but explicit with an F stick. 14
And as you can see the switch expression seems to be a little bit more readable in this example. 15
So if readability is going to improve then you can go for a switch statement. 16
Our next with regards to indent the switch statement tells us that there is only one variable called 17
code involved here. 18
So any future coming up is looking at the code will know that they cannot use any additional variables 19
as part of the test condition. 20
So intent is very clear. 21
Whereas with an F statement Nothing stops a future developer from introducing both as part of his conditions 22
. 23
So whoever wrote this statement might have wanted to use only a single variable but that intent is never 24
formally communicated due to the use of IF statement. 25
So which can be preferred if readability and Dent are important. 26
I think that third factor is also very important under dispute. 27
Switches faster than if this is due to the fact that these liberals are known at compile time itself 28
and model for each d'Este in most only a single condition there is a switch expression compared with 29
the Kislev. 30
This is the switch statement to be internally implemented in a way that makes it faster compared to 31
an IF statement says which is implemented in an efficient way do this if there are any conditions to 32
burst then an if statement in the first case will take linear time. 33
Well a switch statement would take constant time that isn't the case on ornata and conditions would 34
be tested when it comes to an IF statement. 35
This should not be considered as an issue. 36
If there are only a few conditions to test but if there are a large number of conditions are best say 37
about hundred then converting from if to a speech statement can no longer be regarded as a micro-optimization 38
time savings maybe very significant and a profiler can be very useful here a profile that helps you 39
in analyzing the performance of your program and in understanding which parts are slowing down the program 40
. 41
If the profiler tells you that your chain of F's is taking a significant time then rewriting it as a 42
switch may very well be a solution. 43
One good profiler is a profiler which you can probably try at some point in future. 44
Personally I did not use it very much but as we have just seen it's an important tool in certain scenarios 45
. 46
So we know also understand when a switch statement can be used in preference to an IF statement. 47
Thank you.