Jump to content

Comparison of programming languages (basic instructions)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 62.101.126.214 (talk) at 14:06, 1 April 2008 (put a link to ''reflection (computer science)'', may be useful.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Variable declaration

How to declare variable x as the following types:

Real numbers

Integer Floating point
Signed integer Unsigned integer Long integer Single precision Double precision
C (C99) int x; unsigned int x; long x: float x; double x;
C++ (STL) unsigned int x; or
uint x;
C# uint x;
Java [1]
Pascal var x: integer; var x: word; var x: longint; var x: real; var x: extended;
Visual Basic Dim x As Integer [1] Dim x As Long Dim x As Single Dim x As Double
Visual Basic .NET Dim x As UInteger
Python x = value [2] [1] x = value [2]
JavaScript var x = value; or
var x = new Number (value); [2]
S-Lang x = value; [2] x = valuef; x = value; [2]
FORTRAN 77 INTEGER X [1] [1] REAL X DOUBLE PRECISION X
PHP $x = value; [2]
Windows PowerShell $x = value; or
New-Variable -Name x -Value value -Description description

Complex numbers

Integer Floating point
C (C99) [1] [1]
C++ (STL) [std::]complex<int> x; [std::]complex<float> x; or
[std::]complex<double> x;
C# [1] [1]
Java
JavaScript [1]
Pascal
Visual Basic [1] [1]
Visual Basic .NET
Python x = re + imj or
x = complex(re, im)
S-Lang [1] x = re + imi; or
x = re + imj;
FORTRAN 77 COMPLEX X
Windows PowerShell [1] [1]

Other variable types

Text Boolean
Character String
C (C99) char x; char x[length]; or
char *x;
bool x;
C++ (STL) [std::]string x;
C# string x;
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 x = value [2] x = "string" [2] x = value [2]
JavaScript var x = value [2] var x = "string"; or
var x = new String("value"); [2]
var x = value; or
var x = new Boolean (value); [2]
S-Lang x = value; [2] x = "string"; [2] x = value; [3]
FORTRAN 77 CHARACTER*1 X CHARACTER*length X LOGICAL X
PHP $x = value; [2] $x = "string"; or $x = 'string'; [2] $x = value; [2]
Windows PowerShell $x = value; $x = "string"; or $x = 'string'; $x = value;

Array declaration

How to declare x as an array whose components do not have any particular defined values:

one-dimensional array multi-dimensional array
C (C99) type x[n]; type x[n1][n2]...;[4]
C++ (STL) type x[n]; or
[std::]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 = new Array (n); or
var x = new Array (elements); or

var x = []; or
var x = [elements];
var x = new Array (new Array (), new Array (), ...); or
var x = new Array (new Array (n1), new Array (n2), ...); or
var x = new Array (new Array (elements1), new Array (elements2), ...); or

var x = [[], [], [], ...]; or
var x = [[elements1], [elements2], [elements3], ...];
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 x = [] [1]
S-Lang x = type[n]; x = type[n1,n2,...];
FORTRAN 77 type X(N) type X(N1,N2,...)
PHP $x = array(elements);
Windows PowerShell $x = @() $x = New-Object 'object[,,...]' n1,n2,...; or
$x = @(@(), @(), @(), ...);

Control flow

Statements in square brackets are optional

Conditional statements

if else if select case
C (C99) 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
PHP
Windows PowerShell if (condition) { instructions } elseif (condition) { instructions } ... [else { instructions }] switch (variable) { case1 { instructions } ... [default { instructions }] }
Pascal if condition then begin
instructions
end [
else begin
instructions
end];
if condition then begin
instructions
end
else if condition then begin
instructions
end
...
[
else begin
instructions
end];
case variable of
case1: instructions;
... [else: instructions ]
end;
Visual Basic If condition Then
instructions
[Else
instructions ]
End If
If condition Then
instructions
ElseIf condition Then
instructions
...
[Else
instructions ]
End If
Select Case variable
Case case1
instructions
... [Case Else
instructions ]
End Select
Visual Basic .NET
Python if condition :
\t instructions
[else:
\t instructions
]
if condition :
\t instructions
elif:
\t instructions
...
[else:
\t instructions
]
[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
instructions
[ELSE
instructions
] ENDIF
IF (condition) THEN
instructions
ELSEIF (condition) THEN
instructions
...
[ELSE
instructions
] ENDIF
[1]

Loop statements

while do while for i = 1 to N foreach
C (C99) 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] }
PHP for ($i = 0; $i < N; $i++) { instructions } foreach (set as item) { instructions } or
foreach (set as key => item) { instructions }
Windows PowerShell for ($i = 0; $i -lt N; $i++) { instructions } foreach (item in set) { instructions }
Pascal while condition do begin
instructions
end
repeat
instructions
until condition;[7]
for i := 1 [step 1] to N do begin
instructions
end;[8]
Visual Basic Do While condition
instructions
Loop[9]
Do
instructions
Loop While condition[9]
For i = 1 To N [Step 1]
instructions
Next i[8]
[1]
Visual Basic .NET For i[ As type] = 1 To N [Step 1]
instructions
Next i[8]
For Each item As type In set
instructions
Next item
Python while condition :
\t instructions
[else:
\t instructions
]
[1] for i in range(0, N):
\t instructions
[else:
\t instructions
]
for item in set:
\t instructions
[else:
\t instructions
]
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
instructions
nnnn CONTINUE
[1]

Exceptions

throw handler
C (C99) [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 }]
S-Lang try { instructions } catch [exception] ... [finally { instructions }]
Windows PowerShell trap { instructions }
Pascal [1] [1]
Visual Basic [1] [1]
Visual Basic .NET Throw exception Try
instructions
Catch [exception] [When condition]
instructions
...
[Finally
instructions
] End Try
Python raise exception try:
\t instructions
except[ exception]:
\t instructions
...
[else
\t instructions
]
[finally:
\t instructions
]
FORTRAN 77 [1] [1]

Other control flow statements

exit block(break) continue branch (goto)
C (C99) break[ label]; continue; goto label;
C++ (STL)
C#
Java [1]
JavaScript break;
PHP
Pascal
Visual Basic Exit block Continue block GoTo label
Visual Basic .NET
Python break continue [1]
S-Lang break[ label]; continue;
FORTRAN 77 EXIT CYCLE GOTO label
Windows PowerShell

Function declaration

Statements in square brackets are optional. See reflection for calling and declaring functions by strings.

basic value-returning function main function
C (C99) void foo([parameters]) { instructions } type foo([parameters]) { instructions } int main([void]) { instructions } 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 }; or
var foo = function ([parameters]) {instructions }; or
var foo = new Function (["parameters1", "parameters2", ...,] "instructions");
[1]
Pascal procedure foo([parameters])
begin instructions end;
function foo([parameters]):type
begin instructions end;
[1]
Visual Basic Sub Foo([parameters])
instructions
End Sub
Function Foo([parameters]) As type
instructions
End Function
[1]
Visual Basic .NET Sub Main([ByVal CmdArgs() As String])
instructions
End Sub or Function Main([ByVal CmdArgs() As String]) As Integer
instructions
End Function
Python def foo([parameters]):
\t instructions
def foo([parameters]):
\t instructions
return value
if __name__ == '__main__':
\t instructions
S-Lang define foo ([parameters] [;qualifiers]) { instructions } define foo ([parameters] [;qualifiers]) { instructions ... return value; } public define slsh_main () { instructions }
FORTRAN 77 SUBROUTINE FOO[(parameters)]
instructions
END
type FUNCTION FOO[(parameters)]
instructions
FOO = value
END
PROGRAM main
PHP function foo([parameters]) { instructions } [1]
Windows PowerShell function foo([parameters]) { instructions } [1]

Type conversions

string to integer string to long integer string to floating point integer to string floating point to string
C (C99) integer = atoi(string); long = atol(string) float = atof(string); sprintf(string, "%i", integer); sprintf(string, "%f", float);
C++ (STL) [std::]stringstream(string) >> integer; [std::]stringstream(string) >> long; [std::]stringstream(string) >> float; [std::]stringstream(string) << integer; [std::]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;
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) long = long(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
PHP string = sprintf("%i", integer); string = sprintf("%f", float);
Windows PowerShell integer = [int]string long = [long]string float = [float]string string = [string]integer string = [string]float

Standard Input and Standard Output

read from write to
stdin stdout stderr
C (C99) scanf( format, &x ); or
fscanf(stdin, format, &x ); [10]
printf( format, x ); or
fprintf(stdout, format, x ); [11]
fprintf(stderr, format, x );[12]
C++ [std::]cin >> x; [std::]cout << x; [std::]cerr << x;
[std::]clog << x;
C# x = Console[.In].Read(); or
x = Console[.In].ReadLine();
Console[.Out].Write([format, ]x); or
Console[.Out].WriteLine([format, ]x);
Console.Error.Write([format, ]x); or
Console.Error.WriteLine([format, ]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);
JavaScript [1]
JavaScript
Web Browser implementation
x = promt ([QuestionText], [DefaultValue]) alert (x) or
document.write(x)
JavaScript
Active Server Pages
Response.Write (x)
JavaScript
Windows Script Host
WScript.echo (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[.In].Read() or
x = Console[.In].ReadLine()
Console[.Out].Write([format, ]x) or
Console[.Out].WriteLine([format, ]x)
Console.Error.Write([format, ]x) or
Console.Error.WriteLine([format, ]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]
PHP print x; or
echo x; or
printf(format, x);
Windows PowerShell $x = Read-Host; or
$x = [Console]::Read(); or
$x = [Console]::ReadLine();
x; or
Write-Output x; or
echo x
Write-Error x

Reading command-line arguments

Argument values Argument counts
C (C99) argv[n] argc
C++
C# args[n] args.Length
Java args.length
JavaScript (Windows Script Host implementation) WScript.Arguments (n) WScript.Arguments.length
Pascal ParamStr(n) ParamCount
Visual Basic Command [1]
Visual Basic .NET CmdArgs(n) CmdArgs.Length
Python sys.argv len(sys.argv)
S-Lang __argv __argc
FORTRAN 77 [1] [1]
PHP $argv[n] $argc
Windows PowerShell $args[n] $args.Length

Execution of commands

Shell command Execute program
C system(command); exec
C# System.Diagnostics.Process.Start(command);
Visual Basic .NET System.Diagnostics.Process.Start(command)
JavaScript
Windows Script Host implementation
WScript.CreateObject ("WScript.Shell").Run (command [, WindowStyle] [, isWaitOnReturn])
Windows PowerShell [Diagnostics.Process]::Start(command) program

Thread management

Fork thread
C fork();

Notes

  1. ^ 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 ap aq ar as at au This language does not support this feature.
  2. ^ a b c d e f g h i j k l m n o p q This language uses implicit typing to discriminate
  3. ^ This language represents boolean true as a non-zero valued integer, and boolean false as an integer whose value is zero.
  4. ^ a b Multidimensional arrays in C and similar languages are actually arrays of arrays
  5. ^ a b The C-like "type x[]" works in Java, however "type[] x" is the preferred form of array declaration.
  6. ^ C#'s switch statement requires the use of goto case n to achieve fall-through
  7. ^ The instrutions are repeated while the condition is false
  8. ^ a b c "Step n" is used to change the loop interval. If "Step" is omitted, then the loop interval is 1.
  9. ^ a b "Until" can be used instead of "While" to make the loop run while the condition is false
  10. ^ gets(x) and fgets( x, length, stdin ) read unformatted text from stdin. Use of gets is not recommended
  11. ^ puts(x) and fputs( x, stdout ) write unformatted text to stdout
  12. ^ fputs( x, stderr ) writes unformatted text to stderr