Pseudo Code Examples
Formal pseudo code is used to clearly pin down the work that needs to be done when programming. Although some use an informal "list of steps", it is often useful to use formal pseudo code for more complex algorithms, since it is "sort of code" without the riggers of formal programming languages.The following is not complete and is not meant to be complete. It is simply the forms of pseudo code that would be used in a introduction to programming. It contains the sequential structure, the decision structure and the iteration or loop structure.
To begin the program code
START program name
To end the program code
The capitalized START and STOP are pseudo code words. They are in all capitals to draw attention to them. In this example the pseudo code words are also bold to draw even more attention. In practice bold letters are not required but I like them :).
The sequential structure is simply the statement that is required to explain the step. For instance to express a formula code
GrossPay = TotalHours * HourlyRate
In the example above I chose to capitalize the first letter of each word and run the words together to form "data elements" that might have been used in an IPO.
To ask the user of the program to enter the TotalHours data element in the example above two pseudo code words are used. It is known as a prompt.
OUTPUT Please enter the total hours for this employee. INPUT TotalHours
Notice how the pseudo code words pop out? Also notice that the data element name is used in the INPUT statement. The OUTPUT tells the user what is to be input in the next line. Was it total hours or perhaps overtime hours. The user needs to know what the program needs for each input
When decisions are required the IF statement is used. The first one shown is known as a NULL if. In the NULL if there is no "false" path
IF TotalHours > 40 THEN OUTPUT "You have exceeded the regual time. You are not authorized to work overtime" ENDIF
In this next example the if is more complex. It is known as an IFTHENELSE. It is used to create two paths for the program. One path is for the condition when it is true the other when it is not true (it is false)
IF TotalHours > 40 THEN OTHours = TotalHours - 40 RegHours = 40 ELSE RegHours = TotalHours OTHours = 0 ENDIF GrossPay = (RegHours * RegRate)+(OTHours * RegRate * 1.5) DISPLAY "Total Gross Pay is ",GrossPay
Many true and many false statements can be placed in each part of the IF statement. True statements, that is statements that need to be executed if the IF is true go between the THEN and the ELSE. Those that are executed when the IF is false are placed between the ELSE and the ENDIF. If your IF structure does not require an ELSE the ENDIF is still required after all true statements.
The third structure is the iteration or loop structure. Lets code a small multiplication table
OUTPUT "Enter a number for the Table -> " INPUT TheNumber tableSize = 10 tableCount = 1 DOWHILE tableCount < tableSize tableAnswer = TheNumber * tableCount OUTPUT TheNumber " times ", tableCount, " equals ", tableAnswer ENDDO
The output from the previous code would look something like this:
Enter a number for the Table -> 2
2 times 1 equals 2 2 times 2 equals 4 2 times 3 equals 6 2 times 4 equals 8 2 times 5 equals 10 2 times 6 equals 12 2 times 7 equals 14 2 times 8 equals 16 2 times 9 equals 18 2 times 10 equals 20
In addition you can create modules. An example of using modules follows.
START (this is the main program)
main program code starts here now you need to call a module to do some code
CALL MyModule(parm1, parm2) parm1 one now has a value that was calculated in MyModule
STOP (end of the main program)
MyModule (arg1, arg2) put some code here arg1 = some equation here to return a value to the main program RETURN