Pseudo code | |

Pseudo code is used for many purposes. It is a favourite way of showing how algorithms work in text books and on the web. It is also useful for software developers to think out algorithms. Normal programming languages force the programmer to think about the details of code. For example how you might get user input if the algorithm requires it. Or how will the result be displayed? These questions are not usually important when developing an algorithm. For example consider the following algorithm.
min(x,y) IF x <= y THEN RETURN x ELSE RETURN y
Does it matter where the x and y come from? Does it matter that we are assuming that x and y are both numbers? What do we do with the answer? All these questions are not important when deciding how to work out the minimum of two numbers. We are taking the pointless detail and concentrating on the real issues of the algorithm. Lets look at the algorithm to convert Fahrenheit to Celsius. The formula is 5 /9 ( F - 32) Where F is the temperature in Fahrenheit. The pseudo code will be –
Convert(F) RETURN (5/9) * (F - 32)
Or if we want to display the result at the end
Convert(F) PRINT (5/9) * (F - 32)
PRINT will display the result on the screen. Will this be in a text box, a dialog or will it be just magically appear somewhere on the screen at random? Does it matter? Not really. This detail takes nothing away from the expressiveness of the pseudo code but it does allow us to develop algorithms without having to worry about every detail.
There are no hard and fast rules when it comes to pseudo code. As long as the code makes sense to others then it is valid. Code written for the exam will always be in pseudo code so it is important to get used to reading code in this way. |