Comparison of programming languages (basic instructions)
Comparison of programming languages is a common topic of discussion among software engineers. Basic instructions of several programming languages are compared here.
Conventions of this article
The bold is the literal code. The non-bold is interpreted by the reader. Statements in guillemets are optional. indicates a necessary indent.Type identifiers
Integer">Integer (computer science)">Integers
The standard constantsint shorts
and int lengths
can be used to determine how many 'short
's and 'long
's can be usefully prefixed to 'short int
' and 'long int
'. The actually size of the 'short int
', 'int
' and 'long int
' is available as constants short max int
, max int
and long max int
etc.Commonly used for characters.
The ALGOL 68, C and C++ languages do not specify the exact width of the integer types
short
, int
, long
, and long long
, so they are implementation-dependent. In C and C++ 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. C99 and C++11 also define the intN_t
exact-width types in the stdint.h header. SeeC syntax#Integral types for more information. In addition the types size_t
and ptrdiff_t
are defined in relation to the address size to hold unsigned and signed integeres 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 "n" is provided by the
SELECTED_INT_KIND
intrinsic function.ALGOL 68G's run time option
--precision "number"
can set precision for long long int
s to the required "number" significant digits. The standard constants long long int width
and long long max int
can 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_Integer
and Long_Integer
are also commonly predefined, but not required by the Ada standard. Run time 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.
int
in PHP has the same width as long
type 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 constantsreal shorts
and real lengths
can be used to determine how many 'short
's and 'long
's can be usefully prefixed to 'short real
' and 'long real
'. The actually size of the 'short real
', 'real
' and 'long real
' is available as constants short max real
, max real
and long max real
etc. With the constants short small real
, small real
and long small real
available for each type's machine epsilon.declarations of single precision often are not honored
The value of "n" is provided by the
SELECTED_REAL_KIND
intrinsic function.ALGOL 68G's run time option
--precision "number"
can set precision for long long real
s to the required "number" significant digits. The standard constants long long real width
and 'long long max real
can be used to determine actual precision.These IEEE floating-point types will be introduced in the next COBOL standard.
Same size as '
double
' on many implementations.Swift supports 80-bit extended precision floating point type, equivalent to
long double
in C languages.[Complex number]s
The value of "n" is provided by theSELECTED_REAL_KIND
intrinsic 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
TrueClass
evaluates to true and everything in FalseClass
evaluates 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 "n" is provided by the
SELECTED_INT_KIND
intrinsic 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
x
" works in Java, however "type x
" is 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 ON
clause 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.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.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 Comparison of programming languages #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.
[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-IF
may be used instead of the period at the end.Loop statements">Control flow#Loops">Loop statements
"step
n" 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.
THRU
may be used instead of THROUGH
.«IS» GREATER «THAN»
may be used instead of >
.Exceptions">Exception handling">Exceptions
Common Lisp allowswith-simple-restart
, restart-case
and restart-bind
to define restarts for use with invoke-restart
. Unhandled conditions may cause the implementation to show a restarts menu to the user before unwinding the stack.Uncaught exceptions are propagated to the innermost dynamically enclosing execution. Exceptions are not propagated across tasks.
Other control flow statements
Pascal has declaration blocks. See Comparison of programming languages #Functions.label must be a number between 1 and 99999.
Functions">Subroutine">Functions
See reflection for calling and declaring functions by strings.Pascal requires "
forward;
" for forward declarations.Eiffel allows the specification of an application's root class and feature.
In Fortran, function/subroutine parameters are called arguments ; the
CALL
keyword is required for subroutines.Instead of using
"foo"
, a string variable may be used instead containing the same value.[Type conversion]s
Where string is a signed decimal number:JavaScript only uses floating point numbers so there are some technicalities.
Perl doesn't have separate types. Strings and numbers are interchangeable.
NUMVAL-C
or NUMVAL-F
may be used instead of NUMVAL
.Standard stream I/O">Standard streams">Standard stream I/O
ALGOL 68 additionally as the "unformatted" transput routines:read, write, get
and put
.gets
and fgets
read unformatted text from stdin. Use of gets is not recommended.puts
and fputs
write unformatted text to stdout.fputs
writes unformatted text to stderrINPUT_UNIT, OUTPUT_UNIT, ERROR_UNIT
are defined in the ISO_FORTRAN_ENV
module.Reading [command-line argument]s
- In Visual Basic, command-line arguments are not separated. Separating them requires a split function
Split
. - 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