How To Pseudocode (and flowchart) – AS Computing Revision (F452)

Flowchartage (quick dismissal)

  • You can find information on flowcharts in my GCSE computing revision post, here (just scroll down a bit)
  • The only symbols you need are the ellipses for start and end, the rectangles for doing stuff, the parallelograms for input/output, and the diamonds for decisions
  • The rest of this post will be about pseudocode!
Micrasoft Visual Pseudocode 2010 Express Edition

Looks ‘shopped? Yep, this is a joke. You can’t get an IDE that’ll compile and debug pseudocode!

Pseudo-ness

  • Because flowcharts are horrible, pseudocode is often used as an alternative way of writing an algorithm
  • It lets you organise your thoughts on how to tackle the problem in a way that’s like outlining your code – it’s generally more detailed than a flowchart
  • Pseudocode isn’t a real language, which is why you can get away with being incredibly… vague (for want of a better word)
  • For instance, you could write Random = random number from 1 to 10
  • It’s the steps they’re looking for, not the syntax
  • You need to show that you understand how to solve the problem, and can use programming constructs properly
  • Often you’ll be given a procedure you can call to do certain things
  • I’m going to assume you know how to program, but not how to write pseudocode (or that you’re revising how to write pseudocode. What I’m trying to say is I’m not going to explain what an IF statement or a REPEAT loop does in detail)

Data Types

  • STRING
  • INTEGER
  • REAL
  • BOOLEAN
  • CHARACTER
  • (there might be some more I can’t think of – the main point is that double and float are REAL, and there’s only one type of integer)

Arithmetic Operators

  • Addition: +
  • Subtraction: –
  • Multiplication: *
  • Division: /
  • Remainder: MOD
  • Integer Division: DIV

Relational / Comparison Operators (inc. Logical / Boolean Operators)

  • Equal to: ==
  • Not equal to: !=
  • Not equal to: <>
  • Greater than: >
  • Less than: <
  • Greater than or equal to: >=
  • Less than or equal to: <=
  • Both: AND
  • One or both: OR
  • Invert: NOT

Other

  • Assignment operator: =
  • Don’t confuse = with == (they’re different)
  • You can use true and false to check Boolean variables in your conditions (as in IF SomeBooleanVariable == true THEN …etc.)
  • Variables are not loosely typed – you can’t mix different types of variable
  • They don’t seem to have to be declared, although sometimes there’ll be a declaration
  • (If you’re asked to declare something, you can pretty much just make it up – as long as it specifies the data type, identifier, etc…)
  • Sometimes a colon is used to identify the data type of a variable, e.g. SomeVariable : REAL would declare a real (decimal) variable
  • Keywords are in capitals in pseudocode
  • Arrays work as they do in most languages, but often their index starts at 1, rather than 0, and sometimes they use parenthesis ( ) instead of brackets [ ]
  • Multidimensional arrays work like this: identifier(y, x)

String Manipulation

  • There are two functions that look things up in the ASCII character set table for you:

    Ascii full

    Ascii full (Photo credit: Wikipedia)

  • ASCII(character) returns the ASCII value of a character, character
  • CHAR(integer) returns the character of an ASCII value, integer
  • Characters may be in single or double quotes (it’s another thing the examiners don’t seem to have made their minds up about)
  • Strings can be concatenated using the addition operator, +
  • You sometimes have to use concatenation to output something in a friendly way
  • Mathematics can’t be done on strings, but you can compare their ASCII values using the relational operators (<, >, <>, !=, ==, >=, <=)
  • MID(string, integer1, integer2) returns the part of the string between positions integer1 and integer2
  • LEFT(string, integer)
  • RIGHT(string, integer)
  • LENGTH(string) returns the length of the string, string
  • LOCATE(string1, string2) returns the position of the first occurrence of string2 in string1 (0 means it starts at the beginning, -1 means it’s not in there)
  • I don’t know why these functions are acting like there are official pseudocode libraries… they must just be for inspiration, and as a guide

Constructs

IF condition THEN
do something
ELSE
    do something else
END IF

WHILE condition
statements
END WHILE

REPEAT
statements
UNTIL condition

  • The else bit is optional – you don’t have to have it
  • The condition is any expression that evaluates to a Boolean
  • Notice how the statements are indented (well, hopefully they’ll display indented when I publish this…)
  • When using WHILE and REPEAT loops, to count, you need to manually initialize and increment the counting variable
  • The statements inside a loop should cause a change in one of the values in the condition, otherwise you may create an infinite loop

FOR i = 1 to 10
statements
NEXT i
END FOR

  • With a FOR loop, incrementing and initializing of the counting variable are done automatically
  • You can call the counting variable (i, in this case) anything you want, and you can also set the = something TO something values to whatever you want it to count from and to

CASE OF something
something = this: statement
something = that: statement
something = other: statement
default: statement
END CASE

  • In a case statement (that’s switch statement to Java/C# people) you can have however many options you want, but there must always be a default for if none of the options match
  • In the example above, something is the variable that is being checked, and this, that and other are things it’s being compared with

PROCEDURE doSomething(Parameter : DATATYPE, OtherParameter: DATATYPE)
statements
END PROCEDURE

FUNCTION doSomething(parameter: DATATYPE) : RETURNTYPE
statements
RETURN something
END PROCEDURE

  • Procedures and functions don’t have to take parameters, but the parentheses () are necessary
  • Functions must have a return type and must return something (of that type)

File Handling

  • OPEN filename FOR MODE
  • (You can open as READ or WRITE only, one at a time)
  • READ extractedvariable FROM filename
  • WRITE something TO filename
  • CLOSE filename
  • DELETE filename
  • RENAME filename TO something
  • CREATE filename
  • “filename is at end of file” can be used in the condition of loops, to iterate through all the records

High-Level Questions

  • Occasionally, you’ll be asked to write something in a real language
  • I think the course does require you to be taught the basics of a high-level procedural programming language in the first year
  • Again, though, the questions are more about understanding what to do than following the correct syntax
  • Familiarise yourself with how to write the constructs above in a high-level language (i.e. not assembly…)
Advertisements

About Matt

I like writing, filmmaking, programming and gaming, and prefer creating media to consuming it. On the topic of consumption, I'm also a big fan of eating.
This entry was posted in AS Computing Revision, Revision and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Enter comment:

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s