(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 |
---|---|
|
|
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 Pass by address Call a function Return |
ComparisonsEqual Not equal Greater Greater or equal Less Less or equal |
Logical opsTRUE / FALSE And Or Not Xor |
Math ops(int) r (int)(r+0.5) i1 % i2 |
String opsString and escaping Base 0 or 1? Concatenate Get Length Index/Find Substring Date and Time Regular Expressions |
Array opsDeclare array Reference an element Resize Dispose Get size |
CastingString to number Number to string Code to char Char to code Typecast |
Console i/oPrint line Print variables Read line Display alert box |
File i/oOpen and Close Read line Read variables Write line Write variables Detect EOF Rewind Seek to offset Seek to line |
UI File opsInput File Chooser Output File Chooser Traverse |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
C sjd 2005-04-23 /*...*/ OR //...\n |
int i; a = b; |
if (cond) stmt; if (cond) { } else {} select { |
while (cond) { } for (init; test; incr) {...} ?? break continue |
int main (argc,argv) {} int argc; ?? |
int f(int a, char b) { } (default) f(&x) a = f(1,'c'); return(1); |
Comparisonsx==y x!=y x>y x>=y x<y x<=y |
Logicnon-0 vs. 0 && || ! |
Math ops(int) r (int)(r+0.5) i1 % i2 |
String opschar s[6] = "he\"llo" 0 s3 = strncat(s1,s2,max) i = strlen(s) char *p = strindex(s,c) strncpy(to,from+start,n) ??? |
Array opsint a[100] a[5] (manual) free(a) (none) |
Castingi = atoi(s); vsprintf(s,"%i",i); c = i; i = c; (newtype) x |
Console i/oputs(s) sprintf("total is %d.\n",i) gets(s) |
File i/of = 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; case...when... |
while cond...end ??? ?? ??? ??? |
?? ARGV ARGV.length() ?? |
def f(a=5, b="default")... end a = f(1,"c"); return(1); |
Comparisonsx==y x!=y x>y x>=y x<y x<=y |
Logic |
Math ops |
String opss = "hello"; 0 s3 = s1 + s2; i = s.length(); n = s.index(c); s.slice(start, end) ??? |
Array ops |
Casting |
Console i/oputs "hello" puts n << " times"; |
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; 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); |
Comparisonsx==y x!=y x>y x>=y x<y x<=y |
Logicnon-0 vs. 0 && || ! |
Math ops |
String opsString s = new String("hello"); 0 s3 = s1 + s2; i = s.length(); i = s.index(c); s2 = s. ??? |
Array opsint x[] = new int[5]; 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) != > >= < <= |
Logictrue/false && || ! ??? |
Math opsMath.min(x1,x2...) ??? ??? ??? Math.round(r) ??? ??? ??? r = Math.random() |
String ops??? ??? s1 + s2 ??? ??? ??? ??? ??? |
Array ops??? ??? ??? ??? ??? |
Castings-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 |
Comparisonsx = 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 opsset 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 |
Castingset i to s as integer ??? ??? ??? (auto) OR set x to s as type |
Console i/o??? ??? ??? display dialog s |
File i/oset 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).