Comparison of programming languages (basic instructions)
Appearance
Data types
How to declare variable x as the following types:
Signed integer | Unsigned integer | Long integer | |
---|---|---|---|
C | int x; | unsigned int x; | long x: |
C++ (STL) | unsigned int x; //OR
uint x; //requires STL | ||
C# | uint x; | ||
Java | [1] | ||
Pascal | var x: integer; | var x: word; | var x: longint; |
Visual Basic | Dim x As Integer | [1] | Dim x As Long |
Visual Basic .NET | Dim x As UInteger | ||
Python[2] | x = value | ||
JavaScript[2] | var x = value; or var x = new Number (value); | ||
S-Lang[2] | x = value; | ||
FORTRAN 77 | INTEGER X | [1] | [1] |
Single precision | Double precision | |
---|---|---|
C | float x; | double x; |
C++ (STL) | ||
C# | ||
Java | ||
Pascal | var x: real; | var x: extended; |
Visual Basic | Dim x As Single | Dim x As Double |
Visual Basic .NET | ||
Python[2] | [1] | x = value |
JavaScript[2] | var x = value; or var x = new Number (value); | |
S-Lang[2] | x = valuef; | x = value; |
FORTRAN 77 | REAL X | DOUBLE PRECISION X |
Integer | Floating point | |
---|---|---|
C | [1] | [1] |
C++ (STL) | complex<int> x; | complex<float> x; or complex<double> x; |
C# | [1] | [1] |
Java | ||
Pascal | ||
Visual Basic | [1] | [1] |
Visual Basic .NET | ||
Python[2] | x = re + imj or x = complex(re, im) | |
S-Lang[2] | [1] | x = re + imi; or x = re + imj; |
FORTRAN 77 | COMPLEX X |
Other
Text | Boolean | ||
---|---|---|---|
Character | String | ||
C | char x; | char x[length]; or char *x; |
bool x; |
C++ (STL) | string x; | ||
C# | |||
Java | String x; | boolean x; | |
Pascal | var x: char; | var x: string; | var x: boolean; |
Visual Basic | Dim x As Char | Dim x As String | Dim x As Boolean |
Visual Basic .NET | |||
Python[2] | x = value | x = "string" | x = value |
JavaScript[2] | var x = value | var x = "string"; or var x = new String("value"); |
var x = value; or var x = new Boolean (value); |
S-Lang[2] | x = value; | x = "string"; | x = value;[3] |
FORTRAN 77 | CHARACTER*1 X | CHARACTER*length X | LOGICAL X |
Array types
How to declare x as an array whose components do not have any particular defined values:
one-dimensional array | multi-dimensional array | |
---|---|---|
C | type x[n]; | type x[n1][n2]...;[4] |
C++ (STL) | vector<type> x(n); | |
C# | type[] x = new type[n];[5] | type[,,...] x = new type[n1,n2,...]; |
Java | type[][]... x = new type[n1][n2]...;[5][4] | |
JavaScript | var x = new Array (); or var x = []; |
var x = new Array (new Array (), new Array (), ...); or var x = [[], [], [], ...]; |
Pascal | var x: array[0...n] of type; | var x: array[0...n1, 0...n2] of type; |
Visual Basic | Dim x(n) As type | Dim x(n1,n2,...) As type |
Visual Basic .NET | ||
Python | [1] | [1] |
S-Lang | x = type[n]; | x = type[n1,n2,...]; |
FORTRAN 77 | type X(N) | type X(N1,N2,...) |
PHP | $x = array(elements); |
Statements in square brackets are optional
Conditional statements
if | else if | select case | |
---|---|---|---|
C | if (condition) { instructions } [else { instructions }] | if (condition) { instructions } else if (condition) { instructions } ... [else { instructions }] | switch (variable) { case case1: instructions break; ... [default: instructions;]}[6] |
C++ (STL) | |||
C# | |||
Java | |||
JavaScript | |||
Pascal | if condition then begin \n instructions \n end [\n else begin \n instructions \n end]; | if condition then begin \n instructions \n end \n else if condition then begin \n instructions \n end ... [\n else begin \n instructions \n end]; | case variable of \n meaning: instructions; \n ... [else: instructions \n] end; |
Visual Basic | If condition Then \n instructions \n [Else instructions \n] End If | If condition Then \n instructions \n ElseIf condition Then \n instructions \n ... [Else instructions \n] End If | Select Case variable \n Case case1 \n instructions \n ... [Case Else \n instructions \n] End Select |
Visual Basic .NET | |||
Python | if condition : \n \t instructions \n [else: \n \t instructions \n] | if condition : \n \t instructions \n elif: \n \t instructions \n ... [else: \n \t instructions \n] | [1] |
S-Lang | if (condition) { instructions } [else { instructions }] | if (condition) { instructions } else if (condition) { instructions } ... [else { instructions }] | switch (variable) { case case1: instructions } { case case2: instructions } ... |
FORTRAN 77 | IF (condition) THEN \n instructions \n [ELSE \n instructions \n] ENDIF | IF (condition) THEN \n instructions \n ELSEIF (condition) THEN \n instructions \n ... [ELSE \n instructions \n] ENDIF | [1] |
while | do while | for i = 1 to N | foreach | |
---|---|---|---|---|
C | while (condition) { instructions } | do { instructions } while (condition) | for ([type] i = 0; i<N; i++) { instructions } | [1] |
C++ (STL) | ||||
C# | foreach (type item in set) { instructions } | |||
Java | for (type item : set) { instructions } | |||
JavaScript | for (i = 0; i<N; i++) { instructions } | for (property in object) { object [property] } | ||
Pascal | while condition do begin \n instructions \n end | repeat \n instructions \n until condition;[7] | for i := 1 [step 1] to N do begin \n instructions \n end;[8] | |
Visual Basic | Do While condition \n instructions \n Loop[9] | Do \n instructions \n Loop While condition[9] | For i = 1 To N [Step 1] \n instructions \n Next i[8] | [1] |
Visual Basic .NET | For i[ As type] = 1 To N [Step 1] \n instructions \n Next i[8] | For Each item As type In set \n instructions \n Next item | ||
Python | while condition : \n \t instructions \n [else: \n \t instructions \n] | [1] | for i in range(0, N): \n \t instructions \n [else: \n \t instructions \n] | for item in set: \n \t instructions \n [else: \n \t instructions \n] |
S-Lang | while (condition) { instructions } [then optional-block] | do { instructions } while (condition) [then optional-block] | for (i = 0; i<N; i++) { instructions } [then optional-block] | foreach item (set) [using (what)] { instructions } [then optional-block] |
FORTRAN 77 | [1] | [1] | DO nnnn I = 1,N \n instructions \n nnnn CONTINUE | [1] |
raise | handler | |
---|---|---|
C | [1] | [1] |
C++ (STL) | throw exception; | try { instructions } catch [(exception)] { instructions } ... |
C# | try { instructions } catch [(exception)] { instructions } ... [finally { instructions }] | |
Java | ||
JavaScript | try { instructions } catch (exception) { instructions } [finally { instructions }] | |
Pascal | [1] | [1] |
Visual Basic | [1] | [1] |
Visual Basic .NET | Throw exception | Try \n instructions \n Catch [exception] [When condition] \n instructions \n ... [Finally \n instructions \n] End Try |
Python | raise exception | try: \n \t instructions \n except[ exception]: \n \t instructions \n ... [else \n \t instructions \n] [finally: \n \t instructions \n] |
S-Lang | throw exception; | try { instructions } catch [exception] ... [finally { instructions }] |
FORTRAN 77 | [1] | [1] |
Function declaration
Statements in square brackets are optional
basic | value-returning function | main function | |
---|---|---|---|
C | void foo([parameters]) { instructions } | type foo([parameters]) { instructions } | int main([void]) { } or int main(int argc, char *argv[]) { instructions } |
C++ (STL) | |||
C# | static void Main([string[] args]) { instructions } or static int Main([string[] args]) { instructions } | ||
Java | public static void main(String[] args) { instructions } or public static void main(String... args) { instructions } | ||
JavaScript | function foo([parameters]) {instructions } | [1] | |
Pascal | procedure foo([parameters]) \n begin instructions end; | function foo([parameters]):type \n begin instructions end; | [1] |
Visual Basic | Sub Foo([parameters]) \n instructions \n End Sub | Function Foo([parameters]) As type \n instructions \n End Function | [1] |
Visual Basic .NET | Sub Main([ByVal CmdArgs() As String]) \n instructions \n End Sub or Function Main([ByVal CmdArgs() As String]) As Integer \n instructions \n End Function | ||
Python | def foo([parameters]): \n \t instructions \n | def foo([parameters]): \n \t instructions \n return value \n | if __name__ == '__main__': \n \t instructions \n |
S-Lang | define foo ([parameters ; qualifiers]) { instructions } | define foo ([parameters ; qualifiers]) { instructions ... return value; } | public define slsh_main () { instructions } |
FORTRAN 77 | SUBROUTINE FOO[(parameters)] \n instructions \n END | type FUNCTION FOO[(parameters)] \n instructions \n FOO = value \n END | PROGRAM main |
string to integer | string to long integer | string to floating point | integer to string | floating point to string | |
---|---|---|---|---|---|
C | integer = atoi(string); | long = atol(string) | float = atof(string); | sprintf(string, "%i", integer); | sprintf(string, "%f", float); |
C++ (STL) | stringstream(string) >> integer; | stringstream(string) >> long; | stringstream(string) >> float; | stringstream(string) << integer; | stringstream(string) << float; |
C# | integer = int.Parse(string); | long = long.Parse(string); | float = float.Parse(string); or double = double.Parse(string); |
string = integer.ToString(); | string = float.ToString(); |
Java | integer = Integer.parseInt(string); | long = Long.parseLong(string); | float = Float.parseFloat(string); or double = Double.parseDouble(string); |
string = integer; | string = float; |
JavaScript | integer = parseInt (string); or integer = new Number (string) or integer = string*1; |
long = new Number (string) or long = string*1; |
float = parseFloat(string); or float = new Number (string) or float = string*1; |
string = integer.toString (); or string = new String (integer); or string = integer+""; |
string = float.toString (); or string = new String (float); or string = float+""; |
Pascal | integer := StrToInt(string); | float := StrToFloat(string); | string := IntToStr(integer); | string := FloatToStr(float); | |
Visual Basic | integer = CInt(string) | long = CLng(string) | float = CSng(string) or double = CDbl(string) |
string = CStr(integer) | string = CStr(float) |
Visual Basic .NET | |||||
Python | integer = int(string) | float = float(string) | string = str(integer) | string = str(float) | |
S-Lang | integer = atoi(string); | long = atol(string); | float = atof(string); | string = string(integer); | string = string(float); |
FORTRAN 77 | READ(string,format) integer | [1] | READ(string,format) float | WRITE(string,format) integer | WRITE(string,format) float |
Standard Input and Standard Output
read from | write to | ||
---|---|---|---|
stdin | stdout | stderr | |
C | scanf( format, &x ); or fscanf(stdin, format, &x ); [10] |
printf( format, x ); or fprintf(stdout, format, x ); [11] |
fprintf(stderr, format, x );[12] |
C++ | cin >> x; | cout << x; | cerr << x; clog << x; |
C# | x = Console.Read(); or x = Console.ReadLine(); |
Console.Write(x); or Console.WriteLine(x); |
Console.Error.Write(x); or Console.Error.WriteLine(x); |
Java | x = System.in.read(); | System.out.print(x); or System.out.printf(format, x); or System.out.println(x); |
System.err.print(x); or System.err.printf(format, x); or System.err.println(x); |
Pascal | read(x); or readln(x); |
write(x); or writeln(x); |
[1] |
Visual Basic | Input x | Print x or ? x |
[1] |
Visual Basic .NET | x = Console.Read() or x = Console.ReadLine() |
Console.Write(x) or Console.WriteLine(x) |
Console.Error.Write(x) or Console.Error.WriteLine(x) |
Python | For numbers: x = input() For strings: x = raw_input() |
print x or sys.stdout.write(x) |
sys.stderr.write(x) |
S-Lang | fgets (&x, stdin) | fputs (x, stdout) | fputs (x, stderr) |
FORTRAN 77 | READ(5,format) variable names | WRITE(6,format) expressions | [1] |
Reading command-line arguments
Argument values | Argument counts | |
---|---|---|
C | argv[n] | argc |
C++ | ||
C# | args[n] | args.Length |
Java | args.length | |
Pascal | ParamStr(n) | ParamCount |
Visual Basic | [1] | [1] |
Visual Basic .NET | CmdArgs(n) | CmdArgs.Length |
Python | sys.argv | len(sys.argv) |
S-Lang | __argv | __argc |
FORTRAN 77 | [1] | [1] |
Notes
- ^ a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an ao This language does not support this feature.
- ^ a b c d e f g h i j k This language uses dynamic typing
- ^ This language represents boolean true as a non-zero valued integer, and boolean false as an integer whose value is zero.
- ^ a b Multidimensional arrays in C and similar languages are actually arrays of arrays
- ^ a b The C-like "type x[]" works in Java, however "type[] x" is the preferred form of array declaration.
- ^ C#'s switch statement requires the use of goto case n to achieve fall-through
- ^ The instrutions are repeated while the condition is false
- ^ a b c "Step n" is used to change the loop interval. If "Step" is omitted, then the loop interval is 1.
- ^ a b "Until" can be used instead of "While" to make the loop run while the condition is false
- ^
gets(x)
andfgets( x, length, stdin )
read unformatted text from stdin. Use ofgets
is not recommended - ^
puts(x)
andfputs( x, stdout )
write unformatted text to stdout - ^
fputs( x, stderr )
writes unformatted text to stderr