Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.
VB.NET from beginner to advanced programmer
- Introduction and Installation
- Objects and Events
- Variables
- The basic data types
- Logical Operators
- Relational Operators
- If statements Then
- Arithmetical Operators
- Loops Part 1
- Arrays
- Loops Part 2
- Try Catch statements
- Subs and Functions
- Difference between Scopes
- Select Statements
- Multidimensional arrays
- Structures
- Classes
- Enumerations
- Advanced Comments
- Compiling Directives
We have now come to Part 7, If statements. The If Then statement(or only If Statement) is a block of code that will only be run if the specific condition is met and therefor will return True, if the condition returns False the code will jump past the If block and continue at "End If" which is the end of an If block. The syntax for and If statement are:
Code:If <condition> Then 'code to be executed if the condition are met End If
A very simply example here below:
Code:If True Then MessageBox.Show("Inside the If block") End If
The If statement check if the condition is True, and here it is. This will make the line(s) inside the If block to be executed. In this example a messagebox will be shown. But if we change "True" to "False" in the above code, the messagebox will not be showed since the code will jump past the If block because the conditions where False.
Now you're maybe a little bit confused, What condition? Why test if we have wrote True or False?
Yes the above code has no real function at all, and the If statements there is completely useless. But now, we're going to use what we learned about in the two last lessons; Logical operators and Relational operators to make the If Statement not only "not useless" but also completely necessary.
Firstly we use a relational operator:
Code:Dim x As Integer x = 3 If x > 0 Then MessageBox.Show("x is greater then 0") End If
Now we can check if x is greater then 0 with a relational operator. If x is greater then 0 the "x > 0" operation will return True as you learned about in the last Part, the If statement will then check for a True and a False. If It gets a True from the relational operator the If block will be executed.
Now just of a sudden we can test values and get our code to do different things depending on it. We can also include Logical operators to make it a little bit more complex and also more useful:
Code:Dim x, y As Integer x = 7 y = 5 If x > y And y <> 0 Then MessageBox.Show("x is greater then y and y is not 0") End If
If Then Else Statement:
The opertunitives doesn't end there. We can also create something called an If Then Else statement. This works like an If Then Statement but it has two blocks of codes. If the condition is met(True) the first block of code will be run. But instead of just jumping to the End If straight away if the conditions aren't met(False) the second block of code is executed.
The syntax for an If Then Else Statement looks like this:
Code:If <condition> Then 'code to be executed if the condition are met Else 'code to be executed if the condition are NOT met End If
Here comes and example on how to use it:
The code checks if x is less the y and if y is less then z. If this is True one message will be showed and if It's False another message will be showed.Code:Dim x, y, z As Integer x = 1 y = 9 z = 14 If x < y And y < z Then MessageBox.Show("x is smallest and z is largest") Else MessageBox.Show("The variables are not in size order") End If
If Then ElseIf Else Statement:
We can also add ElseIf blocks to an If Statement, we're able to add as many ElseIf blocks as we want in an If Statement, Not only one as it is with the If block and the Else Block. The ElseIf blocks is working exactly the same as an If block, the If block is the first one and then it's followed by ElseIf blocks and at the end you can add a else block if you want. If the If block's statement are True that block will be run and then the code will jump to the End If, however if it's False it will test if the condition of the first ElseIf block is True, if this also is False it will go to the second one etc. If all conditions are False and there's an Else block this will be executed.
Here's the syntax for the If Then ElseIf Else Statement:
Code:If <condition> Then 'code to be executed if the condition are met ElseIf <condition> Then 'code to be executed if the condition are met but no conditions above are met ElseIf <condition> Then 'code to be executed if the condition are met but no conditions above are met ElseIf <condition> Then 'code to be executed if the condition are met but no conditions above are met Else 'code to be executed if none of the conditions are met End If
Here comes an example on an If Then ElseIf Else Statement:
In this example above we will see the message "x is smallest and z is largest" since this is the first of the conditions that is met. However, If we change x's value to 0, both the two conditions will be True. Then the first block which has a met condition will be the block who is executed, in this case we will see: "x is equals to 0".Code:Dim x, y, z As Integer x = 2 y = 5 z = 8 If x = 0 Then MessageBox.Show("x is equals to 0") ElseIf x < y And y < z Then MessageBox.Show("x is smallest and z is largest") ElseIf x = y And y = z Then MessageBox.Show("all variables has the same value") Else MessageBox.Show("None of the conditions above are met") End If
That was everything for this part, we will later learn about an option to If statements, see you later.
Last edited by Vswe; 03-21-2010 at 03:29 PM.
I always hated the fact that VB didn't use brackets around conditions and I disliked Then...End If. Anyway, nicely done! +rep
It's much easier to follow if you're not experienced. Then you can easily see what is ending, which is harder with only a bracket. But I understands what you means.![]()
What I hate is that if you break the elseif into else if, then you need extra end if's. +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks