Lecture 24: Other languages
In this lecture we will compare some languages. In fact, exist
of different programming languages. It can easily be said that every
has its own language. Many languages are similar to PASCAL and they are
called 'imperative' programming languages. Examples of these are C and
FORTRAN. Other languages use a more functional concept, such as MATLAB.
Some are memory-oriented (like PASCAL), where variables are in normal
Others, like FORTH, are stack-oriented, where variables are stored on a
pile (from which only the top variable is accessible). In this lecture
we will take a look at several languages.
Most languages share the basic concepts we learned in our PASCAL
Other things are more specific to certain languages. These include
like records and pointers.
(In most languages) Declaration of variables
Input and output.
Loops: for, while-do, repeat-until.
they have different names, though.
Decisions: if, if-then, if-the-else, case-of.
Again, sometimes with different names.
(In most languages) Procedures, functions and recursivity.
C is a programming language that is very similar to PASCAL. It has the
advantage that it is more flexible and very efficient with string
For that reason, C is the preferred language for engineers (of
The disadvantage is that C excepts everything and the compiler never
helps you. It is of the type "You wrote it. You got it!". For example,
it is very heavy on pointers, and, as we have seen, working with
has to be done with extreme care.
|a := b;
||a = b;
|if (a=b) then ...
||if (a==b) ...
|while (a<>b) do ...
||while (a!=b) ...
FORTRAN (FORmula TRANslator) is an imperative language, like C and
which has the advantage that it is very strong in doing scientific
Many calculation routines have over the years been optimized in
Becuase of this, most of the scientific calculations are still done in
this language. We have to think about diagonalizing matrices of large
||PRINT 20, "Hello World"
|a := b;
||a = b
|if (a=b) then ...
||IF a .EQ. b THEN
|while (a<>b) do ...
||WHILE (a .NE. b)
BASIC = Beginners All-purpose Symbolic Instruction Code. The advantage
of BASIC is that it is a language that is very accessible to the
programmer. It allows many things and doesn't care so much about
programming. As examples:
The advantages are also the disadvantages: Because the programs are not
compiled they are slow. Moreover, the absence of procedures makes the
rapidly look like spaghetti. Modern versions of BASIC (for example
Basic) adpated the ideas of functions and compilation and this also
BASIC look like any other 4th generation language like the ones
Absence of functions and procedures.
No need to declare variables. Memory is reserved upon the first use of
Normally BASIC is implemented as interpreter. This means that no
stage is needed to run the program.
FORTH (from FOuRTH generation programming language) is a different
in that it is stack-oriented instead of memory oriented. What this
is that everytime a value is generated in an expression, this value is
put on top of a stack. Procedures (like PRINT) can use the top value(s)
of the stack. As an example, imagine we want to calculate 3*4+2. In
this would be done in the following way
|| stack after instruction
||put 3 on the stack
||put 4 on the stack
||multiply top two elements of
stack an put result on th stack
||put 2 on the stack
||add top two elements of
stack an put result on the stack
||print the value on the top of the
stack and remove it from the stack
In fact, some calculators (notably the Hewlett Packards) use this
It is called PostFix notation because the operators are put after
the operands instead of in between. Instead of writing "3 * 4",
we typed "3 4 *".
Other languages, like PostScript (for printers) are very similar to
The advantage is that it is extremely fast. Other languages, more
by humans, are compiled on a lower level to structures similar to
MATLAB, Mathematica, MathCAD, etc.
There are many languages that are specially created to satisfy the
of scientists in various areas. The basic idea behind all those
is that a scientist doesn't want to have the feeling (s)he is
In extreme cases (for example MathCAD), the 'programming' consists of
a text document like Word in which equations are present and the
is executing the equations while we write.
As an example of MathCAD (text written in red
italics is output generated by the computer. The rest is
by the user, exactly how it is shown here)
|This is an example of a MathCAD program.
The Thorian matrix is defined by
To go to the Oswaldian matrix, we have to invert the Thorian
b = a-1
This matrix is therefore
We can also include images or other objects. The advantages are
at the end we have a formatted and printable document with
The disadvantages are the low speed and the fact that it is limited to
only scientific calculations.
MATLAB is a scripting language, which means that we can, from a
line, call certain small programs by typing their names. We can also
new small programs and safe them in files, so that they can also be
from the command line. As an example: The MATLAB equivalent of the
MathCAD program would be
a = [
1 0 1
0 1 1
1 1 0
b = a^-1
(every line NOT finishing with a semicolon (;) generates as output
the value just calculated. In this case the matrix b)
Advantages of MATLAB are
No need to worry about declaration of variables (at the starters
Moreover, note that all variables are of type real.
Availability of many predefined routines useful for engineers and
As an example: finding the minimum value of a function.
Mathematica is somewhere inbetween MATLAB and MathCAD.
The code is not as efficient as other languages
Writing in scripts is rather cumbersome.
Java (and C++, etc.)
Java and C++ are so-called object-oriented languages. What this means
easiest to explain in an example. Remember how in PASCAL we can declare
a variable s to store our name.
Var s: string;
Then we can assign a value to this by
s := 'Peter';
and if we want to show it on the screen we call the PASCAL procedure
WriteLn, with s as a parameter:
We have also learned how to define records that contain information
of mixed type. For instance, a record to store my name and my telephone
type r = record
Object oriented programming means that every variable (or function
of procedure; therefore we use the more general word object) is a
that can have fields that are variables (like tel and s
above) or functions!
We might define an object of type string
Var s: StringObject;
To assign a value (my name) to the variable we would assign a value
to the text field of the record. Something like
s.text := 'Peter';
To print it, we would call the field 'print' of this object. Printing
my name would thus be
The advantage of this is the tremendous flexibility of programming.
The disadvatage is obvious: it is much more complicated to write a
Java has a further advantage that it runs on any computer, independent
of the platform. The same (compiled) program will run on a UNIX
or a Macintosh computer and even on a Windows computer.
Of course, the list of languages doesn't end here. As said before,
there are many many more languages. However, with our knowledge of
we have a good basis for writing programs in any language. The
between the languages, is not so big as you might think. That is why
and Hill in their book "The art of electronics" write about languages
Choose a language and stick with it!
Don't try to convince others your language is the best!
Peter Stallinga. Universidade do Algarve, 21 Maio