Branching - If...Then...Else...End If
One of the most fundamental features of a computer is its ability to make decisions. By that, we don't mean intelligent decisions, but simple ones, depending on whether something is true of false. This lies at the heart of virtually all programs and it's something we need to look into.
Let's illustrate what we mean by adding some code to the Age program which we've just been looking at.
- edit the Click procedure of the Results button to look like this:
Private Sub cmdResults_Click()
Dim Days As Integer
Dim TextOut As String
LFCR = Chr(10) + Chr(13)
Days = 365 * Age
'build output string
TextOut = "Hello " + UserName
TextOut = TextOut + LFCR
TextOut = TextOut + "You are at least "
TextOut = TextOut + Str(Days)
TextOut = TextOut + " days old!"
TextOut = TextOut + LFCR
If Days > 15000 Then
TextOut = TextOut + "You look not a day older than 10000!"
Else
TextOut = TextOut + "Ah, just a youngster, then!"
End If
lblResults.Caption = TextOut
End Sub
- run the program choosing some suitable numbers to test it
So what the program prints depends on the age of the user...or, at least, on the number that they type in - the program isn't that smart! This is so important, we're going to ask you to look at it again. The general form of this extra bit of code looks something like this:
If (condition) Then
(do something)
Else
(do something else)
End If
...and when it says
If (condition)
it really means "If the condition is True..."
We will also note that the "Else" part is optional, so we could have something like:
If (condition) Then
(do something)
End If
The "End If" part, though, is not optional and must be there, otherwise you will get an error message.
Notice that what follows the "If" statement is indented, that is, set in a little from the left margin. This is good practice and makes your programs easier to read. Anything following the "Else" statement is similarly indented until we reach the "End If" statement. Try to get into the habit of doing this.
We will also take this opportunity to point out that if you ever need to look for help, the Help index that comes with Visual Basic is very comprehensive and also has examples to show how various parts of code are used.
In the program that you've just been looking at, for example, we used an input box. If you look at input boxes in the Help index you'll see that there are ways in which you can control how and where the box appears, what should go in its title bar, and so on. There isn't space to go into that level of detail here and, in any case, it's probably best left for you to explore at your leisure - but be aware that you have that option.
On a similar note, and when you're working through any of these programs, you are encouraged to experiment with different designs for the forms. Ours are just very basic designs with no frills, but there's no reason why you can't set different properties for the various controls - size, position, colour, text, borders and so on.
Don't, however, get carried away down this road and keep a focus on the main ideas that we're trying to get across...
|