Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

    vbProgramming
Tutorials - Basic Block Statements
 
       
vbProgramming Home :: vbProgramming Forums :: Tutorials :: Contact :: Links 
 
This tutorial will teach you how to do Block statements in VB.NET. Block Statements include statements that check what a value is (the If statement) A Loop (The While Loop).
 

 

 

The If Statement

After all the examples in the previous tutorials, I'm sure you already know what an If statement is. I'm sure you know how its used and the way it works.  We are going into more detail about If statements. We already know that the If statement checks what a value is. You could say:

Dim Jumping As Boolean

If
Jumping = True Then
MessageBox.Show("Mario is Jumping")
End
If

We know what this does. It checks weather Jumping is True or False. If it is True, then a MessageBox appears, saying, "Mario is Jumping."

 There is another way to 'shorten' the If statement ONLY when you use a Boolean.

Dim
Jumping As Boolean

If
Jumping Then
MessageBox.Show("Mario is Jumping")
End
If

Well, VB.NET takes the statement, 
If
Jumping Then
As:
If Jumping = True Then
Its just a quicker way of writing this. But, in some cases, depending on what you name the variable, it wouldn't make sense to you:
If MyVariable Then 
That wouldn't make sense to you. But to the computer it still means:
If MyVariable = True Then 
My suggestion to you is, name your Booleans as verbs. You can say things like:
If Running Then
If
MarioIsUnderwater Then
If
BeingAttacked Then
If
HoldingSword Then
Of course, each If statement must be followed by "End If", but I just listed the above If Statements just for the sake of seeing what they would look like if you used verbs. Try to name them as verbs. If you cant, then don't. Use the long way to write them.

Well, what if you had a program that generated a random number (1 - 10). You are supposed to get a number greater than 7. How would you check if you number is greater than 7? Simple, you would do this: 
If MyLuckyNumber > 7 Then
You use < instead of > to check whether it is less than 7. 
How would you check if it is not 7? There are 2 ways to do this:
If MyLuckyNumber <> 7 Then
or:
If Not MyLuckyNumber = 7 Then

I prefer the first way. Its easier to read and understand. But for Booleans, I would prefer the other way:
If Not Running Then

VB.NET is like English, compared to other languages. Look at this:
If MyLuckyNumber = 7 Or MyLuckyNumber = 9 Then
Simple right? What if your program generated 2 numbers? How would you check for a combination of numbers?
If MyLuckyNumber = 7 And MyLuckyNumber2 = 7 Then
Now that's easy to understand! Notice one thing though. You have to say MyLuckyNumber twice. Let's go back to the "Or" statement, lets make it shorter.
If MyLuckyNumber = 7 Or 9 Then
See? Simple, it checks weather MyLuckyNumber is 7 or 9. Now lets go back to the And statement and make that shorter:
If (MyLuckyNumber And MyLuckyNumber2) = 7 Then
That's easy to understand. The weird thing is, why do we have parentheses?  If you don't have parentheses - for some reason - it would only check weather MyLuckyNumber2 is equal to 7. It wouldn't check for both. Basically, you're grouping MyLuckyNumber and MyLuckyNumber2, and checking weather they're both 7.

Now that's all for the If Statement. We are moving on to Loops.

Loops


A Loop is basically something repeated over and over again until you tell it to stop. If you wanted to check whether "time was up" in a game(10 seconds),  you would do this: After each second (there's a way you can count seconds, we'll get into that later) add a variable by one. When the variable becomes 10, we would say, "Time's Up, Your Score is (whatever)." There are other reasons  to use loops. If you wanted to scroll a map, you would move the map until you've moved it enough - lets say you wanted to move it 15 pixels - you would loop, in the loop you would move the map by 1 pixel, and you would add a variable by 1 until you get 15. OK, maybe you didn't understand this paragraph. You'll understand it below - where I explain different types of loops.

The While Loop


This is probably the most basic loop. It is simple and easy to use. Lets say you wanted to make a useless program that displayed "I have said this Message (X) times." You would only do this 5 times. The first time, it will say, "I have said this Message 1 times" The second time it will say 2 instead of one. The third time 3 instead of 2, etc.

