Saturday, September 26, 2009

My brain locked up

And all that came out was...WHUT?

It was this:

After the system specification is complete but before the design is coded in the actual programming language, portions of the system may be coded in what is called pseudocode, a design technique that lays out the processing in a simple but standardized language so that the logic can be examined before it is translated into the more complex programming language.

Before delving into pseudocode we must consider how expressions are processed in most computers. Expression processing uses concepts from algebra for computing the results of evaluating expressions. In most computer languages as well as in pseudocode the normal symbols + for add, - for subtract, * for multiply, and / for divide are used. Exponentiation, or raising a number to a power, has various representations in the different languages and for our discussion here we will use the symbol ** which is used in several languages and in pseudocode.

The expression A + B * C means to take the value of A, B, and C and perform the indicated operations. However, the rules of operator precedence apply when there are no parentheses in the expression. In the absence of parentheses the rule of operator precedence from highest precedence to lowest precedence is:
     1) any exponentiation is evaluated first
     2) followed by any multiplication or division in their order of appearance from left to right
     3) followed by any addition or subtraction in their order of appearance

So for our simple example, if A has the value 2, B is 3, and C is 4, the expression value is 3 * 4 + 2 which is 14.

However, if parentheses are added to the expression, expressions in parentheses are evaluated first, starting with innermost parentheses. Within parentheses, the above operator precedence rule still applies. If we rewrite our simple expression to (A + B) * C we now add A and B to get 5; then multiply by 4 to get 20

Continuing to use the same values, A is 2, B is 3, and C is 4:
     1) C – A ** 2 is 0
     2) A + B * C ** 2 is 82
     3) (A + B) * C ** 2 is 80
     4) ((A + B) * C) ** 2 is 400

Pseudocode resembles English and contains the following simple statements which are sufficient to describe any process:

• Sequence – an ordered series of simple actions like computations. The pseudocode code statement is SET.

• Decision branching – tests a relationship in the process (called a condition) and performs one sequence if the relationship is true and a different sequence if the relationship is false. The pseudocode statement is IF (condition) … THEN…ELSE…END IF.

• Repetition – the order to continue to perform a sequence of actions while a condition is true. Skip to the sequence following the repetition when the condition becomes false. The pseudocode statement is WHILE (condition)…END WHILE.

A condition can be a comparison. Possible comparisons are:
     = tests if the two values are the same;
     > tests if the left operand is greater than the right operand;
     <>= tests if the left operand is greater than or equal to the right operand;
     <= tests if the left operand is less than or equal to the right operand. 

A condition can use logical operators. The logical operators are AND, OR and NOT. 
     AND is true if both of the operators are true and false otherwise 
     OR is true if either or both of the operators are true and false otherwise 
     NOT reverses the value of true to false and false to true

Again continuing to use the same values, A is 2, B is 3, and C is 4:
     A = B is false 
     A > B is false
     A < B is true

The SET statement gives a value to a variable. An example of a sequence of pseudocode instructions using only SET instructions: 
     SET A TO 2
gives A the value of 2
     SET B TO 3
     SET C TO 4
     SET C TO A + B
changes C from 4 to 5
     SET A TO B * C
changes A from 2 to 15. Why? The previous SET statement changed the value of C. The statements are an example of a sequence of actions that, if they were written in a programming language, the computer would execute in order.

The IF…THEN…ELSE…END IF statement is a decision branching statement that tests the condition specified in the IF clause. If the condition is true, the sequence of statements in the THEN clause is executed and the sequence of statements in the ELSE clause is skipped. If the condition is false, the sequence of instructions in the THEN clause is skipped and the sequence of statements in the ELSE clause is executed. In either case, after the THEN or ELSE sequence, execution goes to the statement after the END IF. Using the values of A, B, and C from the last example,
     IF (A > B) THEN
     END IF
     SET A TO 0
     SET B TO 0
After this pseudocode is evaluated, what are the values of A, B, and LARGER? A started out as 15 and B was 3. Since 15 > 3 is true LARGER becomes 15, the ELSE clause is skipped and both A and B are set to zero. Which clause would be executed if A and B were equal? The condition (A > B) would be false so the ELSE clause is the one that would be executed.

Repetition is coded as a WHILE loop in pseudocode. The following example showing a WHILE loop in pseudocode instructions is from the CLEP sample test.

     SET A TO 1
     SET B TO 3
     SET A TO A + B
     WHILE A <>

An important point about using a WHILE loop is that some statement within the WHILE and END WHILE pair must change a value in the condition that is being tested to eventually make the condition false, otherwise the loop will run forever. In the above pseudocode A has the value of 4 when the sequence of instructions reaches the WHILE statement for the first time. Since 4 is less than 20, the condition is true and the sequence of statements within the WHILE loop is executed. The value of A changes to 4 * 4 / 2 which is 8. The sequence encounters the END WHILE and repetition causes the execution to return to the WHILE statement. The value of A is still less than 20 so the WHILE sequence is entered once again. This time A is set to 8 * 8 / 2 which is 32. The END WHILE is processed and repetition causes the WHILE to be evaluated again. This time A is greater than 20 and the processing skips to the non-existent sequence after the END WHILE. For this example, the final value of A is 32. 

Note that A, B, and C are just arbitrary names for the values. The name given to a value can be any letter or descriptive name like: 

I think I need help.


Cristina said...

I must confess I only skimmed the last part about the particular example, but because I know about pseudocode and just skimming makes sense to me.
What exactly do you need help with? Everything? Or something in particular/from a particular point onwards? Also, context?
I can try to put that into more sensible words, although a place to start translating would be great! I can see if I can also dig up some links that explain the same in a simpler way.

Michelle said...

Pseudocode just means you'll be writing out the code in english before you start. For example if you are asked to write a program that adds 1 + 2 using the variables a and b it'd look something like:

a is equal to 1
b ie equal to 2

1 + 2 = ?

This method is convenient because it gives you a place to start writing no matter what language you are using. Good luck, you can do it!

Michelle said...

sorry, last line should have said

a + b =

Mia said...


I wouldn't have said "WHUT".. I'd have said "WTF" hehehe.

Better you than me, girl :)

beentsy said...

Ow, ow, ow! I broke my brain.

Wool Enough said...

Okay, this is not so complicated. But it may be the most poorly written "explanation" of pseudocode that I have ever seen. It might help if you cross out everything in between "Before delving into pseudocode . . . " and "Pseudocode resembles English . . . "

Pseudo means "fake," and this stuff is just fake code that any programmer can pretty much understand and translate into the specific language (C++, Fortran, Java, whatever) that s/he is using to produce real code.

Mizzle said...

'WHUT' indeed... since when is pseudocode standardized???

The whole idea of pseudocode is to produce something resembling code during a brainstorm... anything goes, as long as it can be translated more or less directly into code...

Don't let them fool you into thinking pseudocode is complicated - you probably don't even need their explanation, just a bit of common sense. You have that, so you'll be fine. :)

Pat said...

That made my brain explode. I'm glad some of the other commentors seem to understand it.

Sheepish Annie said...

I read two sentences and then my head exploded. Now I have to call in sick to work...

Carolyn (Harbor Hon) said...

I couldn't tell you because my brain fried the moment you went into code. I'm just no good at this, but you are one smart cookie girl. Proud of you. xxoo

Logan said...

Where did you go?