- When to use a Select Case statement.
- When to use an If..Then..Else Block.
- When to use an If..Then Block.
- When to use Inline Conditional Statements.
- Why program efficiency is important.
This tutorial is aimed at those beginning programming, if you are past this stage you will proberly know most of the stuff involved with regarding to program efficiency.
1) When to use a Select Case statement.
A Select Case is the eqivilant to a switch statement in other languages. A Select Case statement will give the programmer the ability to test multiple conditions for values, efficiently and effectively. Select Case should be used instead of an If Statement if you have more than one value to test from the same variable. Why you ask? Because an If statement is a long and lengthy process when you could make it faster, normally you'd end up having to do something like this when testing the same variable (you could use ElseIf's too, but thats still the same inefficiency):
Dim rndNumber As New Random() Dim rndValue As Integer = rndNumber.Next(5) + 1 If rndValue = 0 Then MessageBox.Show("It equals Zero.") End If If rndValue = 2 Then MessageBox.Show("It equals Two!") End If
This could have been improved when using a Select Case statement, because the Select Case will determine which part of the code to goto immediantly when testing for multiple values. It would be inefficient to replace If statements with switch statements if you didn't need to check for multiple values, because the computer would need to do more compuation to determine which Case it is checking for.
See how this code could have been improved with a Select Case statement:
Dim rndNumber As New Random() Dim rndValue As Integer = rndNumber.Next(5) + 1 Select Case rndValue Case 0 MessageBox.Show("It equals Zero.") Case 2 MessageBox.Show("It equals Two!") End Select
It's more readable, looks a hell of a lot better and its more efficient, what more could you want?
Lets try using a Select Case with a DialogResult:
Select Case MessageBox.Show("Do you really want to exit?", "Exit?", MessageBoxButtons.YesNo) Case Windows.Forms.DialogResult.Yes Me.Close() Case Windows.Forms.DialogResult.No MessageBox.Show("Ok, I won't close because I am cool.") End Select
As you can see, Select Case will handle any type that an If Statement can handle. Lets try handling a Boolean Value (not that i'd recommend using a boolean with a select case, as it can only use a true or false answer, and that is primarily for a If..Then..Else) for the purpose of this example:
Dim lol As Boolean = True Select Case lol Case False MessageBox.Show("It is False!") Case True MessageBox.Show("It is True!") End Select
This code will obviously display a message box when it is True or False. Now, because the keyword False represents 0, and True represents 1, we can change the keyword's to numerals too:
Dim lol As Boolean = True Select Case lol Case 0 MessageBox.Show("It is False!") Case 1 MessageBox.Show("It is True!") End Select
This code will do the same thing as the previous code.
2) When to use an If..Then..Else Block.
The If..Then..Else block is excellent, when used correctly, method of determining whether something is True or False. But however, there is absolutely no point in testing for True and False at the same time, when you would only want to know one side of it; False.
Here is an example of what not to do with an If..Then..Else statement when you'd only like to know one value:
If IsNumeric("1") Then Else MessageBox.Show("Not Numeric") End If
Why? Because you are checking for a value you don't need to know, True.
Here is an example of how to do this efficiently:
If Not IsNumeric("1") Then MessageBox.Show("Not Numeric") End If
If Not IsNumeric("1") Then MessageBox.Show("Not Numeric")
Thus, the only reason you would need to use an If..Then..Else statement is when you'd like to test conditions for true and false (and true and false only).
Let's try the same sort of scenerio with a string:
Dim strToTest As String = "Hello World" If strToTest = "Hallo Warld" Then Else MessageBox.Show("Hallo Warld isn't Hello World at all!") End If
Well, seeing as we already know what the value is, and we know that it's not going to change, it would be more efficient to test the value for the value that we know is more likely going to be in there anyway:
If strToTest = "Hello World" Then MessageBox.Show("I am Hello World.")
Now that we have seen what not to do with an If..Then..Else statement, lets look at some reasonable scenerio's why we would want to use it.
Dim boolValue As Boolean = False If boolValue = True Then MessageBox.Show("boolValue equals True!") Else MessageBox.Show("It must be False!") End If
The above code is a good example of when to use an If..Then..Else statement, because we know what is more likely to appear in Else, and we wouldn't need to do more conditional statement's in Else to satisfy our tests.
3) When to use an If..Then Block.
The If..Then Block is a great way to test for a value when you don't need to test multiple values, and don't need to check for other conditions. Just because a feature is there though, doesn't make it totally nessesary to use it; expecially in an If statement.
Dim rndNumber As New Random() Dim rndValue As Integer = rndNumber.Next(5) + 1 If rndValue = 1 Or rndValue = 2 Or rndValue = 3 Then MessageBox.Show("1, 2, and 3!") End If
Lets review this code. This If statement will check if rndValue equals one, two or three. This would work just as well as a Select Case, but Select Case is much more efficient when testing one variable for multiple values. Here is how this code could have been improved with a Select Case statement.
Select Case rndValue Case 1 To 3 MessageBox.Show("This looks better and is more efficient.") End Select
As the MessageBox.Show() suggests, It is easier to read and it is faster to compute. So it is only wise to use an If..Then statement when you need to, and it couldn't have been made quicker.
Lets take a look at where the If..Then statement is more powerful. Say if we have a checkbox called checkBox1 on our form, this is how we could check to see if it was checked:
If checkBox1.Checked Then MessageBox.Show("checkBox1 Is Checked!") End If
Obviously, an If..Then statement would be more powerful for this if you only wanted to know if it were checked.
4) When to use Inline Conditional Statements.
Inline Conditional Statements don't really bring efficiency to the table, infact they are as fast as the If..Then block to process. Thus, it is important to use an Inline Conditional statement just about the same way you'd use it for the If..Then block. Inline Conditional Statements are good for performing a singular action.
If rndValue = 5 Then End End If
The above code is in a working state, does what it's supposed to do, however, there are two more lines of code than it needs, to perform a very simple action. Lets see how this can improve:
If rndValue = 5 Then End
This looks better and is faster to compile too.
5) Why program efficiency is important.
This is a pretty self-explainatory answer, but many programmers lack the ability to differenciate between inefficient and efficient coding practices. Efficiency is important, because it makes things faster.
Thus, if it was faster to not whack some useless "Loading" screen thats controlled by a Timer set at a pre-defined Interval, just for the self-indulgence of thinking you actually made it do something, you are wrong and it is just wasting someone's time, and isn't faster at all.
To conclude this tutorial, efficiency is a very important aspect of any coding process. Thanks for reading, and I hope you learn't something valuable regarding Program Flow and Efficiency.
This post has been edited by RodgerB: 05 December 2007 - 06:17 PM