In this section, we discussed the fact that code is executed in sequence. One line is completed before the computer moves on to the next. This is generally true, however, not all problems follow a linear path of execution. Our Tic Tac Toe game is the perfect example, in our commented section from the previous page you probably noticed the word “if” used on a few occasions, and in our Flow Chart we had orange diamonds representing decisions the computer is going to have to make. This is what’s referred to as “Selection“.
“Selection” involves asking the computer a question, that question is what is known as a Boolean Expression. The reason why it contains the word “Boolean” is because the computer can only answer a question if it has a “True” or “False” answer and so decisions will only be made if the question produces a Boolean response, or what is known as a Boolean Expression.
In our example from the previous lesson, our first comment outlines a question that we need to convert into a Boolean Expression:
'IF THE PLAY AGAIN BUTTON IS DISABLED
So how do we know whether the Play Again button is disabled? We check the “Enabled” property of the “butAgain” object, i.e:
If (butAgain.Enabled = False) Then
So now we have converted a plain English comment into actual Visual Basic code. The beauty of an “if” statement, is that the computer will only execute code within it, if the Boolean Expression is evaluated to be true. Here’s what the whole “if” statement would look like (minus the line numbers of course):
1. If (butAgain.Enabled = False) Then 2. butAgain.Enabled = True 3. End If 4. . . .
Understanding how the computer works through code like this is crucial in becoming a confident programmer so we’ll look at it line by line:
When the computer encounters a line that contains an “if” statement, it immediately tries to interpret the Boolean Expression. In our case it looks at the “Enabled” property of the “butAgain” object and checks if it is equal to “False“. If the answer to that question is true, the computer will move on to line #2, if the answer to that question is false then it will skip every line it encounters until it finds the matching “End If” statement where it will resume regular sequential code execution. Understanding this construct is crucial, make sure this makes sense!!!
This line and any other lines you might have before the “End If” line are only executed if the Boolean Expression of the selection statement is evaluated to be true. If the Boolean Expression should turn out to be false, then this section is skipped!
In our case, we only have one line inside this “if” statement but you can have as many as you like. You could have other “if” statements in there and that code would only be executed if the original Boolean Expression from Line #1 is evaluated to be true.
This is where the computer will continue on after the “Selection” statement has completed. If the logic behind this type of statement is unclear then I suggest going back over the flowchart in the previous lesson to try and think through the logic of this problem.
There are other ways to select a path that the computer will follow but the “if” statement is the most popular. It contains variations that we will encounter later on as well.
For now, add the if statement outlined above into your “but1 Click Event”, it is the correct code needed in place of the first two comments, i.e:
'IF THE PLAY AGAIN BUTTON IS DISABLED ' ENABLE THE PLAY AGAIN BUTTON
So the start of your code should look something like this:
'IF THE PLAY AGAIN BUTTON IS DISABLED If (butAgain.Enabled = False) Then 'ENABLE THE PLAY AGAIN BUTTON butAgain.Enabled = True End If
So the process we follow involves filling in the correct code needed to perform the tasks our comments describe. Another good habit to get into is to test your program regularly, even though this code doesn’t do much you should now be able to have the Play Again button enable itself when you click on the “but1” Button. So before we move on, make sure your code runs without error!