23. Logical Operators

So in order to check if we’ve won, we basically need to look at the following three scenarios:

If any of those green lines are true then we’ve won, if not then we haven’t. Here’s the solution and then I’ll explain what it’s doing:

If ((but2.Text = "X") And (but3.Text = "X")) Then
ElseIf ((but4.Text = "X") And (but7.Text = "X")) Then
ElseIf ((but5.Text = "X") And (but9.Text = "X")) Then
End If

The first thing needing explanation here is the fact we’ve utilised a Logical Operator to let us check multiple things at once. The word “And” let’s us check if two (or more) conditions are true. So in our example we’re checking to see if all the squares in particular rows are equal to “X“, if they are then the player has won. The other type of Logical Operator you might find useful is “Or” which is explained later in this lesson.

The second thing needing explanation is the fact that we’ve used a slightly different version of the “if” “Selection” construct. This time we have used “elseifs” which basically let us choose which section (if any) is correct for us. You will find this type of selection handy in allĀ sortsĀ of situations but hopefully this one is a clear example of where, just one of those statements being true means we’ve won.

So now, how do we keep track of whether they’ve won or not? If you recall back when we encountered variables we discussed their purpose was to remember something, this seems like the perfect opportunity to use variables again. If we create a variable called “xHasWon” for instance, we can simply change it’s value depending on whether they have won or not. And since our answer has only two possible solutions, this seems like another good opportunity to use the “Boolean” data type.

This variable is only needed during this section of code so instead of it being global like our other two variables, we’ll declare this one locally…here’s what your code should look like once all this is taken into account:

Notice that our local variable “xHasWon” is initialised as false and can only change to true if our “if” or”elseif” statements confirm we’ve won, this kind of problem solving is what programming is all about. Study the code above and make sure you understand it because I’m going to get you to complete a lot of the remaining tasks yourself which will use very similar programming techniques!

As an aside, I’ve created an alternative method to checking if we’ve won below:

Notice this method utilises both the “And” and “Or” Logical Operators. This method however makes for a very messy looking “if” statement and so I prefer the previous one but this highlights the fact that there are multiple ways to get a program to work…don’t be afraid to try your own ideas!

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>