|
This article needs additional citations for verification. Please help improve this article by adding reliable references. Unsourced material may be challenged and removed. (February 2009) </td>
</tr>
</table>Template:ProgLangCompare
Comparison of programming languages is a common topic of discussion among software engineers. Basic instructions of several programming languages are compared here.
[edit] Conventions of this article
The bold is the literal code. The non-bold is interpreted by the reader. Statements in guillemets (« … ») are optional. Template:Keypress indicates a necessary indent.
[edit] Type identifiers
^a The C and C++ languages do not specify the exact width of the integer types "short", "int", "long", and (C99, C++0x) "long long", so they are implementation-dependent. The "short", "long", and "long long" types are required to be at least 16, 32, and 64 bits wide, respectively, but can be more. The "int" type is required to be at least as wide as "short" and at most as wide as "long", and is typically the width of the word size on the processor of the machine (i.e. on a 32-bit machine it is often 32 bits wide; on 64-bit machines it is often 64 bits wide). C99 also defines the "[u]intN_t" exact-width types in the stdint.h header. See C syntax#Integral types for more information.
^b Commonly used for characters.
^c Perl 5 does not have distinct types. Integers, floating point numbers, strings, etc. are all considered "scalars".
^d PHP has two arbitrary-precision libraries. The BCMath library just uses strings as datatype. The GMP library uses an internal "resource" type.
^a declarations of single precision often are not honored
[edit] Other variable types
^a specifically, strings of arbitrary length and automatically managed.
^b This language represents a boolean as an integer where false is represented as a value of zero and true by a non-zero value.
^c All values evaluate to either true or false. Everything in TrueClass evaluates to true and everything in FalseClass evaluates to false.
^d This language does not have a separate character type. Characters are represented as strings of length 1.
^e Enumerations in this language are algebraic types with only nullary constructors
[edit] Derived types
|
| fixed size array
| dynamic size array
|
| one-dimensional array
| multi-dimensional array
| one-dimensional array
| multi-dimensional array
|
| C (C99)
| [a]
| [a]
|
|
|
| C++ (STL)
|
|
| «std::»vector<type>
|
| C#
| type[size]
| type[size1, size2,...]
| System.Collections.ArrayList or System.Collections.Generic.List<type>
|
|
| Java
| type[size][b]
| type[size1][size2]...[b]
| ArrayList or ArrayList<type>
|
|
| Objective-C
|
|
| NSMutableArray
|
|
| JavaScript
| Template:N/a
| Template:N/a
| Array[d]
|
| Common Lisp
|
|
|
|
|
| Scheme
|
|
|
|
|
| Pascal
| array[first..last] of type[c]
| array[first1..last1] of array[first2..last2] ... of type [c]
or
array[first1..last1, first2..last2, ...] of type [c]
| Template:N/a
| Template:N/a
|
| Object Pascal (Delphi)
| array of type
| array of array ... of type
|
| Visual Basic
|
|
|
|
|
| Visual Basic .NET
|
|
| System.Collections.ArrayList or System.Collections.Generic.List(Of type)
|
|
| Python
|
|
| list
|
|
| S-Lang
|
|
|
|
|
| FORTRAN 77
|
|
|
|
|
| PHP
|
|
| array
|
|
| Perl
|
|
|
|
|
| Ruby
|
|
| Array
|
|
| Windows PowerShell
| type[]
| type[,,...]
|
|
|
| OCaml
| type array
| type array ... array
|
|
|
| F#
| type [] or type array
| type [,,...]
| System.Collections.ArrayList or System.Collections.Generic.List<type>
|
|
| Standard ML
| type vector or type array
|
|
|
|
| Haskell (GHC)
|
|
|
|
|
^a In most expressions (except the sizeof and & operators), values of array types in C are automatically converted to a pointer of its first argument. Also C's arrays can not be described in this format. See C syntax#Arrays.
^b The C-like "type x[]" works in Java, however "type[] x" is the preferred form of array declaration.
^c Subranges are use to define the bounds of the array.
^d JavaScript's array are a special kind of object.
[edit] Other types
|
| Simple composite types
| Algebraic data types
| Unions
|
| Records
| Tuple expression
|
| C (C99)
| struct «name» {type name;...};
| rowspan=2 Template:N/a
| rowspan=3 Template:N/a
| union {type name;...};
|
| Objective-C
|
| C++
| Template:N/A[a][b]
| Template:N/a
|
| C#
| struct name {type name;...}
|
|
| rowspan=3 Template:N/a
|
| Java
| Template:N/a[a]
|
|
|
| JavaScript
|
| Template:N/a
|
|
| Common Lisp
|
| (cons val1 val2)[c]
|
|
|
| Scheme
| Template:N/a
|
|
|
| Pascal
| recordend
| Template:N/a
| Template:N/a
| recordcase type of value: (types); ... end
|
| Visual Basic
|
|
|
|
|
| Visual Basic .NET
| Structure name End Structure
|
|
|
|
| Python
| Template:N/a[a]
| «(»val1, val2, val3, ... «)»
|
| Template:N/a
|
| S-Lang
| struct {name [=value], ...}
|
|
|
|
| FORTRAN 77
|
|
|
|
|
| PHP
| Template:N/a[a]
|
|
|
|
| Perl
| Template:N/a[d]
|
|
| rowspan=2 Template:N/a
|
| Ruby
| OpenStruct.new({:name => value})
|
|
|
| Windows PowerShell
|
|
|
|
|
| OCaml
| type name = {«mutable» name : type;...}
| «(»val1, val2, val3, ... «)»
| type name = Foo «of type» | Bar «of type» | ...
| rowspan=4 Template:N/a
|
| F#
|
| Standard ML
| type name = {name : type,...}
| (val1, val2, val3, ... )
| datatype name = Foo «of type» | Bar «of type» | ...
|
| Haskell
| data Name = Constr {name :: type,...}
| data Name = Foo «types» | Bar «types» | ...
|
^a Only classes are supported.
^b structs in C++ are actually classes, but POD objects are in fact records.
^c pair only
^d Although Perl doesn't have records, because Perl's type system allows different data types to be in an array, "hashes" (associative arrays) that don't have a variable index would effectively be the same as records.
^e Enumerations in this language are algebraic types with only nullary constructors
[edit] Declarations
|
| variable
| constant
| type synonym
|
| C (C99)
| type name «= initial_value»;
| enum{ name = value };
| typedef type synonym;
|
| Objective-C
|
| C++
| const type name = value;
|
| C#
| type name «= initial_value»; Template:Br var name = value;
| using synonym = type;
|
| Java
| type name «= initial_value»;
| final type name = value;
| rowspan=2 Template:N/a
|
| JavaScript
| var name «= initial_value»;
| const name = value;
|
| Common Lisp
| (defparameter name initial_value)
| (defconstant name value)
| (deftype synonym () 'type)
|
| Scheme
| (define name initial_value)
|
|
| Pascal[a]
| name: type «= initial_value»
| name = value
| synonym = type
|
| Visual Basic
| Dim name As type
| Const name As type = value
|
|
| Visual Basic .NET
| Dim name As type«= initial_value»
| Imports synonym = type
|
| Python
| name = initial_value
| Template:N/a
| synonym = type[b]
|
| S-Lang
| name = initial_value;
|
| typedef struct {...} typename
|
| FORTRAN 77
| type name
|
|
|
| PHP
| $name = initial_value;
| define("name", value); const name = value (5.3+)
| rowspan=2 Template:N/a
|
| Perl
| «my» $name «= initial_value»;[c]
| use constant name => value;
|
| Ruby
| name = initial_value
|
| synonym = type[b]
|
| Windows PowerShell
| «[type]» $name = initial_value
|
|
|
| OCaml
| let name «: type ref» = ref value[d]
| let name «: type» = value
| type synonym = type
|
| F#
| let mutable name «: type» = value
|
| Standard ML
| val name «: type ref» = ref value[d]
| val name «: type» = value
|
| Haskell
|
| «name::type;» name = value
| type Synonym = type
|
^a Pascal has declaration blocks. See Comparison of programming languages (basic instructions)#Functions.
^b Types are just regular objects, so you can just assign them.
^c In Perl, the "my" keyword scopes the variable into the block.
^d Technically, this does not declare name to be a mutable variable—in ML, all names can only be bound once; rather, it declares name to point to a "reference" data structure, which is a simple mutable cell. The data structure can then be read and written to using the ! and := operators, respectively.
|
| if
| else if
| select case
| conditional expression
|
| C (C99)
| if (condition) {instructions} «else {instructions}»
| if (condition) {instructions} else if (condition) {instructions} ... «else {instructions}»
| switch (variable) { case1: instructions «break;» ... «default: instructions»}
| condition ? valueIfTrue : valueIfFalse
|
| Objective-C
|
| C++ (STL)
|
| Java
|
| JavaScript
|
| PHP
|
| C#
| switch (variable) { case1: instructions; «jump statement;» ... «default: instructions; «jump statement;»» }
|
| Windows PowerShell
| if (condition) { instructions } elseif (condition) { instructions } ... «else { instructions }»
| switch (variable) { case1 { instructions «break;» ... «default { instructions }»}
|
|
| Perl
| if (condition) {instructions} «else {instructions}» or unless (notcondition) {instructions} «else {instructions}»
| if (condition) {instructions} elsif (condition) {instructions} ... «else {instructions}» or unless (notcondition) {instructions} elsif (condition) {instructions} ... «else {instructions}»
| use feature "switch"; ... given (variable) {case1) { instructions } ... «default { instructions }»}
| condition ? valueIfTrue : valueIfFalse
|
| Ruby
| if condition «else end
| if condition elsif condition ... «else end
| case variable when case1 ... «elseend
|
| Common Lisp
| (when condition instructions) or (if condition (progn instructions) «(progn instructions)»)
| (cond (condition1 instructions) (condition2 instructions) ... «(t instructions)»)
| (case (variable) (case1 instructions) (case2 instructions) ... «(t instructions)»)
| (if condition valueIfTrue valueIfFalse)
|
| Scheme
| (when condition instructions) or (if condition (begin instructions) «(begin instructions)»)
| (cond (condition1 instructions) (condition2 instructions) ... «(else instructions)»)
| (case (variable) ((case1) instructions) ((case2) instructions) ... «(else instructions)»)
|
| Pascal
| if condition then begin end «else beginend»[c]
| if condition then begin end else if condition then begin end ... «else begin end»[c]
| case variable of case1: instructions ... «else: instructions» end[c]
|
|
| Oberon-2
| IF condition THEN «ELSE END[c]
| IF condition THEN ELSIF condition THEN «ELSE END
| CASE expression OF case1a , case1b : | case2a , case2b : ... «ELSE END
|
|
| Visual Basic
| If condition Then «Else End If
| If condition Then ElseIf condition Then ... «Else End If
| Select Case variable Case case1 ... «Case Else End Select
| IIf(condition, valueIfTrue, valueIfFalse)
|
| Visual Basic .NET
| If(condition, valueIfTrue, valueIfFalse)
|
| Python [a]
| if condition : Template:Keypress instructions «else: Template:Keypress instructions»
| if condition : Template:Keypress instructions elif condition : Template:Keypress instructions ... «else: Template:Keypress instructions»
| Template:N/a
| valueIfTrue if condition else valueIfFalse (Python 2.5+)
|
| 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
| index = f(variable) GOTO ( c1, c2, ... cn) index ... c1 instructions
|
|
| Forth
| condition IF instructions « ELSE instructions» THEN
| condition IF instructions ELSE condition IF instructions THEN THEN
| value CASE case OF instructions ENDOF case OF instructions ENDOF default instructions ENDCASE
| condition IF valueIfTrue ELSE valueIfFalse THEN
|
| OCaml
| 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»
| match value with pattern1 -> expression | pattern2 -> expression ... «| _ -> expression»[b]
| if condition then valueIfTrue else valueIfFalse
|
| F#
| if condition then Template:Keypress instructions «else Template:Keypress instructions»
| if condition then Template:Keypress instructions elif condition then Template:Keypress instructions ... «else Template:Keypress instructions»
|
| Standard ML
| if condition then «(»instructions «)» else «(» instructions «)»
| if condition then «(»instructions «)» else if condition then «(» instructions «)» ... else «(» instructions «)»
| case value ofpattern1 => expression | pattern2 => expression ... «| _ => expression»[b]
|
| Haskell (GHC)
| if condition then expression else expression or when condition (do instructions) or unless notcondition (do instructions)
| result | condition = expression | condition = expression | otherwise = expression
| case value of {pattern1 -> expression; pattern2 -> expression; ... «_ -> expression»}[b]
|
|
| if
| else if
| select case
| conditional expression
|
^a A single instruction can be written on the same line following the colon. Multiple instructions are grouped together in a block which starts on a newline (The indentation in required). The conditional expression syntax does not follow this rule.
^b This is pattern matching and is similar to select case but not the same. It is usually used to deconstruct algebraic data types.
^c In languages of the Pascal family, the semicolon is not part of the statement. It is a separator between statements, not a terminator.
|
| while
| do while
| for i = first to last
| foreach
|
| C (C99)
| while (condition) { instructions }
| do { instructions } while (condition)
| for («type» i = first; i <= last; ++i) { instructions }
| Template:N/a
|
| Objective-C
| for (type item in set) { instructions }
|
| C++ (STL)
| «std::»for_each(start, end, function)
|
| C#
| foreach (type item in set) { instructions }
|
| Java
| for (type item : set) { instructions }
|
| JavaScript
| for (var i = first; i <= last; i++) { instructions }
| for (var property in object) { instructions }
|
| PHP
| foreach (range(first, last-1) as $i) { instructions } or for ($i = first; $i <= last; $i++) { instructions }
| foreach (set as item) { instructions } or foreach (set as key => item) { instructions }
|
| Windows PowerShell
| for ($i = first; $i -le last; $i++) { instructions }
| foreach (item in set) { instructions using item }
|
| Perl
| while (condition) { instructions } or until (notcondition) { instructions }
| do { instructions } while (condition) or do { instructions } until (notcondition)
| for«each» «$i» (0 .. N-1) { instructions } or for ($i = first; $i <= last; $i++) { instructions }
| for«each» «$item» (set) { instructions }
|
| Ruby
| while condition end or until notcondition end
| begin end while condition or begin end until notcondition
| for i in first...last end or first.upto(last-1) { |i| instructions }
| for item in set end or set.each { |item| instructions }
|
| Common Lisp
| (loop while condition do instructions) or (do () (notcondition) instructions)
| (loop do instructions while condition)
| (loop for i from first to last do instructions) or (dotimes (i N) instructions) or (do ((i first (1+ i))) ((>= i last)) instructions)
| (loop for item in set do instructions) or (dolist (item set) instructions)
|
| Scheme
| (do () (notcondition) instructions) or (let loop () (if condition (begin instructions (loop))))
| (let loop () (instructions (if condition (loop))))
| (do ((i first (+ i 1))) ((>= i last)) instructions) or (let loop ((i first)) (if (< i last) (begin instructions (loop (+ i 1)))))
| (for-each (lambda (item) instructions) list)
|
| Pascal
| while condition do begin end
| repeat until notcondition;
| for i := first «step 1» to last do begin end;[a]
| Template:N/a
|
| Visual Basic
| Do While condition Loop or Do Until notcondition Loop
| Do Loop While condition or Do Loop Until notcondition
| For i = first To last «Step 1» Next i
| For Each item In set Next item
|
| Visual Basic .NET
| For i «As type» = first To last «Step 1» Next i[a]
| For Each item As type In set Next item
|
| Python
| while condition : Template:Keypress instructions «else: Template:Keypress instructions»
| Template:N/a
| for i in range(first, last): Template:Keypress instructions «else: Template:Keypress instructions»
| for item in set: Template:Keypress instructions «else: Template:Keypress instructions»
|
| S-Lang
| while (condition) { instructions } «then optional-block»
| do { instructions } while (condition) «then optional-block»
| for (i = first; i < last; i++) { instructions } «then optional-block»
| foreach item(set) «using (what)» { instructions } «then optional-block»
|
| FORTRAN 77
| colspan=2 Template:N/a
| DO nnnn I = first,last instructions nnnn CONTINUE
| Template:N/a
|
| Forth
| BEGIN « instructions » condition WHILE instructions REPEAT
| BEGIN instructions condition UNTIL
| limit start DO instructions LOOP
| Template:N/a
|
| OCaml
| while condition do instructions done
| Template:N/a
| for i = first to last-1 do instructions done
| Array.iter (fun item -> instructions) array List.iter (fun item -> instructions) list
|
| F#
| while condition do Template:Keypress instructions
| Template:N/a
| for i = first to last-1 do Template:Keypress instructions
| for item in set do Template:Keypress instructions or Seq.iter (fun item -> instructions) set
|
| Standard ML
| while condition do ( instructions )
| colspan=2 Template:N/a
| Array.app (fn item => instructions) array app (fn item => instructions) list
|
| Haskell (GHC)
| colspan=2 Template:N/a
| Control.Monad.forM_ [0..N-1] (\i -> do instructions)
| Control.Monad.forM_ list (\item -> do instructions)
|
| Eiffel
| from until loop end
|
^a "step n" is used to change the loop interval. If "step" is omitted, then the loop interval is 1.
|
| throw
| handler
| assertion
|
| C (C99)
| longjmp(state, exception);
| switch (setjmp(state)) { case 0: instructions break; case exception: instructions ... }
| assert(condition);
|
| C++ (STL)
| throw exception;
| try { instructions } catch «(exception)» { instructions } ...
|
| C#
| try { instructions } catch «(exception)» { instructions } ... «finally { instructions }»
| Debug.Assert(condition);
|
| Java
| try { instructions } catch (exception) { instructions } ... «finally { instructions }»
| assert condition;
|
| JavaScript
| try { instructions } catch (exception) { instructions } «finally { instructions }»
| ?
|
| PHP
| try { instructions } catch (exception) { instructions } ...
| assert(condition);
|
| S-Lang
| try { instructions } catch «exception» { instructions } ... «finally { instructions }»
| ?
|
| Windows PowerShell
| trap «[exception]» { instructions } ... instructions
| [Debug]::Assert(condition)
|
| Objective-C
| @throw exception;
| @try { instructions } @catch (exception) { instructions } ... «@finally { instructions }»
| NSAssert(condition, description);
|
| Perl
| die exception;
| eval { instructions }; if ($@) { instructions }
| ?
|
| Ruby
| raise exception
| begin rescue exception ... «else «ensure end
|
|
| Common Lisp
| (error exception)
| (handler-case (progn instructions) (exception instructions) ...)
| ?
|
| Scheme (R6RS)
| (raise exception)
| (guard (con (condition instructions) ...) instructions)
| ?
|
| Pascal
| raise Exception.Create()
| try Except on E: exception do begin instructions end; end;
| ?
|
| Visual Basic
| colspan=2 Template:N/a
| ?
|
| Visual Basic .NET
| Throw exception
| Try Catch «exception» «When condition» ... «Finally End Try
| Debug.Assert(condition)
|
| Python
| raise exception
| try: Template:Keypress instructions except «exception»: Template:Keypress instructions ... «else: Template:Keypress instructions» «finally: Template:Keypress instructions»
| assert condition
|
| FORTRAN 77
| colspan=2 Template:N/a
| ?
|
| Forth
| code THROW
| xt CATCH ( code or 0 )
| Template:N/a
|
| OCaml
| raise exception
| try expression with pattern -> expression ...
| assert condition
|
| F#
| try expression with pattern -> expression ... or try expression finally expression
|
| Standard ML
| raise exception «arg»
| expression handle pattern => expression ...
|
|
| Haskell (GHC)
| throw exception or throwError expression
| catch tryExpression catchExpression or catchError tryExpression catchExpression
| assert condition expression
|
[edit] Other control flow statements
|
| exit block(break)
| continue
| label
| branch (goto)
| return value from generator
|
| C (C99)
| break;
| continue;
| label:
| goto label;
| rowspan=3 Template:N/a
|
| Objective-C
|
| C++ (STL)
|
| C#
| yield return value;
|
| Java
| break «label»;
| continue «label»;
| rowspan=2 Template:N/a
|
|
| JavaScript
| yield value«;»
|
| PHP
| break «levels»;
| continue «levels»;
| goto label;
| return() ;
|
| Perl
| last «label»;
| next «label»;
|
|
| Common Lisp
| (return) or (return-from block)
|
|
| (go tag)
|
|
| Scheme
|
|
|
|
|
|
| Pascal(ISO)
| colspan=2 Template:N/a
| label:[a]
| goto label;
| rowspan=4 Template:N/a
|
| Pascal(FPC)
| break;
| continue;
|
| Visual Basic
| Exit block
| Template:N/a
| label:
| GoTo label
|
| Visual Basic .NET
| Continue block
|
| Python
| break
| continue
| colspan=2 Template:N/a
| yield value
|
| S-Lang
| break;
| continue;
|
|
|
|
| FORTRAN 77
| Template:N/a
|
| a number in columns 1 to 5
| GOTO label
| Template:N/a
|
| Ruby
| break
| next
|
|
|
| Windows PowerShell
| break« label»
| continue
|
|
|
|
| OCaml
| rowspan=4 colspan=4 Template:N/a
|
|
| F#
|
|
| Standard ML
|
|
| Haskell (GHC)
|
|
^a Pascal has declaration blocks. See Comparison of programming languages (basic instructions)#Functions.
See reflection for calling and declaring functions by strings.
|
| calling a function
| basic/void function
| value-returning function
| required main function
|
| C (C99)
| foo(«parameters»);
| void foo(«parameters») { instructions }
| type foo(«parameters») { instructions ... return value; }
| «global declarations» int main(«int argc, char *argv[]») { instructions }
|
| Objective-C
|
| 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 («"parameter", ... ,"last parameter"» "instructions");
| function foo(«parameters») { instructions ... return value; }
| rowspan=3 Template:N/a
|
| Common Lisp
| (foo «parameters»)
| (defun foo (parameters) instructions)
| (defun foo (parameters) instructions... return_value)
|
| Scheme
| (define (foo parameters) instructions) or (define foo (lambda (parameters) instructions))
| (define (foo parameters) instructions... return_value) or (define foo (lambda (parameters) instructions... return_value))
|
| Pascal
| foo«(parameters)»
| procedure foo«(parameters)»; «forward;»[a]
«label
«const
«type
«var
«local function declarations» beginend;
| function foo«(parameters)»: type; «forward;»[a]
«label
«const
«type
«var
«local function declarations» begin instructions; foo := value end;
| program name;
«label
«const
«type
«var
«function declarations» begin end.
|
| Visual Basic
| Foo(«parameters»)
| Sub Foo(«parameters») instructions End Sub
| Function Foo(«parameters») As type End Function
| Sub Main()End Sub
|
| Visual Basic .NET
| Function Foo(«parameters») As type End Function
| Sub Main(«ByVal CmdArgs() As String») instructions End Sub or Function Main(«ByVal CmdArgs() As String») As Integer instructionsEnd Function
|
| Python
| foo(«parameters»)
| def foo(«parameters»): Template:Keypress instructions
| def foo(«parameters»): Template:Keypress instructions Template:Keypress return value
| Template:N/a
|
| S-Lang
| foo(«parameters» «;qualifiers»)
| define foo («parameters») { instructions }
| define foo («parameters») { instructions ... return value; }
| public define slsh_main () { instructions }
|
| FORTRAN 77
|
| SUBROUTINE FOO«(parameters)» instructions END
| type FUNCTION FOO«(parameters)» END
| PROGRAM main
|
| Forth
| «parameters» FOO
| : FOO « stack effect comment: ( before -- after ) » instructions ;
| Template:N/a
| Template:N/a
|
| PHP
| foo(«parameters»)
| function foo(«parameters») { instructions }
| function foo(«parameters») { instructions ... return value; }
| rowspan=5 Template:N/a
|
| Perl
| foo(«parameters») or &foo«(parameters)»
| sub foo { «my (parameters) = @_;» instructions }
| sub foo { «my (parameters) = @_;» instructions... «return» value; }
|
| Ruby
| foo«(parameters)»
| def foo«(parameters)» end
| def foo«(parameters)» instructions «return» value end
|
| Windows PowerShell
|
| function foo «(parameters)» { instructions }; or function foo { «param(parameters)» instructions }
| function foo «(parameters)» { instructions … return value }; or function foo { «param(parameters)» instructions … return value }
|
| OCaml
| foo parameters
| let «rec» foo parameters = instructions
| let «rec» foo parameters = instructions... return_value
|
| F#
| [<EntryPoint>] let main args = instructions
|
| Standard ML
| fun foo parameters = ( instructions )
| fun foo parameters = ( instructions... return_value )
|
| Haskell
| foo parameters = do Template:Keypress instructions
| foo parameters = return_value or foo parameters = do Template:Keypress instructions Template:Keypress return value
| «main :: IO ()» main = do instructions
|
| Eiffel
| foo («parameters»)
| foo («parameters»)
| foo («parameters»): type
| [b]
|
^a Pascal requires "forward;" for forward declarations.
^b Eiffel allows the specification of an application's root class and feature.
Where string is a signed decimal number:
|
| 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);
|
| Objective-C
| integer = [string intValue];
| long = [string longLongValue];
| float = [string doubleValue];
| string = [NSString stringWithFormat:@"%i", integer];
| string = [NSString stringWithFormat:@"%f", float];
|
| C++ (STL)
| «std::»istringstream(string) >> number;
| «std::»ostringstream o; o << number; string = o.str();
|
| C#
| integer = int.Parse(string);
| long = long.Parse(string);
| float = float.Parse(string); or double = double.Parse(string);
| string = number.ToString();
|
| Java
| integer = Integer.parseInt(string);
| long = Long.parseLong(string);
| float = Float.parseFloat(string); or double = Double.parseDouble(string);
| string = Integer.toString(integer);
| string = Float.toString(float); or string = Double.toString(double);
|
| JavaScript[a]
| integer = parseInt(string);
| float = parseFloat(string); or float = new Number (string) or float = string*1;
| Template:N/a
| string = number.toString (); or string = new String (number); or string = number+"";
|
| Common Lisp
| (setf integer (parse-integer string))
| (setf float (read-from-string string))
| (setf string (princ-to-string number))
|
| Scheme
| (define number (string->number string))
| (define string (number->string number))
|
| 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(number)
|
| Visual Basic .NET
|
| Python
| integer = int(string)
| long = long(string)
| float = float(string)
| string = str(number)
|
| S-Lang
| integer = atoi(string);
| long = atol(string);
| float = atof(string);
| string = string(number);
|
| FORTRAN 77
| READ(string,format) integer
| Template:N/a
| READ(string,format) float
| WRITE(string,format) number
|
| PHP
| integer = intval(string); or integer = (int)string;
| float = floatval(string); or float = (float)string;
| string = "number"; or string = strval(number); or string = (string)number;
|
| Perl[b]
| number = 0 + string;
| string = "$number";
|
| Ruby
| integer = string.to_i
| float = string.to_f
| string = number.to_s
|
| Windows PowerShell
| integer = [int]string
| long = [long]string
| float = [float]string
| string = [string]number; or string = "number"; or string = (number).ToString()
|
| OCaml
| let integer = int_of_string string
|
| let float = float_of_string string
| let string = string_of_int integer
| let string = string_of_float float
|
| F#
| let integer = int string
| let integer = int64 string
| let float = float string
| let string = string number
|
| Standard ML
| val integer = Int.fromString string
|
| val float = Real.fromString string
| val string = Int.toString integer
| val string = Real.toString float
|
| Haskell (GHC)
| number = read string
| string = show number
|
^a JavaScript only uses floating point numbers so there are some technicalities.[2]
^b Perl doesn't have separate types. Strings and numbers are interchangeable.
|
| read from
| write to
|
| stdin
| stdout
| stderr
|
| C (C99)
| scanf(format, &x); or fscanf(stdin, format, &x); [a]
| printf( format, x); or fprintf(stdout, format, x); [b]
| fprintf(stderr, format, x );[c]
|
| Objective-C
|
|
|
|
| C++
| «std::»cin >> x; or «std::»getline(«std::»cin, str);
| «std::»cout << x;
| «std::»cerr << x; or «std::»clog << x;
|
| C#
| x = Console.Read(); or x = Console.ReadLine();
| Console.Write(«format, »x); or Console.WriteLine(«format, »x);
| Console.Error.Write(«format, »x); or Console.Error.WriteLine(«format, »x);
|
| Java
| x = System.in.read(); or x = new Scanner(System.in).nextInt(); or x = new Scanner(System.in).nextLine();
| 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 Web Browser implementation
|
| document.write(x)
|
|
JavaScript Active Server Pages
|
| Response.Write(x)
|
|
JavaScript Windows Script Host
| x = WScript.StdIn.Read(chars) or x = WScript.StdIn.ReadLine()
| WScript.Echo(x) or WScript.StdOut.Write(x) or WScript.StdOut.WriteLine(x)
| WScript.StdErr.Write(x) or WScript.StdErr.WriteLine(x)
|
| Common Lisp
| (setf x (read-line))
| (princ x) or (format t format x)
| (princ x *error-output*) or (format *error-output* format x)
|
| Scheme (R6RS)
| (define x (read-line))
| (display x) or (format #t format x)
| (display x (current-error-port)) or (format (current-error-port) format x)
|
| Pascal
| read(x); or readln(x);
| write(x); or writeln(x);
| rowspan=2 Template:N/a
|
| Visual Basic
| Input« prompt,» x
| Print x or ? x
|
| Visual Basic .NET
| x = Console.Read() or x = Console.ReadLine()
| Console.Write(«format, »x) or Console.WriteLine(«format, »x)
| Console.Error.Write(«format, »x) or Console.Error.WriteLine(«format, »x)
|
| Python 2.x
| x = raw_input(«prompt»)
| print x or sys.stdout.write(x)
| print >> sys.stderr, x or sys.stderr.write(x)
|
| Python 3.x
| x = input(«prompt»)
| print(x«, end=""»)
| print(x«, end=""», file=sys.stderr)
|
| S-Lang
| fgets (&x, stdin)
| fputs (x, stdout)
| fputs (x, stderr)
|
| FORTRAN 77
| READ(5,format) variable names
| WRITE(6,format) expressions
| Template:N/a
|
| Forth
| buffer length ACCEPT ( # chars read ) KEY ( char )
| buffer length TYPE char EMIT
| Template:N/a
|
| PHP
| $x = fgets(STDIN); or $x = fscanf(STDIN, format);
| print x; or echo x; or printf(format, x);
| fprintf(STDERR, format, x);
|
| Perl
| $x = <>; or $x = <STDIN>;
| print x; or printf format, x;
| print STDERR x; or printf STDERR format, x;
|
| Ruby
| x = gets
| puts x or printf(format, x)
| $stderr.puts(x) or $stderr.printf(format, x)
|
| Windows PowerShell
| $x = Read-Host«« -Prompt» text»; or $x = [Console]::Read(); or $x = [Console]::ReadLine()
| x; or Write-Output x; or echo x
| Write-Error x
|
| OCaml
| let x = read_int () or let str = read_line () or Scanf.scanf format (fun x ... -> ...)
| print_int x or print_endline str or Printf.printf format x ...
| prerr_int x or prerr_endline str or Printf.eprintf format x ...
|
| F#
| let x = System.Console.ReadLine()
| printf format x ... or printfn format x ...
| eprintf format x ... or eprintfn format x ...
|
| Standard ML
| val str = TextIO.inputLIne TextIO.stdIn
| print str
| TextIO.output (TextIO.stdErr, str)
|
| Haskell (GHC)
| x <- readLn or str <- getLine
| print x or putStrLn str
| hPrint stderr x or hPutStrLn stderr str
|
^a gets(x) and fgets(x, length, stdin) read unformatted text from stdin. Use of gets is not recommended.
^b puts(x) and fputs(x, stdout) write unformatted text to stdout.
^c fputs(x, stderr) writes unformatted text to stderr
|
| Argument values
| Argument counts
| Program name / Script name
|
| C (C99)
| argv[n]
| argc
| first argument
|
| Objective-C
|
| C++
|
| C#
| args[n]
| args.Length
| Assembly.GetEntryAssembly().Location;
|
| Java
| args.length
|
|
JavaScript Windows Script Host implementation
| WScript.Arguments(n)
| WScript.Arguments.length
| ?
|
| Common Lisp
| ?
| ?
| ?
|
| Scheme (R6RS)
| (list-ref (command-line) n)
| (length (command-line))
| first argument
|
| Pascal
| ParamStr(n)
| ParamCount
| first argument
|
| Visual Basic
| Command
| Template:N/a
| ?
|
| Visual Basic .NET
| CmdArgs(n)
| CmdArgs.Length
| [Assembly].GetEntryAssembly().Location
|
| Python
| sys.argv[n]
| len(sys.argv)
| first argument
|
| S-Lang
| __argv[n]
| __argc
| first argument
|
| FORTRAN 77
| colspan=2 Template:N/a
| ?
|
| PHP
| $argv[n]
| $argc
| first argument
|
| Perl
| $ARGV[n]
| scalar(@ARGV)
| $0
|
| Ruby
| ARGV[n]
| ARGV.size
| $0
|
| Windows PowerShell
| $args[n]
| $args.Length
| $MyInvocation.MyCommand.Name
|
| OCaml
| Sys.argv.(n)
| Array.length Sys.argv
| first argument
|
| F#
| args.[n]
| args.Length
| Assembly.GetEntryAssembly().Location
|
| Standard ML
| List.nth (CommandLine.arguments (), n)
| length (CommandLine.arguments ())
| CommandLine.name ()
|
| Haskell (GHC)
| do { args <- System.getArgs; return args !! n }
| do { args <- System.getArgs; return length args }
| System.getProgName
|
[edit] Execution of commands
|
| Shell command
| Execute program
|
| C
| system("command");
| execl(path, args); or execv(path, arglist);
|
| C++
|
| C#
|
| System.Diagnostics.Process.Start(path, argstring);
|
| F#
|
|
| Visual Basic
| Interaction.Shell(command «, WindowStyle» «, isWaitOnReturn»)
|
|
| Visual Basic .NET
| Microsoft.VisualBasic.Interaction.Shell(command «, WindowStyle» «, isWaitOnReturn»)
| System.Diagnostics.Process.Start(path, argstring)
|
| Java
| Runtime.exec(command); or new ProcessBuilder(command).start();
|
|
JavaScript Windows Script Host implementation
| WScript.CreateObject ("WScript.Shell").Run(command «, WindowStyle» «, isWaitOnReturn»);
|
|
| Common Lisp
| (shell command)
|
|
| Scheme
| (system command)
|
|
| Pascal
| system(command);
|
|
| OCaml
| Sys.command command, Unix.open_process_full command env (stdout, stdin, stderr),...
| Unix.execv prog args, Unix.execve prog args env, Unix.create_process prog args new_stdin new_stdout new_stderr, ...
|
| Standard ML
| OS.Process.system command
|
|
| Haskell (GHC)
| System.system command
|
|
| Perl
| system(command) or $output = `command`
| exec(path, args)
|
| Ruby
| system(command) or output = `command`
| exec(path, args)
|
| PHP
| system(command) or output = `command`
| exec(command)
|
| Python
| os.system(command) or subprocess.Popen(command)
| os.execv(path, args)
|
| S-Lang
| system(command)
|
|
| Windows PowerShell
| [Diagnostics.Process]::Start(command)
| «Invoke-Item »program arg1 arg2 …
|
[edit] References
|