Well, how would we display a number that changes every time we display it - it adds itself by one. Lets say our variable is called NumberOfTimes. And you say:

MessageBox.Show("I have said this Message NumberOfTimes times") 

Well, the message would appear exactly like this: I have said this Message NumberOfTimes times. This is crazy! You can't see the number in the MessageBox? Well, that's not the way your supposed to do that. Remember that Variables arent supposed to go in Quotes (" ").  Here's what your supposed to do:

MessageBox.Show("I have said this Message" & NumberOfTimes & "Times") 

Look at the & sign (called the Ampersand). The ampersand is there to separate variables from words. They are also a great visual help too, check it out if you don't place the Ampersand:

MessageBox.Show("I have said this Message"  NumberOfTimes "Times") 
'This code has wrong syntax. Do not use this code, it is just an example!

It just wouldn't fit with the rest of it. Well, anyway, we know now how to place a variable in a MessageBox. Now, back to Loops! How would we make this variable add itself on its own? Well, you could add a button and make it increase by one, and have a MessageBox display it. But we want it to do it by itself. How about we do this when the form loads? Here's what we would do: First, declare a variable called  NumberOfTimes in the Declarations area:
Dim NumberOfTimes As Integer 

Then in MyBase. Load type in this:

While NumberOfTimes < 5
MessageBox.Show("I have said this Message" & NumberOfTimes & "Times")
NumberOfTimes = NumberOfTimes + 1
End
While

Basically its simple, the stuff between While and End While repeat while NumberOfTimes is less than 5. Then it displays 5 MessageBoxes saying how many times it repeated itself. The last statement is essential. If we don't increase NumberOfTimes by 1, then we wouldn't get anywhere. Look at it logically, you are saying "While NumberOfTimes is less than 5, repeat the MessageBox over and over." But, how will you get anywhere if you don't add NumberOfTimes by 1? The value of NumberOfTimes would still be 0. And the loop will repeat over and over because NumberOfTimes is still less than 5. This is known as an infinite loop. So, we avoided an infinite loop by adding NumberOfTimes by 1.

Run your program. You will get 5 MessageBoxes! But wait, they don't go from 1 - 5, they go from 0 - 4 (which are still 5 MessageBoxes). Do you know why? Well, look at this again:
 
While NumberOfTimes < 5
LESS than 5, not INCLUDING 5.  Well, there are 2 things which we could do: We could change it to:
While NumberOfTimes < 6
or
While NumberOfTimes <= 5
The 2 ways mentioned above are the same. I would prefer the second way, which is saying, "While NumberOfTimes is less than or equal to 5, do this loop." The first way includes 5, but doesn't include 6.  Choose whichever way you want, and change your code. This time you have 6 MessageBoxes, with NumberOfTimes ranging from 0 - 5! This is a simple problem. Notice how the first MessageBox says, "I have said this message0times." Saying something 0 times doesn't make sense. So go back to your declarations area and change the declaration of NumberOfTimes to this:
Dim NumberOfTimes As Integer = 1

Easy, it sets the default value of NumberOfTimes to 1. Run your program. Now you have 5 MessageBoxes. The first MessageBox says, "I have said this message1times." Wait a minute, all the words are scrunched up! That is another easy problem, the solution including 2 spaces. Go back to your loop and look at the MessageBox Statement:
MessageBox.Show("I have said this Message" & NumberOfTimes & "Times")

Add a space after "Message" another one before "times":

MessageBox.Show("I have said this Message " & NumberOfTimes & " Times")

This is so that the words don't get scrunched up. Run your program, it works perfectly (with the minor exception that it says "1 times" instead of "1 time." Let's not worry about that, it can be fixed with a simple If statement. There is another programming strategy which you should be aware of, look at this statement:

NumberOfTimes = NumberOfTimes + 1

We can shorten it to this:

NumberOfTimes += 1

 Its just a shorthand version of writing the first statement. You can do the same for +, -, *, and /.
That's it! We're done with this program! 

The Source Code for this tutorial is located here:

You can also locate this by logging in to vbProgramming Forums and going to:
Tutorials > Tutorial Source Code > Source Code