Back

Common operations on numeric and Boolean data

Introduction
We often have to perform common operations on both numeric and Boolean data. Over time and with experience, this becomes easier. The more actual examples we try out, the more we can understand what is happening and how to do something. The trick is to build up lots of examples we can refer back to and build up experience.

Common operations on numeric data
We often have to add, subtract, multiply and divide numbers in programming. This can be easily achieved in our programs. Let us declare two variables of data type Integer and one of type Real:

int firstnumber
int secondnumber
real result

We can add, subtract, multiply and divide numbers in the following way:

result = firstnumber + secondnumber
result = firstnumber - secondnumber
result = firstnumber * secondnumber
result = firstnumber / secondnumber

Result had to be declared as a Real number because when we divide one number by another number, there is a good chance that the result will have a fractional part.

Common operations on Boolean data
Boolean variables are really useful in programming because they give you a way of doing tests. Depending on the outcome of the test, you can then do or not do some action. For example, let us first declare a Boolean variable in pseudo-code with an identifier 'wonthelottery' like this:

wonthelottery : Boolean

Once it has been declared, we can then set its value to true or false, like this:

wonthelottery = true or
wonthelottery = false

Now we can use the Boolean value to do a test:

IF (wonthelottery = true) THEN
     PRINT "Congratulations"
ELSE
     PRINT "Better luck next time"
ENDIF

We look inside the brackets of (wonthelottery = true) and ask ourselves if this is a true statement. if it is, then we would print 'Congratulations'. If it is not true i.e. false, then print 'Better luck next time'.

We can build up quite complex tests using Boolean variables. Let's declare another variable:

wontheluckynumber : Boolean

We could now test to see if we have either one the lottery or the lucky number draw by using OR:

IF (wonthelottery = true) OR (wontheluckynumber = true) THEN
     PRINT "Congratulations"
ELSE
     PRINT "Better luck next time"
ENDIF

As long as any one of the statements in brackets are true (or both of them are true, then 'Congratulations' will be printed, else 'Better luck next time' will be printed.

Let's declare yet another variable:

sixteenorover : Boolean

We can now use this to check whether the winner is sixteen or over.

IF ((wonthelottery = true) OR (wontheluckynumber = true)) AND (sixteenorover = true) THEN
     PRINT "Congratulations"
ELSE
     PRINT "Better luck next time"
ENDIF

This time, you print 'Congratulations if either wonthelottery OR wontheluckynumber is true, AND the winner is sixteen or over. Notice the extra brackets around ((wonthelottery = true) OR (wontheluckynumber = true)). This makes sure that the whole statement ((wonthelottery = true) OR (wontheluckynumber = true)) is evaluated first to be true or false, before ANDing it to (sixteenorover = true) 

Back