Skip to content

Basic Programming Structures and Pseudocode

Marcelo Barsotti edited this page Dec 29, 2018 · 7 revisions

There are many types of algorithms, and many ways to develop and describe them. We will stick to the basics here so we will be using pseudocode, which is a high level description of a problem solution or algorithm, in language that is close to English. For general purpose algorithm description, it is common to use Structured English, but the pseudocode style can be adapted to get close to the target computer language that will be used. The big difference is that a program is intended to be read by a computer, while a pseudocode is intended to be read and understood by human beings.

While you gain experience, and for bigger or complex problems, it is very common to write down a pseudocode version of the algorithm before trying to write it down directly in a computer language. It helps to keep your ideas clear and break down the problem in smaller and simpler chunks, without having to deal with details and particularities of a specific formal computer language. Some people prefer using diagrams, like flowcharts, but for the description of algorithms, and for the purpose of this course, we will be using Structured English, because I can write it directly in my IDE as comments, and when I am satisfied with the overall solution, I can include the actual code by replacing or adding to the comments I have already written.

There are three categories of Structured English elements:

  1. Operation statements are written as English imperative phrases and executed from top down:

print name
set balance to 0
read email address

  1. Conditional blocks, indicated by keywords like "if", "then", "else", "end if", "switch", "case", etc.:
if balance < -1000  
then  
   block customer checking account
else 
   execute checking account transaction
end if
  1. Repetition blocks, indicated by keywords like "while", "do", "until", "for", "for each", etc.:
for each customer
   print customer checking account statement
end for