Comparison of programming languages (basic instructions)#Data conversions
{{Short description|None}}
{{Refimprove|date=February 2009}}
{{ProgLangCompare}}
This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.
__TOC__
Conventions of this article
Generally, var, {{var|var}}, or {{var serif|var}} is how variable names or other non-literal values to be interpreted by the reader are represented. The rest is literal code. Guillemets ({{code|«}} and {{code|»}}) enclose optional sections. {{keypress|Tab}} indicates a necessary (whitespace) indentation.
The tables are not sorted lexicographically ascending by programming language name by default, and that some languages have entries in some tables but not others.
{{Clear}}
Type identifiers
= [[Integer (computer science)|Integer]]s =
class="wikitable sortable"
! rowspan=2| ! colspan=2| 8 bit (byte) ! colspan=2| 16 bit (short integer) ! colspan=2| 32 bit ! colspan=2| 64 bit (long integer) ! colspan=2| Word size ! rowspan=2| Arbitrarily precise (bignum) |
Signed
! Unsigned ! Signed ! Unsigned ! Signed ! Unsigned ! Signed ! Unsigned ! Signed ! Unsigned |
---|
AdaAda Reference Manual – Language and Standard Libraries; ISO/IEC 8652:201x (E), {{cite web |url=http://www.ada-auth.org/standards/12rm/RM-Final.pdf |title=Reference Manual |access-date=2013-07-19 |url-status=dead |archive-url=https://web.archive.org/web/20110427190723/http://www.ada-auth.org/standards/12rm/RM-Final.pdf |archive-date=2011-04-27 }}
| | | | | | | | | | | {{n/a}} |
rowspan=2| ALGOL 68 (variable-width)
| rowspan=2| | rowspan=2 {{n/a}} | rowspan=2| | rowspan=2 {{n/a}} | rowspan=2| | rowspan=2 {{n/a}} | rowspan=2| | rowspan=2 {{n/a}} | | {{n/a}} | rowspan=2| |
colspan=2 align=center|bytes and bits |
C (C99 fixed-width)
| rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=5 {{n/a}} |
C++ (C++11 fixed-width) |
C (C99 variable-width)
| rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| | rowspan=2| |
C++ (C++11 variable-width) |
Objective-C (Cocoa)
| | | | | | | | | | |
C#
| | | rowspan=2| | | rowspan=2| | | rowspan=2| | | | | |
Java
| | {{n/a}} | | {{n/a}} | {{n/a}} | {{n/a}} | {{n/a}} | |
Go
| | | | | | | | | | | |
Rust
| | | | | | | | | | | {{n/a}} |
Swift
| | | | | | | | | | |
D
| | | | | | | | | {{n/a}} | {{n/a}} | |
Common Lisp{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Front/index.htm|title=Common Lisp HyperSpec (TM)|work=lispworks.com|access-date=30 January 2017}}
| | | | | | | | | | | |
Scheme
| | | | | | | | | | | |
ISLISP{{cite web|url=http://www.islisp.info/specification.html|title=www.islisp.info: Specification|work=islisp.info|access-date=30 January 2017|archive-url=https://web.archive.org/web/20160122121427/http://islisp.info/specification.html|archive-date=22 January 2016|url-status=dead}}
| | | | | | | | | | | |
Pascal (FPC)
| | | | | | | | | | | {{n/a}} |
Visual Basic
| {{n/a}} | rowspan=2| | | {{n/a}} | | {{n/a}} | colspan=2 {{n/a}} | rowspan=2 colspan=2 {{n/a}} | rowspan=1 {{n/a}} |
Visual Basic .NET
| | | | | | | | |
FreeBasic
| | | | | | | | | | | {{n/a}} |
Python 2.x
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | | {{n/a}} | |
Python 3.x
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | |
S-Lang
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | {{n/a}} |
Fortran
| | {{n/a}} | | {{n/a}} | | {{n/a}} | | {{n/a}} | | | |
PHP
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | | {{n/a}} | | {{n/a}} | colspan=2 {{n/a}} | {{ref|PHP bignum|[e]}} |
Perl 5
| colspan=2 {{n/a}}{{ref|scalars|[d]}} | colspan=2 {{n/a}}{{ref|scalars|[d]}} | colspan=2 {{n/a}}{{ref|scalars|[d]}} | colspan=2 {{n/a}}{{ref|scalars|[d]}} | colspan=2 {{n/a}}{{ref|scalars|[d]}} | |
Raku
| | | | | | | | | | colspan=2 {{n/a}} |
Ruby
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | | {{n/a}} | |
Erlang{{ref|Erlang_int|[n]}}
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | | {{n/a}} | |
Scala
| | {{n/a}} | | | | {{n/a}} | | {{n/a}} | {{n/a}} | {{n/a}} | |
Seed7
| {{n/a}} | {{n/a}} | {{n/a}} | {{n/a}} | {{n/a}} | {{n/a}} | | {{n/a}} | {{n/a}} | {{n/a}} | |
Smalltalk
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | | {{n/a}} | |
Windows PowerShell
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | {{n/a}} |
OCaml
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | | {{n/a}} | rowspan=2| | {{n/a}} | | | |
F#
| | | | | | | | | | |
Standard ML
| {{n/a}} | | colspan=2 {{n/a}} | | | | | | | |
Haskell (GHC)
| | | | | | | | | | | |
Eiffel
| | | | | | | | | | | {{n/a}} |
COBOL{{ref|Cobol|[h]}}
| | | | | | | | | {{n/a}} | {{n/a}} | {{n/a}} |
Mathematica
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | |
Wolfram Language
| colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | colspan=2 {{n/a}} | |
- {{note|a68|a}} The standard constants
int shorts
andint lengths
can be used to determine how manyshort
s andlong
s can be usefully prefixed toshort int
andlong int
. The actual sizes ofshort int
,int
, andlong int
are available as the constantsshort max int
,max int
, andlong max int
etc. - {{note|Java char|b}} Commonly used for characters.
- {{note|CInt|c}} The ALGOL 68, C and C++ languages do not specify the exact width of the integer types
short
,int
,long
, and (C99, C++11)long long
, so they are implementation-dependent. In C and C++short
,long
, andlong long
types are required to be at least 16, 32, and 64 bits wide, respectively, but can be more. Theint
type is required to be at least as wide asshort
and at most as wide aslong
, 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 sometimes 64 bits wide). C99 and C++11{{citation needed|date=December 2011}} also define the[u]intN_t
exact-width types in the stdint.h header. See C syntax#Integral types for more information. In addition the typessize_t
andptrdiff_t
are defined in relation to the address size to hold unsigned and signed integers sufficiently large to handle array indices and the difference between pointers. - {{note|scalars|d}} Perl 5 does not have distinct types. Integers, floating point numbers, strings, etc. are all considered "scalars".
- {{note|PHP bignum|e}} PHP has two arbitrary-precision libraries. The BCMath library just uses strings as datatype. The GMP library uses an internal "resource" type.
- {{note|Kinds|f}} The value of
n
is provided by theSELECTED_INT_KIND
{{cite web|url=http://fortranwiki.org/fortran/show/selected_int_kind|title=selected_int_kind in Fortran Wiki|work=fortranwiki.org|access-date=30 January 2017}} intrinsic function. - {{note|a68g|g}} ALGOL 68G's runtime option
--precision "number"
can set precision forlong long int
s to the required "number" significant digits. The standard constantslong long int width
andlong long max int
can be used to determine actual precision. - {{note|Cobol|h}} COBOL allows the specification of a required precision and will automatically select an available type capable of representing the specified precision. "
PIC S9999
", for example, would require a signed variable of four decimal digits precision. If specified as a binary field, this would select a 16-bit signed type on most platforms. - {{note|Smalltalk|i}} Smalltalk automatically chooses an appropriate representation for integral numbers. Typically, two representations are present, one for integers fitting the native word size minus any tag bit ({{mono|SmallInteger}}) and one supporting arbitrary sized integers ({{mono|LargeInteger}}). Arithmetic operations support polymorphic arguments and return the result in the most appropriate compact representation.
- {{note|Ada_range|j}} Ada range types are checked for boundary violations at run-time (as well as at compile-time for static expressions). Run-time boundary violations raise a "constraint error" exception. Ranges are not restricted to powers of two. Commonly predefined Integer subtypes are: Positive (
range 1 .. Integer'Last
) and Natural (range 0 .. Integer'Last
).Short_Short_Integer
(8 bits),Short_Integer
(16 bits) andLong_Integer
(64 bits) are also commonly predefined, but not required by the Ada standard. Runtime checks can be disabled if performance is more important than integrity checks. - {{note|Ada mod|k}} Ada modulo types implement modulo arithmetic in all operations, i.e. no range violations are possible. Modulos are not restricted to powers of two.
- {{note|Scala char|l}} Commonly used for characters like Java's char.
- {{note|PHP 32/64 bit long|m}}
int
in PHP has the same width aslong
type in C has on that system.{{ref|CInt|[c]}} - {{note|Erlang int|n}} Erlang is dynamically typed. The type identifiers are usually used to specify types of record fields and the argument and return types of functions.{{cite web|url=http://www.erlang.org/doc/reference_manual/typespec.html|title=Erlang {{--}} Types and Function Specifications|work=erlang.org|access-date=30 January 2017}}
- {{note|Erlang arb|o}} When it exceeds one word.{{cite web|url=http://www.erlang.org/doc/efficiency_guide/advanced.html|title=Erlang {{--}} Advanced|work=erlang.org|access-date=30 January 2017}}
= [[Floating point]] =
class="wikitable sortable"
! ! Other precision ! Processor dependent |
Ada
| | | {{n/a}} |
ALGOL 68
| | | |
C
| rowspan=3| | rowspan=5| | rowspan=3| |
C++ (STL) |
Objective-C (Cocoa)
| |
C#
| rowspan=2| | rowspan=3 {{n/a}} |
Java |
Go
| | |
Rust
| | | |
Swift
| | | | |
D
| | | | |
Common Lisp
| | | |
Scheme
| | | |
ISLISP
| | | |
Pascal (FPC)
| | | | |
Visual Basic
| rowspan=3| | rowspan=3| | rowspan=3 {{n/a}} |
Visual Basic .NET |
Xojo |
Python
| rowspan=2 {{n/a}} | | |
JavaScript
| | {{n/a}} |
S-Lang
| | | |
Fortran
| colspan=2| | |
PHP
| | | |
Perl
| | | |
Raku
| | | | |
Ruby
| {{n/a}} | | rowspan=4 {{n/a}} |
Scala
| | |
Seed7
| {{n/a}} | |
Smalltalk
| | |
Windows PowerShell
| | | |
OCaml
| {{n/a}} | rowspan=2| | rowspan=3 {{n/a}} |
F#
| |
Standard ML
| {{n/a}} | | |
Haskell (GHC)
| | | |
Eiffel
| | | |
COBOL
| | | |
Mathematica
| {{n/a}} | {{n/a}} | | |
- {{note|a68_real|a}} The standard constants
real shorts
andreal lengths
can be used to determine how manyshort
s andlong
s can be usefully prefixed toshort real
andlong real
. The actual sizes ofshort real
,real
, andlong real
are available as the constantsshort max real
,max real
andlong max real
etc. With the constantsshort small real
,small real
andlong small real
available for each type's machine epsilon. - {{note|lax floats|b}} declarations of single precision often are not honored
- {{note|real kinds|c}} The value of
n
is provided by theSELECTED_REAL_KIND
{{cite web|url=http://fortranwiki.org/fortran/show/selected_real_kind|title=selected_real_kind in Fortran Wiki|work=fortranwiki.org|access-date=30 January 2017}} intrinsic function. - {{note|a68g-real|d}} ALGOL 68G's runtime option
--precision "number"
can set precision forlong long real
s to the required "number" significant digits. The standard constantslong long real width
andlong long max real
can be used to determine actual precision. - {{note|Cobol-ieee|e}} These IEEE floating-point types will be introduced in the next COBOL standard.
- {{note|C-long-double|f}} Same size as
double
on many implementations. - {{note|Swift-long-double|g}} Swift supports 80-bit extended precision floating point type, equivalent to
long double
in C languages.
= [[Complex number]]s =
class="wikitable sortable"
! ! Integer ! Single precision ! Double precision ! Half and Quadruple precision etc. |
Ada
| {{n/a}} | | | |
ALGOL 68
| {{n/a}} | | | |
C (C99){{cite web|url=https://www.gnu.org/software/libc/manual/html_node/Complex-Numbers.html#Complex-Numbers|title=The GNU C Library: Complex Numbers|work=gnu.org|access-date=30 January 2017}}
| {{n/a}} | | | rowspan=7 {{n/a}} |
C++ (STL)
| {{n/a}} | | |
C#
| {{n/a}} | {{n/a}} | |
Java
| {{n/a}} | {{n/a}} | {{n/a}} |
Go
| {{n/a}} | | |
D
| {{n/a}} | | |
Objective-C
| {{n/a}} | {{n/a}} | {{n/a}} |
Common Lisp
| (complex integer) | (complex single-float) | (complex double-float) | complex |
Scheme
| | | | rowspan=4 {{n/a}} |
Pascal
| {{n/a}} | {{n/a}} | |
Visual Basic
| {{n/a}} | {{n/a}} | |
Visual Basic .NET
| {{n/a}} | {{n/a}} | |
Perl
| | | | |
Raku
| | | | |
Python
| | | | rowspan=15 {{n/a}} |
JavaScript
| {{n/a}} | {{n/a}} | |
S-Lang
| {{n/a}} | {{n/a}} | |
Fortran
| | colspan=2| |
Ruby
| | {{n/a}} | |
Scala
| {{n/a}} | {{n/a}} | {{n/a}} |
Seed7
| {{n/a}} | {{n/a}} | |
Smalltalk
| | | |
Windows PowerShell
| {{n/a}} | {{n/a}} | |
OCaml
| {{n/a}} | {{n/a}} | |
F#
| | | |
Standard ML
| {{n/a}} | {{n/a}} | {{n/a}} |
Haskell (GHC)
| {{n/a}} | | |
Eiffel
| {{n/a}} | {{n/a}} | {{n/a}} |
COBOL
| {{n/a}} | {{n/a}} | {{n/a}} |
Mathematica
| | {{n/a}} | {{n/a}} | |
= Other variable types =
class="wikitable sortable"
! rowspan=2| ! colspan=2| Text ! rowspan=2| Boolean ! rowspan=2| Enumeration |
Character
! String{{ref|string|[a]}} |
---|
Ada
| | | | | |
ALGOL 68
| | | | {{n/a}} - [http://rosettacode.org/wiki/Enumerations#ALGOL_68 User defined] | {{n/a}} |
C (C99)
| rowspan=2| | {{n/a}} | rowspan=2| | rowspan=3| |
C++ (STL) |
Objective-C
| | | | |
C#
| rowspan=2| | | | | object |
Java
| | | | |
Go
| | | | | |
Rust
| | | | | |
Swift
| | | | | |
D
| | | | | |
Common Lisp
| | | | | |
Scheme
| | | | | |
ISLISP
| | | | | |
Pascal (ISO)
| rowspan=2| | {{n/a}} | rowspan=2| | rowspan=2| | {{n/a}} |
Object Pascal (Delphi)
| | |
Visual Basic
| {{n/a}} | rowspan=3| | rowspan=3| | rowspan=3| | |
Visual Basic .NET
| | |
Xojo
| {{n/a}} | |
Python
| {{n/a}}{{ref|string as char|[d]}} | | | | |
JavaScript
| {{n/a}}{{ref|string as char|[d]}} | | | | |
S-Lang
| | | | | |
Fortran
| | | | | |
PHP
| {{n/a}}{{ref|string as char|[d]}} | | | | (type declaration omitted) |
Perl
| {{n/a}}{{ref|string as char|[d]}} | | | | |
Raku
| | | | | |
Ruby
| {{n/a}}{{ref|string as char|[d]}} | | | | |
Scala
| | | | | |
Seed7
| | | | | |
Windows PowerShell
| | | | | |
OCaml
| rowspan=3| | rowspan=3| | rowspan=3| | {{n/a}}{{ref|enum|[e]}} | {{n/a}} |
F#
| | |
Standard ML
| {{n/a}}{{ref|enum|[e]}} | {{n/a}} |
Haskell (GHC)
| | | | {{n/a}}{{ref|enum|[e]}} | {{n/a}} |
Eiffel
| | | | {{n/a}} | |
COBOL
| | | | {{n/a}} | |
Mathematica
| {{n/a}}{{ref|string as char|[d]}} | | | | {{n/a}} |
- {{note|string|a}} specifically, strings of arbitrary length and automatically managed.
- {{note|int bool|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.
- {{note|Ruby's bool|c}} All values evaluate to either true or false. Everything in
TrueClass
evaluates to true and everything inFalseClass
evaluates to false. - {{note|string as char|d}}This language does not have a separate character type. Characters are represented as strings of length 1.
- {{note|enum|e}} Enumerations in this language are algebraic types with only nullary constructors
- {{note|logical kinds|f}} The value of
n
is provided by theSELECTED_INT_KIND
intrinsic function.
Derived types
= [[Array data type|Array]] =
{{Further|Comparison of programming languages (array)}}
class="wikitable sortable" |
valign="top"
! rowspan=2| ! colspan=2| fixed size array ! colspan=2| dynamic size array |
valign="top" |
valign="top"
| Ada | | | | |
valign="top"
| ALGOL 68 | | | | |
valign="top"
| | | | |
valign="top"
| C++ (STL) | colspan=1| | | colspan=2| |
valign="top"
| C# | | | | |
valign="top"
| Java | | | | |
valign="top"
| D | | | | |
valign="top"
| Go | | | | |
valign="top"
| Rust | | | | |
valign="top"
| Swift | | | |
valign="top"
| | | | |
valign="top"
| {{n/a}} | {{n/a}} | colspan=2| |
valign="top"
| | | | |
valign="top"
| Scheme | | | | |
valign="top"
| ISLISP | | | | |
valign="top"
| Pascal | rowspan=2| | rowspan=2| | {{n/a}} | {{n/a}} |
valign="top"
| Object Pascal (Delphi) | | |
valign="top"
| | | | |
valign="top"
| | | | |
valign="top"
| Python | | | | |
valign="top"
| S-Lang | | | | |
valign="top"
| Fortran | | | | |
valign="top"
| PHP | | | | |
valign="top"
| Perl | | | | |
valign="top"
| Raku | | | | |
valign="top"
| Ruby | | | | |
valign="top"
| Scala | | | | |
valign="top"
| Seed7 | | | | |
valign="top"
| | | | |
valign="top"
| | | | |
valign="top"
| OCaml | | | | |
valign="top"
| F# | | | | |
valign="top"
| | | | |
valign="top"
| | | | |
valign="top"
| COBOL | | {{em|one-dimensional array definition...}} | | {{n/a}} |
- {{note|C's array|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. See C syntax#Arrays for further details of syntax and pointer operations. - {{note|Java's array|b}} The C-like
type x[]
works in Java, howevertype[] x
is the preferred form of array declaration. - {{note|subrange|c}} Subranges are used to define the bounds of the array.
- {{note|JavaScript's array|d}} JavaScript's array are a special kind of object.
- {{note|COBOL DEPENDING ON clause|e}} The
DEPENDING ON
clause in COBOL does not create a true variable length array and will always allocate the maximum size of the array.
= Other types =
class="wikitable sortable"
! rowspan=2| ! colspan=2|Simple composite types ! rowspan=2|Algebraic data types ! rowspan=2|Unions |
Records
! Tuple expression |
---|
Ada
| | {{n/a}} | Any combination of records, unions, and enumerations (as well as references to those, enabling recursive types). | |
ALGOL 68
| | colspan=2 align=center| Required types and operators can be user-defined | |
C (C99)
| rowspan=2| | rowspan=2 {{n/a}} | rowspan=3 {{n/a}} | rowspan=3| |
Objective-C |
C++
| | |
C#
| | | | rowspan=3 {{n/a}} |
Java
| {{n/a}}{{ref|just classes|[a]}} | | |
JavaScript
| | {{n/a}} | |
D
| | | | |
Go
| | | | |
Rust
| | | | |
Swift
| | | | |
Common Lisp
| | rowspan=3| | | |
Scheme
| {{n/a}} | | |
ISLISP
| | | |
Pascal
| | {{n/a}} | {{n/a}} | |
Visual Basic
| | | | |
Visual Basic .NET
| | | | |
Python
| {{n/a}}{{ref|just classes|[a]}} | | | {{n/a}} |
S-Lang
| | | | |
Fortran
| | | | |
PHP
| {{n/a}}{{ref|just classes|[a]}} | | | |
Perl
| {{n/a}}{{ref|Perl's records|[d]}} | | | rowspan=3 {{n/a}} |
Raku
| {{n/a}}{{ref|just classes|[a]}} | | |
Ruby
| {{code|lang=ruby|1=OpenStruct.new({:name => value})}} | | |
Scala
| | | | |
Windows PowerShell
| | | | |
OCaml
| rowspan=2| | rowspan=2| | rowspan=2| | rowspan=4 {{n/a}} |
F# |
Standard ML
| | rowspan=2| | |
Haskell
| | |
COBOL
| | {{n/a}} | {{n/a}} | |
- {{note|just classes|a}} Only classes are supported.
- {{note|C++'s struct|b}}
struct
s in C++ are actually classes, but have default public visibility and are also POD objects. C++11 extended this further, to make classes act identically to POD objects in many more cases. - {{note|pair only|c}} pair only
- {{note|Perl's records|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.
- {{note|enum|e}} Enumerations in this language are algebraic types with only nullary constructors
Variable and constant declarations
class="wikitable sortable"
! ! style="width: 30%" | variable ! constant ! type synonym |
Ada
| | | |
ALGOL 68
| | | |
C (C99)
| rowspan=3| | rowspan=2| | rowspan=3| |
Objective-C |
C++
| |
C#
| | | |
D
| | | |
Java
| | | rowspan=2 {{n/a}} |
JavaScript
| | |
Go
| | | |
Rust{{ref|Rust declaration|[f]}}
| | | |
Swift
| | | |
Common Lisp
| | | |
Scheme
| | | |
ISLISP
| | | {{n/a}} |
Pascal{{ref|Pascal's declarations|[a]}}
| | | |
Visual Basic
| | rowspan=3| See notes to left. Constants use the same syntax, and:
| |
Visual Basic .NET{{Cite web|url=https://ljw1004.github.io/vbspec/vb.html|title=Grammar vb|date=2016-06-17|website=Visual Basic Language Specification|archive-url=https://web.archive.org/web/20190829225051/https://ljw1004.github.io/vbspec/vb.html|archive-date=2019-08-29|url-status=live|access-date=2019-08-29}}
| The variable declaration syntax of VB.NET is unusually difficult to precisely describe. Given that there exist the identifier suffixes ("modifiers"):
and that
valid declaration statements are of the form {{In5}} where, for the purpose of semantic analysis, to convert the
and for which, for each
If | |
Xojo
| | {{n/a}} |
Python
| | rowspan=2 {{n/a}} | |
CoffeeScript
| |
S-Lang
| | | |
Fortran
| | | |
PHP
| | | rowspan=2 {{n/a}} |
Perl
| | |
Raku
| | | |
Ruby
| | | |
Scala
| | | |
Windows PowerShell
| | {{n/a}} | {{n/a}} |
Bash shell
| | {{n/a}} | {{n/a}} |
OCaml
| | rowspan=2| | rowspan=3| |
F#
| |
Standard ML
| | |
Haskell
| | | |
Forth
| | | |
COBOL
| | | |
Mathematica
| | {{n/a}} | {{n/a}} |
- {{note|Pascal's declarations|a}} Pascal has declaration blocks. See functions.
- {{note|variable types|b}}Types are just regular objects, so you can just assign them.
- {{note|Perl's my keyword|c}} In Perl, the "my" keyword scopes the variable into the block.
- {{note|ML ref|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.:= - {{note|Ada declaration|e}} If no initial value is given, an invalid value is automatically assigned (which will trigger a run-time exception if it used before a valid value has been assigned). While this behaviour can be suppressed it is recommended in the interest of predictability. If no invalid value can be found for a type (for example in case of an unconstraint integer type), a valid, yet predictable value is chosen instead.
- {{note|Rust declaration|f}} In Rust, if no initial value is given to a
let
orlet mut
variable and it is never assigned to later, there is an [https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#unused-variables "unused variable" warning]. If no value is provided for aconst
orstatic
orstatic mut
variable, there is an error. There is a [https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html?#non-upper-case-globals "non-upper-case globals"] error for non-uppercaseconst
variables. After it is defined, astatic mut
variable can only be assigned to in anunsafe
block or function.
[[Control flow]]
= [[Conditional (programming)|Conditional]] statements =
class="wikitable sortable" |
valign="top"
! ! if ! else if |
valign="top"
| Ada | | | | |
valign="top"
| Seed7 | | | | |
valign="top"
| Modula-2 | | | rowspan=1 | | |
valign="top"
| ALGOL 68 | | | | |
valign="top"
| ALGOL 68 | | | | |
valign="top"
| APL | | | | |
valign="top"
| rowspan=7| | rowspan=7| | rowspan=7| |
valign="top" |
valign="top"
| C++ (STL) |
valign="top"
| D |
valign="top"
| Java |
valign="top" |
valign="top"
| PHP |
valign="top"
| C# |
| | All non-empty cases must end with a The |
valign="top"
| | | | |
valign="top"
| Go | | | | |
valign="top"
| Swift | | | | |
valign="top"
| Perl | | | |
valign="top"
| Raku | | | | |
valign="top"
| Ruby | | | |
valign="top"
| Scala | | | | |
valign="top"
| | | | |
valign="top"
| | | | |
valign="top"
| Scheme | | | | |
valign="top"
| ISLISP | | | | |
valign="top"
| Pascal | | | |
valign="top"
| rowspan=3| | rowspan=3| | rowspan=3| | |
valign="top"
| rowspan=2| |
valign="top"
| Xojo |
valign="top"
| Python{{ref|python indent|[a]}} | | | Python 3.10+: | Python 2.5+: |
valign="top"
| S-Lang | | | | |
valign="top"
| Fortran | | | | |
valign="top"
| Forth | | | | |
valign="top"
| OCaml | | | rowspan=2| | rowspan=4| |
valign="top"
| F# | Lightweight syntax mode: Either on a single line or with indentation as shown below:
| Lightweight syntax mode: |
valign="top"
| | | |
valign="top"
| | | |
valign="top"
| | | | |
valign="top"
| | | | All conditions are expressions. |
valign="top"
| COBOL | | | | |
valign="top"
| Rust | | | | All conditions are expressions |
valign="top"
! ! if ! else if |
- {{note|python indent|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 is required). The conditional expression syntax does not follow this rule.
- {{note|pattern matching|b}} This is pattern matching and is similar to select case but not the same. It is usually used to deconstruct algebraic data types.
- {{note|pascal semicolon|c}} In languages of the Pascal family, the semicolon is not part of the statement. It is a separator between statements, not a terminator.
- {{note|COBOL END-IF|d}}
END-IF
may be used instead of the period at the end. - {{note|Rust match expression|e}} In Rust, the comma (
,
) at the end of a match arm can be omitted after the last match arm, or after any match arm in which the expression is a block (ends in possibly empty matching brackets{}
).
= [[Control flow#Loops|Loop statements]] =
class="wikitable sortable" | |
! while loop
! foreach | |
---|---|
Ada
| | | | | |
rowspan=2| ALGOL 68 | colspan=3 align=center| «for index» «from first» «by increment» «to last» «while condition» do statements od
| rowspan=2| |
«while condition»{{indent|2}}do statements od
| | | |
APL
| | | | | |
C (C99)
| rowspan=9| | rowspan=9| | rowspan=5| | {{n/a}} | |
Objective-C
| | |
C++ (STL)
| | |
C#
| | |
Java
| | |
JavaScript
| | Since EcmaScript 2015:{{cite web|url=https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...of|title=for...of|work=mozilla.org|access-date=30 January 2017}}
| |
PHP
| | | |
Windows PowerShell
| | | |
D
| | | |
Go
| | | | | |
Swift
| | 2.x: | | | |
Perl
| | | | | |
Raku
| | | | | |
Ruby
| | | | | |
Bash shell
| | {{n/a}} | | | |
Scala
| | | | | |
Smalltalk
| | | | | |
Common Lisp
| | | | | |
Scheme
| | | | | |
ISLISP
| | | | | |
Pascal
| | | | | |
Visual Basic
| rowspan=2| | rowspan=2| |
| | |
Visual Basic .NET
| rowspan=2| | rowspan=2| | |
Xojo
| | | |
Python
| | {{n/a}} | Python 3.x: | | |
S-Lang
| | | | | |
Fortran
| | | | {{n/a}} | |
Forth
| | | | {{n/a}} | |
OCaml
| | {{n/a}} | | | |
F#
| | {{n/a}} | | | |
Standard ML
| | colspan=2 {{n/a}} | | |
Haskell (GHC)
| colspan=2 {{n/a}} | | | |
Eiffel
| colspan=4| | |
CoffeeScript
| | {{n/a}} | | | |
COBOL
| | | | {{n/a}} | |
Rust
| | | | |
- {{note|step|a}} "
step
n" is used to change the loop interval. If "step
" is omitted, then the loop interval is 1. - {{note|Ada_quantifiers|b}} This implements the universal quantifier ("for all" or "") as well as the existential quantifier ("there exists" or "").
- {{note|COBOL THRU|c}}
THRU
may be used instead ofTHROUGH
. - {{note|COBOL GREATER THAN|d}}
«IS» GREATER «THAN»
may be used instead of>
. - {{note|Rust FOREACH|e}} Type of set expression must implement trait
std::iter::IntoIterator
.
= [[Exception handling|Exceptions]] =
{{Further|Exception handling syntax}}
class="wikitable sortable" |
! throw
! handler ! assertion |
---|
Ada
| | | |
APL
| | | |
C (C99)
| | | rowspan=2| |
C++
| rowspan=8| | |
C#
| | |
Java
| | |
JavaScript
| | {{dunno}} |
D
| | |
PHP
| | |
S-Lang
| | {{dunno}} |
Windows PowerShell
| | |
Objective-C
| | | |
Swift
| | | |
Perl
| rowspan=2| | | {{dunno}} |
Raku
| | {{dunno}} |
Ruby
| | | |
Smalltalk
| | | |
Common Lisp
| | | |
Scheme (R6RS)
| | | {{dunno}} |
ISLISP
| | | {{dunno}} |
Pascal
| | | {{dunno}} |
Visual Basic
| | '*** Try class *** Private mstrDescription As String Private mlngNumber As Long Public Sub Catch() mstrDescription = Err.Description mlngNumber = Err.Number End Sub Public Property Get Number() As Long Number = mlngNumber End Property Public Property Get Description() As String Description = mstrDescription | |
Visual Basic .NET
| | | |
Xojo
| | | {{n/a}} |
Python
| | | |
Fortran
| colspan=3 {{n/a}} |
Forth
| | | {{n/a}} |
OCaml
| rowspan=2| | | rowspan=2| |
F#
| |
Standard ML
| | | |
Haskell (GHC)
| | | |
COBOL
| | | {{n/a}} |
Rust
| |
- {{note|common lisp restarts|a}} Common Lisp allows
with-simple-restart
,restart-case
andrestart-bind
to define restarts for use withinvoke-restart
. Unhandled conditions may cause the implementation to show a restarts menu to the user before unwinding the stack. - {{note|Ada uncaught exceptions|b}} Uncaught exceptions are propagated to the innermost dynamically enclosing execution. Exceptions are not propagated across tasks (unless these tasks are currently synchronised in a rendezvous).
= Other control flow statements =
class="wikitable sortable" |
! exit block (break)
! continue ! label ! branch (goto) ! return value from generator |
---|
Ada
| | {{n/a}} | | | {{n/a}} |
ALGOL 68
| | | | | (Callback){{cite web|url=http://rosettacode.org/wiki/Prime_decomposition#ALGOL_68|title=Prime decomposition – Rosetta Code|work=rosettacode.org|access-date=30 January 2017}} |
APL
| | | | | {{n/a}} |
C (C99)
| rowspan=5| | rowspan=5| | rowspan=12| | rowspan=5| | rowspan=4 {{n/a}} |
Objective-C |
C++ (STL) |
D |
C#
| |
Java
| rowspan=2| | rowspan=2| | rowspan=2 {{n/a}} | |
JavaScript
| |
PHP
| | | rowspan=3| | |
Perl
| rowspan=2| | rowspan=2| | |
Raku
| |
Go
| | | | |
Swift
| | | {{n/a}} | |
Bash shell
| | | {{n/a}} | {{n/a}} | {{n/a}} |
Common Lisp
| | | | | |
Scheme
| | | | | |
ISLISP
| | | | | |
Pascal (ISO)
| colspan=2 {{n/a}} | rowspan=2| | rowspan=2| | rowspan=4 {{n/a}} |
Pascal (FPC)
| | |
Visual Basic
| rowspan=3| | {{n/a}} | rowspan=3| | rowspan=3| |
Xojo
| rowspan=2 | |
Visual Basic .NET
| |
Python
| | | colspan=2 {{n/a}} | |
RPG IV
| | | | | |
S-Lang
| | | | | |
Fortran
| | | | | {{n/a}} |
Ruby
| | | | | |
Windows PowerShell
| | | | | |
OCaml
| rowspan=4 colspan=4 {{n/a}} | |
F#
| |
Standard ML
| |
Haskell (GHC)
| |
COBOL
| | | | | {{n/a}} |
- {{note|Pascal's declarations|a}} Pascal has declaration blocks. See functions.
- {{note|Fortran label|b}} label must be a number between 1 and 99999.
[[Subroutine|Function]]s
See reflective programming for calling and declaring functions by strings.
{{sort-under}}
class="wikitable sortable sort-under" |
valign="top"
! ! calling a function ! basic/void function ! value-returning function ! required main function |
valign="top"
| Ada | | | | {{n/a}} |
valign="top"
| ALGOL 68 | | | | {{n/a}} |
valign="top"
| APL | | | | {{n/a}} |
valign="top"
| rowspan=9| | rowspan="5" | | rowspan="5" | | rowspan=3| |
valign="top" |
valign="top"
| C++ (STL) |
valign="top"
| Java | |
valign="top"
| D | |
valign="top"
| C# | Same as above; alternatively, if only one statement:
| Same as above; alternatively, if simple enough to be an expression:
| |
valign="top"
| | | {{n/a}} |
valign="top"
| Go | | | |
valign="top"
| Swift | | | {{n/a}} |
valign="top"
| rowspan=3| | | | rowspan=3 {{n/a}} |
valign="top"
| Scheme | | |
valign="top"
| ISLISP | | |
valign="top"
| Pascal | | | | |
valign="top"
| rowspan=3| | rowspan=3| | | |
valign="top"
| rowspan=2| Same as above; alternatively:
| rowspan=2| |
valign="top"
| Xojo |
valign="top"
| Python | | | | {{n/a}} |
valign="top"
| S-Lang | | | | |
valign="top"
| Fortran | | | | |
valign="top"
| Forth | | | | {{n/a}} |
valign="top"
| PHP | | | | rowspan=4 {{n/a}} |
valign="top"
| Perl | | | |
valign="top"
| Raku | | | |
valign="top"
| Ruby | | | |
valign="top"
| Rust | | | | |
valign="top"
| Scala | | | | |
valign="top"
| | | | rowspan=4 {{n/a}} |
valign="top"
| rowspan=2| Bash shell | rowspan=2| | | |
valign="top"
| colspan=2|
|
valign="top"
| OCaml | rowspan=4| | rowspan=2| | rowspan=2| |
valign="top"
| F# | |
valign="top"
| | | |
valign="top"
| Haskell | | | |
valign="top"
| Eiffel | | | | {{ref|root class and feature|[b]}} |
valign="top"
| rowspan=2| CoffeeScript | | | | rowspan=2 {{n/a}} |
valign="top"
| | | |
valign="top"
| rowspan=2 | COBOL | | | rowspan=2 | | rowspan=2 {{n/a}} |
«FUNCTION» foo«(«parameters»)»
| {{n/a}} |
- {{note|forward declaration|a}} Pascal requires "
forward;
" for forward declarations. - {{note|root class and feature|b}} Eiffel allows the specification of an application's root class and feature.
- {{note|Fortran arguments|c}} In Fortran, function/subroutine parameters are called arguments (since
PARAMETER
is a language keyword); theCALL
keyword is required for subroutines. - {{note|COBOL calling programs|d}} Instead of using
"foo"
, a string variable may be used instead containing the same value.
[[Type conversion]]s{{anchor|Data conversions}}
Where string is a signed decimal number:
class="wikitable sortable sort-under" |
! string to integer
! string to long integer ! string to floating point ! integer to string ! floating point to string |
---|
Ada
| | | | | |
rowspan=3 | ALGOL 68 with general, and then specific formats
| colspan=5 align=center| With prior declarations and association of: |
get(proxy, ivar);
| | | | |
getf(proxy, ($g$, ivar)); or getf(proxy, ($dddd$, ivar));
| | | | |
APL
| | | | | |
C (C99)
| | | | | |
Objective-C
| | | | | |
C++ (STL)
| colspan=3| | colspan=2| |
C++11
| | | | colspan=2| |
C#
| | | | colspan=2| |
D
| | | | colspan=2| |
Java
| | | | | |
JavaScript{{ref|JavaScript's technicalities|[a]}}
| colspan=2| | | colspan=2| |
Go
| | | | | |
Rust{{ref|Rust type conversion|[d]}}
| | | | | |
Common Lisp
| colspan=2| | | colspan=2| |
Scheme
| colspan=3| | colspan=2| |
ISLISP
| colspan=2| | | colspan=2| |
Pascal
| | | | | |
Visual Basic
| | | | | |
Visual Basic .NET (can use both VB syntax above and .NET methods shown right) | | | | colspan="2" | |
Xojo
| | | | colspan=2| |
Python
| | | | colspan=2| |
S-Lang
| | | | colspan=2| |
Fortran
| colspan=3| | colspan=2| |
PHP
| colspan=2| | | colspan=2| |
Perl{{ref|Perl's technicalities|[b]}}
| colspan=3| | colspan=2| |
Raku
| colspan=3| | colspan=2| |
Ruby
| colspan=2| | | colspan=2| |
Scala
| | | | colspan=2| |
Smalltalk
| colspan=2| | | colspan=2| |
Windows PowerShell
| | | | colspan=2| |
OCaml
| | | | | |
F#
| | | | colspan=2| |
Standard ML
| | | | | |
Haskell (GHC)
| colspan=3| | colspan=2| |
COBOL
| colspan=3 | | colspan=2 | |
- {{note|JavaScript's technicalities|a}} JavaScript only uses floating point numbers so there are some technicalities.
- {{note|Perl's technicalities|b}} Perl doesn't have separate types. Strings and numbers are interchangeable.
- {{note|COBOL's NUMVAL alternatives|c}}
NUMVAL-C
orNUMVAL-F
may be used instead ofNUMVAL
. - {{note|Rust type conversion}} [https://doc.rust-lang.org/stable/std/primitive.str.html#method.parse
str::parse
] is available to convert any type that has an implementation of the [https://doc.rust-lang.org/stable/std/str/trait.FromStr.htmlstd::str::FromStr
] trait. Bothstr::parse
and [https://doc.rust-lang.org/stable/std/str/trait.FromStr.html#tymethod.from_strFromStr::from_str
] return aResult
that contains the specified type if there is no error. The turbofish (
) on::<_> str::parse
can be omitted if the type can be inferred from context.
[[Standard streams|Standard stream I/O]]
class="wikitable sortable" |
rowspan=2|
! read from ! colspan=2| write to |
---|
stdin
! stdout ! stderr |
Ada
| | | |
ALGOL 68
| | | |
APL
| | | |
C (C99)
| | | |
Objective-C
| | | |
[[C++
| | | |
C#
| | | |
D
| | | |
Java
| | | |
Go
| | | |
Swift
| | | |
JavaScript Web Browser implementation | | | |
JavaScript Active Server Pages | | | |
JavaScript Windows Script Host | | | |
Common Lisp
| | | |
Scheme (R6RS)
| | | |
ISLISP
| | | |
Pascal
| | | |
Visual Basic
| | | {{n/a}} |
Visual Basic .NET
| | | |
Xojo
| | | |
Python 2.x
| | | |
Python 3.x
| | | |
S-Lang
| | | |
Fortran
| | | |
Forth
| | | {{n/a}} |
PHP
| | | |
Perl
| | | |
Raku
| | | |
Ruby
| | | |
Windows PowerShell
| | | |
OCaml
| | | |
F#
| |
Standard ML
| | | |
Haskell (GHC)
| | | |
COBOL
| | | |
- {{note|ALGOL Unformatted|a}} ALGOL 68 additionally as the "unformatted" transput routines:
read
,write
,get
, andput
. - {{note|more c input|b}}
gets(x)
andfgets(x, length, stdin)
read unformatted text from stdin. Use of gets is not recommended. - {{note|more c input|c}}
puts(x)
andfputs(x, stdout)
write unformatted text to stdout. - {{note|more c error output|d}}
fputs(x, stderr)
writes unformatted text to stderr - {{note|Fortran standard units|e}} {{mono|INPUT_UNIT, OUTPUT_UNIT, ERROR_UNIT}} are defined in the {{mono|ISO_FORTRAN_ENV}} module.{{cite web|url=http://fortranwiki.org/fortran/show/iso_fortran_env|title=iso_fortran_env in Fortran Wiki|work=fortranwiki.org|access-date=30 January 2017}}
Reading [[command-line argument]]s
class="wikitable sortable" |
! Argument values
! Argument counts ! Program name / Script name |
---|
Ada
| | | |
C (C99)
| rowspan=3| | rowspan=3| | rowspan=3| first argument |
Objective-C |
C++ |
C#
| rowspan=3| | | |
Java
| rowspan=2| | |
D
| first argument |
JavaScript Windows Script Host implementation | | | |
Go
| | | first argument |
Rust{{ref|Rust args|[a]}}
| | | |
Swift
| | | first argument |
Common Lisp
| {{dunno}} | {{dunno}} | {{dunno}} |
Scheme (R6RS)
| | | first argument |
ISLISP
| {{n/a}} | {{n/a}} | {{n/a}} |
Pascal
| | | first argument |
Visual Basic
| | {{n/a}} | |
Visual Basic .NET
| | | |
Xojo
| | (string parsing) | |
Python
| | | first argument |
S-Lang
| | | first argument |
Fortran
| | | |
PHP
| | | first argument |
Bash shell
| | | |
Perl
| | | |
Raku
| | | |
Ruby
| | | |
Windows PowerShell
| | | |
OCaml
| | | first argument |
F#
| | | |
Standard ML
| {{code|lang=sml|List.nth (CommandLine.arguments (), n)}} | {{code|lang=sml|length (CommandLine.arguments ())}} | {{code|lang=sml|CommandLine.name ()}} |
Haskell (GHC)
| {{code|lang=haskell|do { args <- System.getArgs; return length args !! n }}} | {{code|lang=haskell|do { args <- System.getArgs; return length args }}} | |
COBOL
| colspan=2 | {{ref|COBOL Arguments|[c]}} | {{n/a}} |
- {{note|Rust args|a}} In Rust,
std::env::args
andstd::env::args_os
return iterators,std::env::Args
andstd::env::ArgsOs
respectively.Args
converts each argument to aString
and it panics if it reaches an argument that cannot be converted to UTF-8.ArgsOs
returns a non-lossy representation of the raw strings from the operating system (std::ffi::OsString
), which can be invalid UTF-8. - {{note|unseparated|b}} In Visual Basic, command-line arguments are not separated. Separating them requires a split function
Split(string)
. - {{note|COBOL Arguments|c}} The COBOL standard includes no means to access command-line arguments, but common compiler extensions to access them include defining parameters for the main program or using
ACCEPT
statements.
Execution of commands
class="wikitable sortable"
! !Shell command !Execute program |
Ada
| colspan=3 align="center"| Not part of the language standard. Commonly done by compiler provided packages or by interfacing to C or POSIX.{{cite web|url=http://rosettacode.org/wiki/Execute_a_system_command#Ada|title=Execute a system command – Rosetta Code|work=rosettacode.org|access-date=30 January 2017}} |
C
| rowspan=3| | |
C++
| |
Objective-C
| | |
C#
| | rowspan=2| | |
F#
| | |
Go
| | | |
Visual Basic
| | | |
Visual Basic .NET
| | | |
Xojo
| | | {{n/a}} |
D
| | | |
Java
| | | |
JavaScript Windows Script Host implementation | | | |
Common Lisp
| | | |
Scheme
| | | |
ISLISP
| {{n/a}} | {{n/a}} | {{n/a}} |
Pascal
| | | |
OCaml
| | | |
Standard ML
| | | |
Haskell (GHC)
| | | |
Perl
| | | |
Ruby
| | | |
PHP
| | | |
Python
| | | |
S-Lang
| | | |
Fortran
| | | |
Windows PowerShell
| | | |
Bash shell
| | | |
{{note|Fortran 2008|a}} Fortran 2008 or newer.{{cite web|url=https://gcc.gnu.org/onlinedocs/gfortran/EXECUTE_005fCOMMAND_005fLINE.html|title=EXECUTE_COMMAND_LINE – The GNU Fortran Compiler|work=gnu.org|access-date=30 January 2017}}