On a previous page, the Boolean datatype was explained. A Boolean variable contains either "True" or "False". The various conditional expressions, like = (Equality) and <> (Inequality) result in a Boolean value that is part of a conditional statement.
The conditional statements that you will see include:
- Branching statements such as If statements
- Looping statements such as For ... Next or While blocks
- Selection blocks using a Select Case statement
You can code If statements on one line:
If Beauty = Truth Then ReadBook(Keats) Else PlayVideoGame()
As a block:
If Beauty = Truth Then
ReadBook(Keats)
Else
PlayVideoGame()
End If
And you can use the ElseIf statement to include other conditions:
If Beauty = Truth Then
ReadBook(Keats)
ElseIf MentalAge = PhysicalAge / 2 Then
PlayVideoGame()
End If
A nested If looks like this:
If Beauty = Truth Then
If War = Peace Then
If Up = Down Then
... other statements
If you have a lot of nested If statements in your program or if the nesting is many levels deep, you might need to rethink your program design. This is often a sign that your program could be coded in a way that is a simpler and easier to understand.
Looping Statements
Code loops when you want to repeat essentially the same code. You might do this to count something. (How many branch offices have transmitted their daily report?) Or you might code a loop to repeat an operation. (Send a reminder to branch offices until all daily reports are received.)
These examples demonstrates the two major types of loops. For ... Next loops repeat a block of code a specific number of times. Do While/Until loops repeat until some variable condition is met.
One other thing to keep in mind is that you can test a condition before or after the statements inside the loop:
Do While Beauty = Truth
... other statements
Loop
Or:
Do
... other statements
Loop While Beauty = Truth
When you choose the second type, the statements inside the loop will always be executed at least one time. An alternative syntax is to code While ... End While.
Select Case
The third way that conditional expressions are used is in Selection blocks using the Select Case statement. We'll see a programming example in the next part to select the right language label in a multi-language "Hello World" program. A Select Case block can be logically the same thing as an If block that includes ElseIf statements, so it's up to you which one you use. In the updated Signature block program, I decided to use Select Case.
Since this is a course in programming as well as Visual Basic Express, the next segment of the tutorial --
-- discusses the craft of programming. One of the mistakes that beginners often make is to assume that the whole job is just writing code. There's a lot more to it than that. We'll get back to the detailed job of programming in Visual Basic after we learn something about what programming is.

