Comparison of programming languages (basic instructions): Difference between revisions
Appearance
Content deleted Content added
Line 803: | Line 803: | ||
!Fork thread |
!Fork thread |
||
|- |
|- |
||
|[[C (programming language)|C]] |
|||
|[[C]] |
|||
|[[Fork (operating system)|fork]](); |
|[[Fork (operating system)|fork]](); |
||
|} |
|} |
Revision as of 13:24, 23 March 2008
Variable declaration
How to declare variable x as the following types:
Signed integer | Unsigned integer | Long integer | |
---|---|---|---|
C (C99) | int x; | unsigned int x; | long x: |
C++ (STL) | unsigned int x; or uint x; | ||
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] |
PHP[2] | $x = value; | ||
Windows PowerShell | $x = value; or New-Variable -Name x -Value value -Description description |
Single precision | Double precision | ||
---|---|---|---|
C (C99) | 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 | |
PHP[2] | $x = value; | ||
Windows PowerShell | $x = value; or New-Variable -Name x -Value value -Description description |
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 | ||
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 | |
Windows PowerShell | [1] | [1] |
Other
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[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 |
PHP[2] | $x = value; | $x = "string"; or $x = 'string'; | $x = value; |
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 = []; |
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); | |
Windows PowerShell | $x = @() | $x = New-Object 'object[,,...]' n1,n2,...; or $x = @(@(), @(), @(), ...); |
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 | |||
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 meaning: 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] |
PHP | if (condition) { instructions } [else { instructions }] | if (condition) { instructions } else if (condition) { instructions } ... [else { instructions }] | switch (variable) { case case1: instructions break; ... [default: instructions;]} |
Windows PowerShell | if (condition) { instructions } [else { instructions }] | if (condition) { instructions } elseif (condition) { instructions } ... [else { instructions }] | switch (variable) { case1 { instructions } ... [default { instructions }] } |
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] } | ||
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] |
PHP | while (condition) { instructions } | do { instructions } while (condition) | for ($i = 0; $i < N; $i++) { instructions } | foreach (set as item) { instructions } or foreach (set as key => item) { instructions } |
Windows PowerShell | while (condition) { instructions } | do { instructions } while (condition) | for ($i = 0; $i -lt N; $i++) { instructions } | foreach (item in set) { instructions } |
raise | 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 }] | |
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 ] |
S-Lang | throw exception; | try { instructions } catch [exception] ... [finally { instructions }] |
FORTRAN 77 | [1] | [1] |
Windows PowerShell | throw exception | trap { instructions } |
Function declaration
Statements in square brackets are optional
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 } | [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] |
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; |
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) | 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); |
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 | |
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) | |
Windows PowerShell | [Diagnostics.Process]::Start(command) | program |
Thread management
Fork thread | |
---|---|
C | fork(); |
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 ap aq ar This language does not support this feature.
- ^ a b c d e f g h i j k l m n 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