Logical reasoningLogical reasoning in practice

There is almost always more than one solution to a problem. Logical reasoning is used to predict the outcomes of the algorithms that are designed to solve a problem, to help select the best solution.

Part ofComputer ScienceAlgorithms

Logical reasoning in practice

Example: Are you old enough to drive?

Look at this simple for working out if you are old enough to drive a car:

OUTPUT 'How old are you?'
INPUT user inputs their age
STORE the user's input in the age variable
IF age > 17 THEN OUTPUT 'You are old enough to drive a car!'
ELSE IF age < 17 THEN OUTPUT 'You are too young to drive a car.'

This algorithm has a fault. can be used to determine what the fault is.

There are two in this algorithm. The first checks if age is greater than 17. The second checks if age is less than 17.

We can use logical reasoning to predict the outcomes of this algorithm. Logically, it is clear that a person’s age could be greater than 17, less than 17, or could actually be 17. From this, it is possible to use these values in the algorithm to make predictions:

  • A rule exists to check if age is greater than 17. If age is greater than 17, the algorithm will output ‘You are old enough to drive a car!’
  • A rule exists to check if age is less than 17. If age is less than 17, the algorithm will output ‘You are too young to drive a car.’
  • No rule exists to check if age is equal to 17. If age is equal to 17, there will be no output.

Logic tells us that either a rule needs to be added, or the existing rules need to be changed. The algorithm can be fixed by either:

  • adding a new rule – ELSE IF age = 17 OUTPUT ‘You are old enough to drive a car!’
  • or amending the first rule – IF age >= 17 OUTPUT ‘You are old enough to drive a car!’

Either solution would work.