24. Arithmetic Operators

So our program is now coming along pretty well, we’ve just checked to see if our placement means we’ve won and we’ve updated a variable called “xHasWon” to show whether that is true or not. Now let’s decide what to do based on the value of that variable:

'IF WE WON
If (xHasWon = True) Then
   'FINISH THE GAME APPROPRIATELY
Else
   'HAVE THE COMPUTER'S TURN
   'ETC...
End If

Notice here that we’ve used yet another variant of the “if” “Selection” construct. This time we’re basically telling the computer to check the initial Boolean Expression and if that turns out to be false then we’ve given a path for the computer to take no matter what. This is yet another handy method of selection that you’ll no doubt find use for!

So what does it mean to “finish the game appropriately“? Well that really is up to you. Imagine you’re actually playing your game of Tic Tac Toe and you’ve just won, what do you think should happen? For now, all I’ll show you is how to increase the player’s score by one, you can decide what else should occur:

If (xHasWon = True) Then
   xWins = xWins + 1
   lblCrossWins.Text = "Number of X wins: " + Trim(Str(xWins))
   ' << SPECIAL CODE, SEEN IN NEXT LESSON >>

Arithmetic Operators
In this snippet of code you can see we’ve altered the value of “xWins” by using the arithmetic operator “plus” ( + ). We used this operator before but it was to “concatenate” or join two strings together. When used with numbers it works exactly as you would expect it to in mathematics. This is true of all the regular mathematical operators, i.e. “minus” ( – ), “multiply” ( * ) and “divide” ( / ). You can also use brackets to create the correct order of operations you require.

IMPORTANT: Always remember the way in which the computer evaluates a statement that has an equals sign in it…work out what’s on the right-hand side first, then put it in the left-hand side!

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>