Comparison of programming languages (basic instructions)
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.
Conventions of this article
Generally, var,, or is how variable names or other non-literal values to be interpreted by the reader are represented. The rest is literal code. Guillemets enclose optional sections. indicates a necessary 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.
Type identifiers
Integer">Integer (computer science)">Integers
- The standard constants
int shortsandint lengthscan be used to determine how manyshorts andlongs can be usefully prefixed toshort intandlong int. The actual sizes ofshort int,int, andlong intare available as the constantsshort max int,max int, andlong max intetc. - Commonly used for characters.
- The ALGOL 68, C and C++ languages do not specify the exact width of the integer types
short,int,long, andlong long, so they are implementation-dependent. In C and C++short,long, andlong longtypes are required to be at least 16, 32, and 64 bits wide, respectively, but can be more. Theinttype is required to be at least as wide asshortand at most as wide aslong, and is typically the width of the word size on the processor of the machine. C99 and C++11 also define theintN_texact-width types in the stdint.h header. See C syntax#Integral types for more information. In addition the typessize_tandptrdiff_tare defined in relation to the address size to hold unsigned and signed integers sufficiently large to handle array indices and the difference between pointers. - Perl 5 does not have distinct types. Integers, floating point numbers, strings, etc. are all considered "scalars".
- PHP has two arbitrary-precision libraries. The BCMath library just uses strings as datatype. The GMP library uses an internal "resource" type.
- The value of
nis provided by theSELECTED_INT_KINDintrinsic function. - ALGOL 68G's runtime option
--precision "number"can set precision forlong long ints to the required "number" significant digits. The standard constantslong long int widthandlong long max intcan be used to determine actual precision. - 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. - 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 and one supporting arbitrary sized integers. Arithmetic operations support polymorphic arguments and return the result in the most appropriate compact representation.
- Ada range types are checked for boundary violations at run-time. Run-time boundary violations raise a "constraint error" exception. Ranges are not restricted to powers of two. Commonly predefined Integer subtypes are: Positive and Natural.
Short_Short_Integer,Short_IntegerandLong_Integerare also commonly predefined, but not required by the Ada standard. Runtime checks can be disabled if performance is more important than integrity checks. - Ada modulo types implement modulo arithmetic in all operations, i.e. no range violations are possible. Modulos are not restricted to powers of two.
- Commonly used for characters like Java's char.
intin PHP has the same width aslongtype in C has on that system. - Erlang is dynamically typed. The type identifiers are usually used to specify types of record fields and the argument and return types of [|functions].
- When it exceeds one word.
[Floating point]
- The standard constants
real shortsandreal lengthscan be used to determine how manyshorts andlongs can be usefully prefixed toshort realandlong real. The actual sizes ofshort real,real, andlong realare available as the constantsshort max real,max realandlong max realetc. With the constantsshort small real,small realandlong small realavailable for each type's machine epsilon. - declarations of single precision often are not honored
- The value of
nis provided by theSELECTED_REAL_KINDintrinsic function. - ALGOL 68G's runtime option
--precision "number"can set precision forlong long reals to the required "number" significant digits. The standard constantslong long real widthandlong long max realcan be used to determine actual precision. - These IEEE floating-point types will be introduced in the next COBOL standard.
- Same size as
doubleon many implementations. - Swift supports 80-bit extended precision floating point type, equivalent to
long doublein C languages.
[Complex number]s
- The value of
nis provided by theSELECTED_REAL_KINDintrinsic function. - Generic type which can be instantiated with any base floating point type.
Other variable types
- specifically, strings of arbitrary length and automatically managed.
- This language represents a boolean as an integer where false is represented as a value of zero and true by a non-zero value.
- All values evaluate to either true or false. Everything in
TrueClassevaluates to true and everything inFalseClassevaluates to false. - This language does not have a separate character type. Characters are represented as strings of length 1.
- Enumerations in this language are algebraic types with only nullary constructors
- The value of
nis provided by theSELECTED_INT_KINDintrinsic function.
Derived types
Array">Array data type">Array
- In most expressions, 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.
- The C-like
type xworks in Java, howevertype xis the preferred form of array declaration. - Subranges are used to define the bounds of the array.
- JavaScript's array are a special kind of object.
- The
DEPENDING ONclause in COBOL does not create a true variable length array and will always allocate the maximum size of the array.
Other types
- Only classes are supported.
structs 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. - pair only
- Although Perl doesn't have records, because Perl's type system allows different data types to be in an array, "hashes" that don't have a variable index would effectively be the same as records.
- Enumerations in this language are algebraic types with only nullary constructors
Variable and constant declarations
- Pascal has declaration blocks. See functions.
- Types are just regular objects, so you can just assign them.
- In Perl, the "my" keyword scopes the variable into the block.
- 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 no initial value is given, an invalid value is automatically 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, a valid, yet predictable value is chosen instead.
- In Rust, if no initial value is given to a
letorlet mutvariable and it is never assigned to later, there is an . If no value is provided for aconstorstaticorstatic mutvariable, there is an error. There is a error for non-uppercaseconstvariables. After it is defined, astatic mutvariable can only be assigned to in anunsafeblock or function.
[Control flow]
Conditional">Conditional (programming)">Conditional statements
- 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 conditional expression syntax does not follow this rule.
- This is pattern matching and is similar to select case but not the same. It is usually used to deconstruct algebraic data types.
- In languages of the Pascal family, the semicolon is not part of the statement. It is a separator between statements, not a terminator.
END-IFmay be used instead of the period at the end. - 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.
Loop statements">Control flow">Loop statements
- "
stepn" is used to change the loop interval. If "step" is omitted, then the loop interval is 1. - This implements the universal quantifier as well as the existential quantifier.
THRUmay be used instead ofTHROUGH. - may be used instead of
>. - Type of set expression must implement trait
std::iter::IntoIterator.
Exceptions">Exception handling">Exceptions
| throw | handler | assertion | |
| Ada | raise exception_name «with string_expression» | beginstatements | |
| APL | «string_expression» ⎕SIGNAL number_expression | :Trap number«s»_expression'statements | «string_expression» ''condition |
| C | longjmp; | switch | assert; |
| C++ | throw exception; | try catch «» ... | assert; |
| C# | throw exception; | try catch «» ... «finally » | System.Diagnostics.Debug.Assert;or System.Diagnostics.Trace.Assert; |
| Java | throw exception; | try catch ... «finally » | assert condition «: description»; |
| JavaScript | throw exception; | try catch «finally » | |
| D | throw exception; | try catch ... «finally » | assert; |
| PHP | throw exception; | try catch ... «finally » | assert |
| Objective-C | @throw exception; | @try @catch ... «@finally » | NSAssert; |
| Swift | throw exception | do catch exception ... | assert |
| Perl | die exception''; | eval |