An Introduction to Loops

Number of View: 0

There are three types of loop for us to cover with VB.NET: a For loop, a Do loop, and a While … End While loop. This last one is almost the same as a Do loop, and we won’t be covering it here. But the other two types of loop come in very handy, and a lot of the time you can’t programme effectively without using loops.

 

What is a Loop?

A loop is something that goes round and round and round. If someone told you to move your finger around in a loop, you’d know what to do immediately. In programming, loops go round and round and round, too. In fact, they go round and round until you tell them to stop. You can programme without using loops. But it’s an awful lot easier with them. Consider this.

You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You could do it like this

Dim answer As Integer

answer = 1 + 2 + 3 + 4

MsgBox answer

Fairly simple, you think. And not much code, either. But what if you wanted to add up a thousand numbers? Are you really going to type them all out like that? It’s an awful lot of typing. A loop would make life a lot simpler.

But don’t get hung up too much on the name of the Loop. Just remember what they do: go round and round until you tell them to stop.

We’ll discuss the For Loop first.

VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.8_1114]
Rating: 0 (from 0 votes)

Popularity: unranked

Section Three Exercises

Number of View: 753

Part 1 – If statements

Start a new project. Add a textbox, a Label and a button to your new Form. Then write a programme that does the following:

  1. Asks users to enter a number between 10 and 20.
  2. The number will be entered into the Textbox.
  3. When the Button is clicked, your Visual Basic code will check the number entered in the Textbox.
  4. If it is between 10 and 20, then a message will be displayed.
  5. The message box will display the number from the Textbox.
  6. If the number entered is not between 10 and 20 then the user will be invited to try again, and whatever was entered in the Textbox will be erased

Part 2 – Select Case Statements

Add a Combo box and another button to your form. Create a list of items for your Combo Box. The list of items in your Combo box can be anything you like – pop groups, football teams, favourite foods, anything of your choice. Then try the following:

Use a select case statement to test what a user has chosen from your drop-down list. Give the user a suitable message when the button was clicked.

 

In the next section of this course, we’ll move on to loops in Visual Basic .NET.

VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.8_1114]
Rating: 0 (from 0 votes)

Popularity: 9%

Conditional Operators

Number of View: 1928

The Conditional Operators allow you to refine what you are testing for. Instead of saying “If X is equal to Y”, you can specify whether it’s greater than, less than, and a whole lot more. Examine the list of Operators:

Operator Meaning
> This symbol means Is Greater Than and is used like this:

If number > 10 Then
MsgBox “The Number was Greater Than 10″
End If

 

< This symbol means Is Less Than and is used like this:

If number <10 Then
MsgBox “The Number was Less Than 10″
End If

 

>= These symbols mean Is Greater Than or Equal to, and are used like this:

If number >= 10 Then
MsgBox “The Number was 10 or Greater”
End If

 

<= These symbols mean Is Less Than or Equal to, and are used like this:

If number <= 10 Then
MsgBox “The Number was 10 or Less”
End If

 

And You can combine the logical operators with the word And. Like this:

If number > 5 And number < 15 Then
MsgBox “Greater than 5 And Less than 15″
End If

 

Or You can combine the logical operators with the word Or. Like this:

If number > 5 Or number < 15 Then
MsgBox “Greater than 5 Or Less than 15″
End If

 

<> These symbols mean Is Not Equal to, and are used like this:

If number1 <> number2 Then
MsgBox “number1 is not equal to number2″
End If

 

A word about And and Or. Notice the format with And and Or. The variable is repeated twice

If VariableName = 7 Or VariableName = 10 Then MsgBox “7 or 10 spotted”

If you just put something like this

If VariableName > 7 Or < 10 Then MsgBox “7 or 10 spotted”

then Visual Basic will give you an error message. What you have to say is

If [test the variable for this value] And [test the variable for that value] Then

Your If statement can contain an Else part, too. The format is this:

If [conditional logic here] Then
Some Code here
Else
Some Other Code here
End If

But don’t worry if all that hasn’t sunk in – you’ll get used to the Conditional Operators as you go along. In the next part, there’s two little programmes for you to write. They will test the skills you have acquired so far.

VN:F [1.9.8_1114]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.8_1114]
Rating: 0 (from 0 votes)

Popularity: 68%

« 1 2 3 4 5 6 7 8 9 10 11 12 ... 32 »