Compass Rose logo for the Compass DeRose guidesThe Compass DeRose Guide to Programming Languages

(c) 2005 by Steven J. DeRose. All rights reserved.

This page was written by Steven J. DeRose on 2005-04-19, and was last updated on 2005-04-19.

This page gives a very big table comparing the syntax of many programming languages. The bulk of most languages involves the same meaning, with slightly different syntax (ok, well sometimes it's wildly different syntax). So, once you know several languages, you can get going in another very quickly by learning the syntax details for the basic stuff you'll need. You won't get fluent that way, because languages tend to add very cool distinctives; but you'll be able to get work done. This table tries to list those key constructs so you can remind yourself how they're written in whichever language you're using today.

This document is probably best if printed out and hung on the wall as a chart. However, I expect to add some rollover footnotes on various details.



This table only lists basic constructs and usages; many languages have more possibilities. For example, there may be many different types of file I/O (like in PL/I), but this table only lists the most basic sequential character/line-based options.

Many entries are given as example code fragments. Sometimes the fragment does slightly more than other times, if it seemed helpful. The following names are used as conventional examples of particular types, to avoid showing declarations over and over. Any can have a number added when more than one is needed:

Conventional variables Other conventions
i...n
integer
s
string
c
character
b
Boolean
r
Floating point/real
fn
Function
f
File
a
Array or vector
x,y
Any type
\n
required line-break.
stmt
means any statement of the language.
cond
indicates a condition, or generally anything that can be interpreted as a Boolean.
...
at the end of a parameter list indicates the last parameter can be repeated as many times as desired (I'll bet most compilers crash after some fixed limit, though).
N/A
means not applicable
None
means the capability is not provided, and cannot readily be simulated (for example, finding out a string's allocated length in C -- if this had been done right, 95% of OS vulnerabilities to hacking would not have arisen)
Auto
means it just happens when you need it (for example, typecasts in typeless languages
Manual
means you can simulate it (for example, resizing a C array by mallocing a new one, copying the data, and freeing the old one)
OR
identifies alternative constructs
???
means the entry has not been filled in yet.
{ }
means a block of statements surrounded by literal curly-braces (many languages use this notation). Unless noted otherwise, this can be used just like a single statement.

Some features are omitted because they seem unnecesary. For example, there is no indication of the form of variable names allowed. This is because nearly all languages accept names beginning with a Latin letter, and followed by Latin letters and Arabic digits. Maybe underscore, too. Many languages allow a few more characters, but there's no need for them, so it doesn't seem worth including here. Similarly for arithmetic operators.

I would like to add more languages to this table. If you're willing to contribute one, please copy the HTML template here, fill it out, and email it to me (see bottom of page). Among the languages I would most like to add are:

Perl PHP VB Prolog XSLT SVG PostScript Forth Icon TCL Python csh sh tcsh bash

Language

Contributor

Updated

Comments

Declarations

Assignment

If/then

Switch

While

For loop

For Each

Break

Continue

Main

Main args

Halt

Define function

Pass by value(cannot be changed)

Pass by address (can be changed)

Call a function

Return

Comparisons

Equal

Not equal

Greater

Greater or equal

Less

Less or equal

Logical ops

TRUE / FALSE

And

Or

Not

Xor

Math ops

(int) r

(int)(r+0.5)

i1 % i2

String ops

String and escaping

Base 0 or 1?

Concatenate

Get Length

Index/Find

Substring

Date and Time

Regular Expressions

Array ops

Declare array

Reference an element

Resize

Dispose

Get size

Casting

String to number

Number to string

Code to char

Char to code

Typecast

Console i/o

Read line

Display alert box

File i/o

Open and Close

Read line

Read variables

Write line

Write variables

Detect EOF

Rewind

Seek to offset

Seek to line

UI File ops

Input File Chooser

Output File Chooser

Traverse

C

sjd

2005-04-23

/*...*/ OR //...\n

int i;
char c='c';
float f[20];
char *s;

a = b;

if (cond) stmt;

if (cond) { }

else {}

select {
when x: stmt;
break;
otherwise stmt; }

while (cond) { }

for (init; test; incr) {...}

??

break

continue

int main (argc,argv) {}

int argc;
char argv[][];

??

int f(int a, char b) { }

(default)

f(&x)

a = f(1,'c');

return(1);

Comparisons

x==y

x!=y

x>y

x>=y

x<y

x<=y

Logic

non-0 vs. 0

&&

||

!


Math ops




(int) r

(int)(r+0.5)

i1 % i2

String ops

char s[6] = "he\"llo"

0

s3 = strncat(s1,s2,max)

i = strlen(s)

char *p = strindex(s,c)

strncpy(to,from+start,n)

???

Array ops

int a[100]

a[5]

(manual)

free(a)

(none)

Casting

i = atoi(s);
r = atof(s);

vsprintf(s,"%i",i);

c = i;

i = c;

(newtype) x

Console i/o

gets(s)

File i/o

f = fopen(path); fclose(f);

fgets(f);

fscanf("%d",r);

fputs(s)

fsprintf(f,"%i %f7.2",i,r);

fseek(f,0,0);

fseek(f,100,0);

(manual)

UI File ops

??

??

??

Ruby

sjd

???

int i; char c='c'; float f[20]; char *s;

a = b;

if cond then stmt;
if cond \n... else \n... end

case...when...

while cond...end

???

??

???

???

??

ARGV ARGV.length()

??

def f(a=5, b="default")... end

a = f(1,"c");

return(1);

Comparisons

x==y

x!=y

x>y

x>=y

x<y

x<=y

Logic

Math ops

String ops

s = "hello";

0

s3 = s1 + s2;

i = s.length();

n = s.index(c);

s.slice(start, end)
(negatives ok)

???

Array ops

Casting

Console i/o

File i/o

???

UI File ops

??

??

??

LISP

???

N/A

(setq a 4)

(if cond stmt elsestmt)

???

??

???

???

()

??

??

Comparisons

(eq x y)

(ne x y)

(gt x y)

(ge x y)

(lt x y)

(le x y)

Logic

Math ops

String ops

???

Array ops

Casting

Console i/o

File i/o

???

UI File ops

??

??

??

Java

sjd

2005-05-05

/*...*/ OR //...\n

int i; char c='c'; float f[20]; String s;

a = b;

if (cond) stmt;
if (cond) { } else {}

select { when : stmt; otherwise stmt; }

while (cond) { }

for (init; test; incr) {...}

??

???

???

??

int argc; char argv[][];

??

int f(int a, char b) { }

a = f(1,'c');

return(1);

Comparisons

x==y

x!=y

x>y

x>=y

x<y

x<=y

Logic

non-0 vs. 0

&&

||

!

Math ops

String ops

String s = new String("hello");

0

s3 = s1 + s2;

i = s.length();

i = s.index(c);

s2 = s.

???

Array ops

int x[] = new int[5];
ArrayList

x.length()

Casting

Console i/o

File i/o

???

UI File ops

??

??

??

JavaScript™

sjd

2005-04-23

/*...*/ OR //...\n

??

a = b;

if (cond) stmt; else stmt;

switch() { case: ... break; default: ... }

for (init; test; incr) {...}

while(cond) {...} OR do {...} while(cond);

??

???

???

??

N/A

??

function f(a1, a2...) { ... }

???

???

f(x)

return(x)

Comparisons

== (=== also requires same type)

!=

>

>=

<

<=

Logic

true/false

&&

||

!

???

Math ops

Math.min(x1,x2...)

???

???

???

Math.round(r)

???

???

???

r = Math.random()

String ops

???

???

s1 + s2

???

???

???

???

???

Array ops

???

???

???

???

???

Casting

s-0

(auto)

???

???

???

Console i/o

???

alert("hello" + x)

File i/o

???

???

???

???

???

???

???

???

???

UI File ops

??

??

??

AppleScript™

sjd

2005-05-11

--...\n

??

set x to y

if cond then\n stmt\n else\n stmt\n end if

??

repeat with i from j to k by l ... end repeat

repeat while cond... end repeat

repeat with i from j to the number of text items of cSeq... end repeat

???

???

??

??

??

??

???

???

f(x)

return x

Comparisons

x = y OR x equal Y OR equal OR equal to OR is equal to

x not equal to y

x > y OR...

x >= y OR OR...

x < y

x < y OR x less than or equal to y OR...

Logic

??

x and y

x or y

not(b)

???

Math ops

??

???

???

???

???

i mod j

f^g

???

???

String ops

"hello\" there"

counts from 1 OR first, and -1 OR last

s1 & s2

length of s OR number OR count

offset of s1 in s2

text i thru j of s OR words 1 thru 2 OR characters OR lines OR paragraphs

set today to day of (current date)

Array ops

set a to {}

item 2 of the_list end of the_list beginning of the_list a = b & c to concat arrays

(auto)

N/A

length of a

set the_list to find text "-?[0-9]+(\\.[0-9])?" in the_string with string result, regexp and all occurrences

Casting

set i to s as integer

???

???

???

(auto) OR set x to s as type

Console i/o

???

display dialog s

File i/o

set f to s if f does not contain ":" then set f to POSIX file f as Unicode text set fa to alias f set fref to (open for access file f with write permission) close access f

read fref [[char]]

read fref as type

???

write i to fref as type

???

???

???

???

UI File ops

??

??

??



Back to home page of Steve DeRose or The Bible Technologies Group. or The Bible Technologies Group Working Groups. Or, contact me via email (fix the punctuation).