Comparison of programming languages (basic instructions)
Template:Short description Template:Refimprove Template: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.
Conventions of this article
Generally, var, Template:Var, or Template:Var serif is how variable names or other non-literal values to be interpreted by the reader are represented. The rest is literal code. Guillemets (« and ») enclose optional sections. Template:Keypress 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.
Type identifiers
Integers
| 8 bit (byte) | 16 bit (short integer) | 32 bit | 64 bit (long integer) | Word size | Arbitrarily precise (bignum) | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Signed | Unsigned | Signed | Unsigned | Signed | Unsigned | Signed | Unsigned | Signed | Unsigned | ||
| Ada[1] | range -2**7 .. 2**7 - 1[j]
|
range 0 .. 2**8 - 1[j] or mod 2**8[k]
|
range -2**15 .. 2**15 - 1[j]
|
range 0 .. 2**16 - 1[j] or mod 2**16[k]
|
range -2**31 .. 2**31 - 1[j]
|
range 0 .. 2**32 - 1[j] or mod 2**32[k]
|
range -2**63 .. 2**63 - 1[j]
|
mod 2**64[k]
|
Integer[j]
|
range 0 .. 2**Integer'[j] or mod Integer'[k]
|
— |
| ALGOL 68 (variable-width) | short short int[c]
|
— | short int[c]
|
— | int[c]
|
— | long int[c]
|
— | int[c]
|
— | long long int[a][g]
|
bytes and bits
| |||||||||||
| C (C99 fixed-width) | int8_t
|
uint8_t
|
int16_t
|
uint16_t
|
int32_t
|
uint32_t
|
int64_t
|
uint64_t
|
intptr_t[c]
|
size_t[c]
|
— |
| C++ (C++11 fixed-width) | |||||||||||
| C (C99 variable-width) | signed char
|
unsigned char
|
short[c]
|
unsigned short[c]
|
long[c]
|
unsigned long[c]
|
long long[c]
|
unsigned long long[c]
|
int[c]
|
unsigned int[c]
| |
| C++ (C++11 variable-width) | |||||||||||
| Objective-C (Cocoa) | signed char or int8_t
|
unsigned char or uint8_t
|
short or int16_t
|
unsigned short or uint16_t
|
int or int32_t
|
unsigned int or uint32_t
|
long long or int64_t
|
unsigned long long or uint64_t
|
NSInteger or long
|
NSUInteger or unsigned long
| |
| C# | sbyte
|
byte
|
short
|
ushort
|
int
|
uint
|
long
|
ulong
|
IntPtr
|
UIntPtr
|
System.Numerics.(.NET 4.0) |
| Java | byte
|
— | char[b]
|
— | — | — | — | java.math.
| |||
| Go | int8
|
uint8 or byte
|
int16
|
uint16
|
int32
|
uint32
|
int64
|
uint64
|
int
|
uint
|
big.Int
|
| Rust | i8
|
u8
|
i16
|
u16
|
i32
|
u32
|
i64
|
u64
|
isize
|
usize
|
— |
| Swift | Int8
|
UInt8
|
Int16
|
UInt16
|
Int32
|
UInt32
|
Int64
|
UInt64
|
Int
|
UInt
| |
| D | byte
|
ubyte
|
short
|
ushort
|
int
|
uint
|
long
|
ulong
|
— | — | BigInt
|
| Common Lisp[2] | (signed-byte 8)
|
(unsigned-byte 8)
|
(signed-byte 16)
|
(unsigned-byte 16)
|
(signed-byte 32)
|
(unsigned-byte 32)
|
(signed-byte 64)
|
(unsigned-byte 64)
|
bignum
| ||
| Scheme | |||||||||||
| ISLISP[3] | bignum
| ||||||||||
| Pascal (FPC) | shortint
|
byte
|
smallint
|
word
|
longint
|
longword
|
int64
|
qword
|
integer
|
cardinal
|
— |
| Visual Basic | — | Byte
|
Integer
|
— | Long
|
— | — | — | — | ||
| Visual Basic .NET | SByte
|
Short
|
UShort
|
Integer
|
UInteger
|
Long
|
ULong
|
System.Numerics(.NET 4.0) | |||
| FreeBasic | Byte or Integer<8>
|
UByte or UInteger<8>
|
Short or Integer<16>
|
UShort or UInteger<16>
|
Long or Integer<32>
|
ULong or UInteger<32>
|
LongInt or Integer<64>
|
ULongInt or UInteger<64>
|
Integer
|
UInteger
|
— |
| Python 2.x | — | — | — | — | int
|
— | long
| ||||
| Python 3.x | — | — | — | — | — | int
| |||||
| S-Lang | — | — | — | — | — | — | |||||
| Fortran | INTEGER[f]
|
— | INTEGER[f]
|
— | INTEGER[f]
|
— | INTEGER[f]
|
— | |||
| PHP | — | — | int[m]
|
— | int[m]
|
— | — | [e] | |||
| Perl 5 | —[d] | —[d] | —[d] | —[d] | —[d] | Math::BigInt
| |||||
| Raku | int8
|
uint8
|
int16
|
uint16
|
int32
|
uint32
|
int64
|
uint64
|
Int
|
— | |
| Ruby | — | — | — | — | Fixnum
|
— | Bignum
| ||||
| Erlang[n] | — | — | — | — | integer()
|
— | integer()[o]
| ||||
| Scala | Byte
|
— | Short
|
Char[l]
|
Int
|
— | Long
|
— | — | — | scala.math.BigInt
|
| Seed7 | — | — | — | — | — | — | integer
|
— | — | — | bigInteger
|
| Smalltalk | — | — | — | — | SmallInteger[i]
|
— | LargeInteger[i]
| ||||
| Windows PowerShell | — | — | — | — | — | — | |||||
| OCaml | — | — | int32
|
— | int64
|
— | int or nativeint
|
open Big_int;; or big_int
| |||
| F# | sbyte
|
byte
|
int16
|
uint16
|
int32 or int
|
uint32
|
uint64
|
nativeint
|
unativeint
|
bigint
| |
| Standard ML | — | Word8.word
|
— | Int32.int
|
Word32.word
|
Int64.int
|
Word64.word
|
int
|
word
|
LargeInt.int orIntInf.int
| |
| Haskell (GHC) | «import Int» or Int8
|
«import Word» or Word8
|
«import Int» or Int16
|
«import Word» or Word16
|
«import Int» or Int32
|
«import Word» or Word32
|
«import Int» or Int64
|
«import Word» or Word64
|
Int
|
«import Word» or Word
|
Integer
|
| Eiffel | INTEGER_8
|
NATURAL_8
|
INTEGER_16
|
NATURAL_16
|
INTEGER_32
|
NATURAL_32
|
INTEGER_64
|
NATURAL_64
|
INTEGER
|
NATURAL
|
— |
| COBOL[h] | BINARY-CHAR «SIGNED»
|
BINARY-CHAR UNSIGNED
|
BINARY-SHORT «SIGNED»
|
BINARY-SHORT UNSIGNED
|
BINARY-LONG «SIGNED»
|
BINARY-LONG UNSIGNED
|
BINARY-DOUBLE «SIGNED»
|
BINARY-DOUBLE UNSIGNED
|
— | — | — |
| Mathematica | — | — | — | — | — | Integer
| |||||
| Wolfram Language | — | — | — | — | — | Integer
| |||||
- <templatestyles src="Citation/styles.css"/>^a 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. - <templatestyles src="Citation/styles.css"/>^b Commonly used for characters.
- <templatestyles src="Citation/styles.css"/>^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 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 (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++11Script error: No such module "Unsubst". also define the[u]intN_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. - <templatestyles src="Citation/styles.css"/>^d Perl 5 does not have distinct types. Integers, floating point numbers, strings, etc. are all considered "scalars".
- <templatestyles src="Citation/styles.css"/>^e PHP has two arbitrary-precision libraries. The BCMath library just uses strings as datatype. The GMP library uses an internal "resource" type.
- <templatestyles src="Citation/styles.css"/>^f The value of
nis provided by theSELECTED_INT_KIND[4] intrinsic function. - <templatestyles src="Citation/styles.css"/>^g 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. - <templatestyles src="Citation/styles.css"/>^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. - <templatestyles src="Citation/styles.css"/>^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 (Template:Mono) and one supporting arbitrary sized integers (Template:Mono). Arithmetic operations support polymorphic arguments and return the result in the most appropriate compact representation.
- <templatestyles src="Citation/styles.css"/>^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. - <templatestyles src="Citation/styles.css"/>^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.
- <templatestyles src="Citation/styles.css"/>^l Commonly used for characters like Java's char.
- <templatestyles src="Citation/styles.css"/>^m
intin PHP has the same width aslongtype in C has on that system.[c] - <templatestyles src="Citation/styles.css"/>^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.[5]
- <templatestyles src="Citation/styles.css"/>^o When it exceeds one word.[6]
Floating point
| Single precision | Double precision | Other precision | Processor dependent | |
|---|---|---|---|---|
| Ada[1] | Float
|
Long_Float
|
— | |
| ALGOL 68 | real[a]
|
long real[a]
|
short real, long long real, etc.[d]
| |
| C | float[b]
|
double
|
long double[f]
| |
| C++ (STL) | ||||
| Objective-C (Cocoa) | CGFloat
| |||
| C# | float
|
— | ||
| Java | ||||
| Go | float32
|
float64
| ||
| Rust | f32
|
f64
|
f16, f128
| |
| Swift | Float or Float32
|
Double or Float64
|
Float80[g]
|
CGFloat
|
| D | float
|
double
|
real
| |
| Common Lisp | single-float
|
double-float
|
float, short-float, long-float
| |
| Scheme | ||||
| ISLISP | ||||
| Pascal (FPC) | single
|
double
|
real
| |
| Visual Basic | Single
|
Double
|
— | |
| Visual Basic .NET | ||||
| Xojo | ||||
| Python | — | float
|
||
| JavaScript | Number[7]
|
— | ||
| S-Lang | ||||
| Fortran | REAL(KIND = n)[c]
|
|||
| PHP | float
|
|||
| Perl | ||||
| Raku | num32
|
num64
|
Num
| |
| Ruby | — | Float
|
— | |
| Scala | Float
|
Double
| ||
| Seed7 | — | float
| ||
| Smalltalk | Float
|
Double
| ||
| Windows PowerShell | ||||
| OCaml | — | float
|
— | |
| F# | float32
| |||
| Standard ML | — | real
| ||
| Haskell (GHC) | Float
|
Double
|
||
| Eiffel | REAL_32
|
REAL_64
|
||
| COBOL | FLOAT-BINARY-7[e]
|
FLOAT-BINARY-34[e]
|
FLOAT-SHORT, FLOAT-LONG, FLOAT-EXTENDED
| |
| Mathematica | — | — | Real
| |
- <templatestyles src="Citation/styles.css"/>^a 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. - <templatestyles src="Citation/styles.css"/>^b declarations of single precision often are not honored
- <templatestyles src="Citation/styles.css"/>^c The value of
nis provided by theSELECTED_REAL_KIND[8] intrinsic function. - <templatestyles src="Citation/styles.css"/>^d 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. - <templatestyles src="Citation/styles.css"/>^e These IEEE floating-point types will be introduced in the next COBOL standard.
- <templatestyles src="Citation/styles.css"/>^f Same size as
doubleon many implementations. - <templatestyles src="Citation/styles.css"/>^g Swift supports 80-bit extended precision floating point type, equivalent to
long doublein C languages.
Complex numbers
| Integer | Single precision | Double precision | Half and Quadruple precision etc. | |
|---|---|---|---|---|
| Ada[1] | — | Complex[b]
|
Complex[b]
|
Complex[b]
|
| ALGOL 68 | — | compl
|
long compl etc.
|
short compl etc. and long long compl etc.
|
| C (C99)[9] | — | float complex
|
double complex
|
— |
| C++ (STL) | — | std::complex<float>
|
std::complex<double>
| |
| C# | — | — | System.Numerics.Complex(.NET 4.0) | |
| Java | — | — | — | |
| Go | — | complex64
|
complex128
| |
| D | — | cfloat
|
cdouble
| |
| Objective-C | — | — | — | |
| Common Lisp | (complex integer) | (complex single-float) | (complex double-float) | complex |
| Scheme | — | |||
| Pascal | — | — | ||
| Visual Basic | — | — | ||
| Visual Basic .NET | — | — | System.Numerics.Complex(.NET 4.0) | |
| Perl | Math::Complex
|
|||
| Raku | complex64
|
complex128
|
Complex
| |
| Python | complex
|
— | ||
| JavaScript | — | — | ||
| S-Lang | — | — | ||
| Fortran | COMPLEX(KIND = n)[a]
| |||
| Ruby | Complex
|
— | Complex
| |
| Scala | — | — | — | |
| Seed7 | — | — | complex
| |
| Smalltalk | Complex
|
Complex
|
Complex
| |
| Windows PowerShell | — | — | ||
| OCaml | — | — | Complex.t
| |
| F# | System.Numerics.Complex(.NET 4.0) | |||
| Standard ML | — | — | — | |
| Haskell (GHC) | — | ComplexTemplate:Not a typoComplex Float
|
ComplexTemplate:Not a typoComplex Double
| |
| Eiffel | — | — | — | |
| COBOL | — | — | — | |
| Mathematica | Complex
|
— | — | Complex
|
- <templatestyles src="Citation/styles.css"/>^a The value of
nis provided by theSELECTED_REAL_KIND[8] intrinsic function. - <templatestyles src="Citation/styles.css"/>^b Generic type which can be instantiated with any base floating point type.
Other variable types
| Text | Boolean | Enumeration | Object/Universal | ||
|---|---|---|---|---|---|
| Character | String[a] | ||||
| Ada[1] | Character
|
String, Bounded_String, Unbounded_String
|
Boolean
|
(item1, item2, ...)
|
tagged null record
|
| ALGOL 68 | char
|
string, bytes
|
bool, bits
|
— - User defined | — |
| C (C99) | char, wchar_t
|
— | bool[b]
|
enum «name» { item1, item2, ... };
|
void *
|
| C++ (STL) | «std::»string
| ||||
| Objective-C | unichar
|
NSString *
|
BOOL
|
id
| |
| C# | char
|
string
|
bool
|
enum name { item1« = value», item2« = value», ... }
|
object |
| Java | String
|
boolean
|
enum name { item1, item2, ... }
|
Object
| |
| Go | byte, rune
|
string
|
bool
|
const (
|
interface{}
|
| Rust | char
|
String
|
bool
|
enum name { item1« = value», item2« = value», ... }
|
std::any::Any
|
| Swift | Character
|
String
|
Bool
|
enum name { case item1, item2, ... }
|
Any
|
| D | char
|
string
|
bool
|
enum name { item1, item2, ... }
|
std.variant.Variant
|
| Common Lisp | character
|
string
|
boolean
|
(member item1 item2 ...)
|
t
|
| Scheme | |||||
| ISLISP | |||||
| Pascal (ISO) | char
|
— | boolean
|
( item1, item2, ... )
|
— |
| Object Pascal (Delphi) | string
|
variant
| |||
| Visual Basic | — | String
|
Boolean
|
Enum name
|
[[Variant type|Variant]]
|
| Visual Basic .NET | Char
|
Object
| |||
| Xojo | — | Object or Variant
| |||
| Python | —[d] | str
|
bool
|
from enum import Enum
|
object
|
| JavaScript | —[d] | String
|
Boolean
|
Object
| |
| S-Lang | |||||
| Fortran | CHARACTER(LEN = *)
|
CHARACTER(LEN = :), allocatable
|
LOGICAL(KIND = n)[f]
|
CLASS(*)
| |
| PHP | —[d] | string
|
bool
|
(type declaration omitted) | |
| Perl | —[d] | UNIVERSAL
| |||
| Raku | Char
|
Str
|
Bool
|
enum name<item1 item2 ...>enum name <<:item1(value) :item2(value) ..>>
|
Mu
|
| Ruby | —[d] | String
|
Object[c]
|
Object
| |
| Scala | Char
|
String
|
Boolean
|
object name extends Enumeration {
|
Any
|
| Seed7 | char
|
string
|
boolean
|
const type: name is new enum
|
|
| Windows PowerShell | |||||
| OCaml | char
|
string
|
bool
|
—[e] | — |
| F# | item2 = value | ... | obj
| |||
| Standard ML | —[e] | — | |||
| Haskell (GHC) | Char
|
String
|
Bool
|
—[e] | — |
| Eiffel | CHARACTER
|
STRING
|
BOOLEAN
|
— | ANY
|
| COBOL | PIC X
|
PIC X(string length) or PIC X«X...»
|
PIC 1«(number of digits)» or PIC 1«1...»
|
— | OBJECT REFERENCE
|
| Mathematica | —[d] | String
|
— | ||
- <templatestyles src="Citation/styles.css"/>^a specifically, strings of arbitrary length and automatically managed.
- <templatestyles src="Citation/styles.css"/>^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.
- <templatestyles src="Citation/styles.css"/>^c All values evaluate to either true or false. Everything in
TrueClassevaluates to true and everything inFalseClassevaluates to false. - <templatestyles src="Citation/styles.css"/>^d This language does not have a separate character type. Characters are represented as strings of length 1.
- <templatestyles src="Citation/styles.css"/>^e Enumerations in this language are algebraic types with only nullary constructors
- <templatestyles src="Citation/styles.css"/>^f The value of
nis provided by theSELECTED_INT_KIND[4] intrinsic function.
Derived types
Array
Script error: No such module "labelled list hatnote".
| fixed size array | dynamic size array | |||
|---|---|---|---|---|
| one-dimensional array | multidimensional array | one-dimensional array | multidimensional array | |
| Ada[1] | array (<first> .. <last>) of <type>or array (<discrete_type>) of <type>
|
array (<first1> .. <last1>, <first2> .. <last2>, ...) of <type>or array (<discrete_type1>, <discrete_type2>, ...) of <type>
|
array (<discrete_type> range <>) of <type>
|
array (<discrete_type1> range <>, <discrete_type2> range <>, ...) of <type>
|
| ALGOL 68 | [first:last]or simply: [size]
|
[first1:last1, first2:last2]or [first1:last1][first2:last2]etc. |
flex[first:last]or simply: flex[size]
|
flex[first1:last1, first2:last2]or flex[first1:last1]
|
| C (C99) | type name[size][a]
|
type name[size1][size2][a]
|
type *nameor within a block: int n = ...; type name[n]
|
|
| C++ (STL) | «std::»array<type, size>(C++11)
|
«std::»vector<type>
| ||
| C# | type[]
|
type[[[:Template:Not a typo]]...]
|
Systemor System
|
|
| Java | type[][b]
|
type[][]...[b]
|
ArrayList or ArrayList<type>
|
|
| D | type[size]
|
type[size1][size2]
|
type[]
|
|
| Go | [size]type
|
[size1][size2]...type
|
[]type
|
[][]type
|
| Rust | [type; size]
|
[[type; size1]; size2]
|
Vec<type>
|
Vec<Vec<type>>
|
| Swift | [type] or Array<type>
|
[[type]] or Array<Array<type>>
| ||
| Objective-C | NSArray
|
NSMutableArray
|
||
| JavaScript | — | — | Array[d]
| |
| Common Lisp | (simple-array type (dimension))
|
(simple-array type (dimension1 dimension2))
|
(array type (dimension))
|
(array type (dimension1 dimension2))
|
| Scheme | ||||
| ISLISP | ||||
| 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]
|
— | — |
| Object Pascal (Delphi) | array of type
|
array of array ... of type
| ||
| Visual Basic | Dim x(last) As type
|
Dim x(last1, last2,...) As type
|
||
| Visual Basic .NET | type()
|
type(Template:Not a typo...)
|
Systemor System
|
|
| Python | list
|
|||
| S-Lang | x = type[size];
|
x = type[size1, size2, ...];
|
||
| Fortran | type :: name(size)
|
type :: name(size1, size2,...)
|
type, ALLOCATABLE :: name(:)
|
type, ALLOCATABLE :: name(:,:,...)
|
| PHP | array
|
|||
| Perl | ||||
| Raku | Array[type] or Array of type
|
|||
| Ruby | x = Array.new(size1){ Array.new(size2) }
|
Array
|
||
| Scala | Array[type]
|
Array[...[Array[type]]...]
|
ArrayBuffer[type]
|
|
| Seed7 | array typeor array [idxType] type
|
array array typeor array [idxType] array [idxType] type
|
array typeor array [idxType] type
|
array array typeor array [idxType] array [idxType] type
|
| Smalltalk | Array
|
OrderedCollection
|
||
| Windows PowerShell | type[]
|
type[[[:Template:Not a typo]]...]
|
||
| OCaml | type array
|
type array ... array
|
||
| F# | type []or type array
|
type [[[:Template:Not a typo]]...]
|
Systemor System
|
|
| Standard ML | type vector or type array
|
|||
| Haskell (GHC) | x = Array.array (0, size-1) list_of_association_pairs
|
x = Array.array ((0, 0,...), (size1-1, size2-1,...)) list_of_association_pairs
|
||
| COBOL | level-number type OCCURS size «TIMES».
|
Template:Em | level-number type OCCURS min-size TO max-size «TIMES» DEPENDING «ON» size.[e]
|
— |
- <templatestyles src="Citation/styles.css"/>^a In most expressions (except the
sizeofand&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. - <templatestyles src="Citation/styles.css"/>^b The C-like
type x[]works in Java, howevertype[] xis the preferred form of array declaration. - <templatestyles src="Citation/styles.css"/>^c Subranges are used to define the bounds of the array.
- <templatestyles src="Citation/styles.css"/>^d JavaScript's array are a special kind of object.
- <templatestyles src="Citation/styles.css"/>^e 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
| Simple composite types | Algebraic data types | Unions | ||
|---|---|---|---|---|
| Records | Tuple expression | |||
| Ada[1] | null record] | — | Any combination of records, unions, and enumerations (as well as references to those, enabling recursive types). | type name (variation : discrete_type) is recordScript error: No such module "Indent".case variation isScript error: No such module "Indent".when choice_list1 =>Script error: No such module "Indent".fieldname1 : type;Script error: No such module "Indent"....Script error: No such module "Indent".when choice_list2 =>Script error: No such module "Indent".fieldname2 : type;Script error: No such module "Indent"....Script error: No such module "Indent"....Script error: No such module "Indent".end case;
|
| ALGOL 68 | struct (modename «fieldname», ...);
|
Required types and operators can be user-defined | union (modename, ...);
| |
| C (C99) | struct «name» {type name;...};
|
— | — | union {type name;...};
|
| Objective-C | ||||
| C++ | struct «name» {type name;...};[b]
|
«std::»tuple<type1..typen>
| ||
| C# | struct name {type name;...}
|
(val1, val2, ... )
|
— | |
| Java | —[a] | |||
| JavaScript | — | |||
| D | struct name {type name;...}
|
std.variant.Algebraic!(type,...)
|
union {type name;...}
| |
| Go | struct { Script error: No such module "Indent".«name» typeScript error: No such module "Indent"....
|
|||
| Rust | struct name {name: type, ...}
|
(val1, val2, ... )
|
enum name { Foo(types), ...}
|
union name {name: type, ...}
|
| Swift | struct name {Script error: No such module "Indent".var name «: type»Script error: No such module "Indent"....
|
(«name1:» val1, «name2:» val2, «name3:» val3, ... )
|
enum name { case Foo«(types)» case Bar «(types)» ... }
|
|
| Common Lisp | (defstruct name slot-name (slot-name initial-value) (slot-name initial-value :type type) ...)
|
(cons val1 val2)[c]
|
||
| Scheme | — | |||
| ISLISP | ||||
| Pascal | recordScript error: No such module "Indent".name: type;Script error: No such module "Indent"....
|
— | — | recordScript error: No such module "Indent".case type ofScript error: No such module "Indent".value: (types);Script error: No such module "Indent"....
|
| Visual Basic | ||||
| Visual Basic .NET | Structure nameScript error: No such module "Indent".Dim name As typeScript error: No such module "Indent"....
|
(val1, val2, ... )
|
||
| Python | —[a] | «(»val1, val2, val3, ... «)»
|
— | |
| S-Lang | struct {name [=value], ...}
|
|||
| Fortran | TYPE nameScript error: No such module "Indent".type :: nameScript error: No such module "Indent"....
|
|||
| PHP | —[a] | |||
| Perl | —[d] | — | ||
| Raku | —[a] | |||
| Ruby | OpenStruct.new({:name => value})
|
|||
| Scala | case class name(«var» name: type, ...)
|
(val1, val2, val3, ... )
|
abstract class nameScript error: No such module "Indent".case class Foo(«parameters») extends nameScript error: No such module "Indent".case class Bar(«parameters») extends nameScript error: No such module "Indent"....or abstract class nameScript error: No such module "Indent".case object Foo extends nameScript error: No such module "Indent".case object Bar extends nameScript error: No such module "Indent"....or a combination of case classes and case objects |
|
| Windows PowerShell | ||||
| OCaml | type name = {«mutable» name : type;...}
|
«(»val1, val2, val3, ... «)»
|
type name = Foo «of type» | Bar «of type» | ...
|
— |
| F# | ||||
| Standard ML | type name = {name : type,...}
|
(val1, val2, val3, ... )
|
Bar «of type» | ... | |
| Haskell | data Name = Constr {name :: type,...}
|
Bar «types» | ... | ||
| COBOL | level-number name type clauses.Script error: No such module "Indent".level-number+n name type clauses.Script error: No such module "Indent"....
|
— | — | name REDEFINES variable type.
|
- <templatestyles src="Citation/styles.css"/>^a Only classes are supported.
- <templatestyles src="Citation/styles.css"/>^b
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. - <templatestyles src="Citation/styles.css"/>^c pair only
- <templatestyles src="Citation/styles.css"/>^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.
- <templatestyles src="Citation/styles.css"/>^e Enumerations in this language are algebraic types with only nullary constructors
Variable and constant declarations
| variable | constant | type synonym | |
|---|---|---|---|
| Ada[1] | identifier : type« := initial_value»[e]
|
identifier : constant type := final_value
|
subtype identifier is type
|
| ALGOL 68 | modename name« := initial_value»;
|
modename name = value;
|
mode synonym = modename;
|
| C (C99) | type name« = initial_value»;
|
enum{ name = value };
|
typedef type synonym;
|
| Objective-C | |||
| C++ | const type name = value;
| ||
| C# | type name1« = initial_value», name2« = initial_value», ...;or var name = initial_value;
|
const type name = value, name = value, ...;or readonly type name = value, name = value, ... ;
|
using synonym = type;
|
| D | type name« = initial_value»;or auto name = value;
|
const type name = value;or immutable type name = value;
|
alias type synonym;
|
| Java | type name« = initial_value»;
|
final type name = value;
|
— |
| JavaScript | var name« = initial_value»; orlet name« = initial_value»; (since ECMAScript 2015)
|
const name = value; (since ECMAScript 2015)
| |
| Go | var name type« = initial_value»or name := initial_value
|
const name «type» = value
|
type synonym type
|
| Rust[f] | let mut name«: type»« = initial_value»;static mut NAME: type = value;
|
let name«: type»« = initial_value»;const NAME: type = value;static NAME: type = value;
|
type synonym = typename;
|
| Swift | var name «: type»« = initial_value»
|
let name «: type» = value
|
typealias synonym = type
|
| Common Lisp | (defparameter name initial-value)or (defvar name initial-value)
|
(defconstant name value)
|
(deftype synonym () 'type)
|
| Scheme | (define name initial_value)
|
||
| ISLISP | (defglobal name initial_value)or (defdynamic name initial_value)
|
(defconstant name value)
|
— |
| Pascal[a] | name: type« = initial_value»
|
name = value
|
synonym = type
|
| Visual Basic | Dim name «As type»
|
See notes to left.
Constants use the same syntax, and:
|
|
| Visual Basic .NET[10] | 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
Template:In5
and for which, for each
If |
Imports synonym = type
| |
| Xojo | Dim name «As type»« = initial_value»
|
— | |
| Python | name«: type» = initial_value
|
— | synonym = type[b]
|
| CoffeeScript | name = initial_value
| ||
| S-Lang | name = initial_value;
|
typedef struct {...} typename
| |
| Fortran | type :: name
|
type, PARAMETER :: name = value
|
|
| PHP | $name = initial_value;
|
define("name", value);
|
— |
| Perl | «my» $name« = initial_value»;[c]
|
use constant name => value;
| |
| Raku | «my «type»» $name« = initial_value»;[c]
|
«my «type»» constant name = value;
|
::synonym ::= type
|
| Ruby | name = initial_value
|
Name = value
|
synonym = type[b]
|
| Scala | var name«: type» = initial_value
|
val name«: type» = value
|
type synonym = type
|
| Windows PowerShell | «[type]» $name = initial_value
|
— | — |
| Bash shell | 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
| |
| Forth | VARIABLE name (in some systems use value VARIABLE name instead)
|
value CONSTANT name
|
|
| COBOL | level-number name type clauses.
|
«0»1 name CONSTANT «AS» value.
|
level-number name type clauses «IS» TYPEDEF.
|
| Mathematica | name=initial_value
|
— | — |
- <templatestyles src="Citation/styles.css"/>^a Pascal has declaration blocks. See functions.
- <templatestyles src="Citation/styles.css"/>^b Types are just regular objects, so you can just assign them.
- <templatestyles src="Citation/styles.css"/>^c In Perl, the "my" keyword scopes the variable into the block.
- <templatestyles src="Citation/styles.css"/>^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. - <templatestyles src="Citation/styles.css"/>^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.
- <templatestyles src="Citation/styles.css"/>^f In Rust, if no initial value is given to a
letorlet mutvariable and it is never assigned to later, there is an "unused variable" warning. If no value is provided for aconstorstaticorstatic mutvariable, there is an error. There is a "non-upper-case globals" error for non-uppercaseconstvariables. After it is defined, astatic mutvariable can only be assigned to in anunsafeblock or function.
Control flow
Conditional statements
| if | else if | select case | conditional expression | |
|---|---|---|---|---|
| Ada[1] | if condition thenScript error: No such module "Indent".statements
|
if condition1 thenScript error: No such module "Indent".statements
|
case expression isScript error: No such module "Indent".when value_list1 => statementsScript error: No such module "Indent".when value_list2 => statementsScript error: No such module "Indent"....Script error: No such module "Indent".«when others => statements»
|
(if condition1 thenScript error: No such module "Indent".expression1or (case expression isScript error: No such module "Indent".when value_list1 => expression1Script error: No such module "Indent".when value_list2 => expression2Script error: No such module "Indent"....Script error: No such module "Indent".«when others => expressionn»
|
| Seed7 | if condition thenScript error: No such module "Indent".statements
|
if condition1 thenScript error: No such module "Indent".statements
|
case expression ofScript error: No such module "Indent".when set1 : statementsScript error: No such module "Indent"....Script error: No such module "Indent".«otherwise: statements»
|
|
| Modula-2 | if condition thenScript error: No such module "Indent".statements
|
if condition1 thenScript error: No such module "Indent".statements
|
case expression ofScript error: No such module "Indent".caseLabelList : statements |Script error: No such module "Indent"....Script error: No such module "Indent".«else statements»
|
|
| ALGOL 68 | if condition then statements «else statements» fi
|
if condition then statements elif condition then statements fi
|
case switch in statements, statements«,... out statements» esac
|
( condition | valueIfTrue | valueIfFalse )
|
| ALGOL 68 (brief form) |
( condition | statements «| statements» )
|
( condition | statements |: condition | statements )
|
( variable | statements,... «| statements» )
|
|
| APL | :If conditionScript error: No such module "Indent".instructions
|
:If conditionScript error: No such module "Indent".instructions
|
:Select expressionScript error: No such module "Indent".:Case case1Script error: No such module "Indent".instructionsScript error: No such module "Indent"....Script error: No such module "Indent".«:ElseScript error: No such module "Indent".instructions»
|
{condition:valueIfTrue ⋄ valueIfFalse}
|
| C (C99) | if (condition) instructionsinstructions can be a single statement or a block in the form of: { statements }
|
if (condition) instructionsor if (condition) instructions
|
switch (variable) { Script error: No such module "Indent".case case1: instructions «; break;»Script error: No such module "Indent"....Script error: No such module "Indent".«default: instructions»
|
condition ? valueIfTrue : valueIfFalse
|
| Objective-C | ||||
| C++ (STL) | ||||
| D | ||||
| Java | ||||
| JavaScript | ||||
| PHP | ||||
| C# | if (condition) instructions
|
if (condition) instructions
|
switch (variable)
All non-empty cases must end with a |
condition ? valueIfTrue : valueIfFalse
|
| Windows PowerShell | if (condition) instruction
|
if (condition) { instructions }
|
switch (variable) { case1{instructions «break;» } ... «default { instructions }»}
|
|
| Go | if condition {instructions}
|
if condition {instructions}or switch { Script error: No such module "Indent".case condition: instructionsScript error: No such module "Indent"....Script error: No such module "Indent".«default: instructions»
|
switch variable { Script error: No such module "Indent".case case1: instructionsScript error: No such module "Indent"....Script error: No such module "Indent".«default: instructions»
|
|
| Swift | if condition {instructions}
|
if condition {instructions}
|
switch variable { Script error: No such module "Indent".case case1: instructionsScript error: No such module "Indent"....Script error: No such module "Indent".«default: instructions»
|
|
| Perl | if (condition) {instructions}or unless (notcondition) {instructions}
|
if (condition) {instructions}or unless (notcondition) {instructions}
|
use feature "switch";
|
condition ? valueIfTrue : valueIfFalse
|
| Raku | if condition {instructions}or unless notcondition {instructions}
|
if condition {instructions}
|
given variable { Script error: No such module "Indent".when case1 { instructions }Script error: No such module "Indent"....Script error: No such module "Indent".«default { instructions }»
|
condition ?? valueIfTrue !! valueIfFalse
|
| Ruby | if conditionScript error: No such module "Indent".instructions
|
if conditionScript error: No such module "Indent".instructions
|
case variableScript error: No such module "Indent".when case1Script error: No such module "Indent".instructionsScript error: No such module "Indent"....Script error: No such module "Indent".«elseScript error: No such module "Indent".instructions»
|
condition ? valueIfTrue : valueIfFalse
|
| Scala | if (condition) {instructions}
|
if (condition) {instructions}
|
expression match { Script error: No such module "Indent".case pattern1 => expressionScript error: No such module "Indent".case pattern2 => expressionScript error: No such module "Indent"....Script error: No such module "Indent".«case _ => expression»[b]
|
if (condition) valueIfTrue else valueIfFalse
|
| Smalltalk | condition ifTrue:Script error: No such module "Indent".trueBlock
|
condition ifTrue: trueBlock ifFalse: falseBlock
| ||
| Common Lisp | (when conditionScript error: No such module "Indent".instructions)or (unless conditionScript error: No such module "Indent".instructions)or (if conditionScript error: No such module "Indent".(progn instructions)Script error: No such module "Indent".«(progn instructions)»)
|
(cond (condition1 instructions)Script error: No such module "Indent".(condition2 instructions)Script error: No such module "Indent"....Script error: No such module "Indent".«(t instructions)»)
|
(case expressionScript error: No such module "Indent".(case1 instructions)Script error: No such module "Indent".(case2 instructions)Script error: No such module "Indent"....Script error: No such module "Indent".«(otherwise instructions)»)
|
(if test then else)or (cond (test1 value1) (test2 value2) ...))
|
| 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)»)
|
(if condition valueIfTrue valueIfFalse)
|
| ISLISP | (if conditionScript error: No such module "Indent".(progn instructions)Script error: No such module "Indent".«(progn instructions)»)
|
(cond (condition1 instructions)Script error: No such module "Indent".(condition2 instructions)Script error: No such module "Indent"....Script error: No such module "Indent".«(t instructions)»)
|
(case expressionScript error: No such module "Indent".(case1 instructions)Script error: No such module "Indent".(case2 instructions)Script error: No such module "Indent"....Script error: No such module "Indent".«(t instructions)»)
|
(if condition valueIfTrue valueIfFalse)
|
| Pascal | if condition then beginScript error: No such module "Indent".instructions[c]
|
if condition then beginScript error: No such module "Indent".instructions[c]
|
case variable ofScript error: No such module "Indent".case1: instructionsScript error: No such module "Indent"....Script error: No such module "Indent".«else: instructions»[c]
| |
| Visual Basic | If condition ThenScript error: No such module "Indent".instructionsSingle-line, when instructions are instruction1 : instruction2 : ...:If condition Then instructions «Else instructions»
|
If condition ThenScript error: No such module "Indent".instructionsSingle-line: See note about C-like languages; the Else clause of a single-line If statement can contain another single-line If statement.
|
Select« Case» variableScript error: No such module "Indent".Case case_pattern1Script error: No such module "Indent".instructionsScript error: No such module "Indent"....Script error: No such module "Indent".«Case ElseScript error: No such module "Indent".instructions»
|
IIf(condition, valueIfTrue, valueIfFalse)
|
| Visual Basic .NET | If(condition, valueIfTrue, valueIfFalse)
| |||
| Xojo | ||||
| Python[a] | if condition :
|
if condition :
|
Python 3.10+:match variable:
|
Python 2.5+:valueIfTrue if condition else valueIfFalse
|
| 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 | IF (condition) THENScript error: No such module "Indent".instructions
|
IF (condition) THENScript error: No such module "Indent".instructions
|
SELECT CASE(variable)Script error: No such module "Indent".CASE (case1)Script error: No such module "Indent".instructionsScript error: No such module "Indent"....Script error: No such module "Indent".CASE DEFAULTScript error: No such module "Indent".instructions
|
|
| Forth | condition IF instructions « ELSE instructions» THEN
|
condition IF instructions ELSE condition IF instructions THEN THEN
|
value CASEScript error: No such module "Indent".case OF instructions ENDOFScript error: No such module "Indent".case OF instructions ENDOFScript error: No such module "Indent".default instructions
|
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 Script error: No such module "Indent".pattern1 -> expressionScript error: No such module "Indent".| pattern2 -> expressionScript error: No such module "Indent"....Script error: No such module "Indent".«| _ -> expression»[b]
|
if condition then valueIfTrue else valueIfFalse
|
| F# | Lightweight syntax mode:
Either on a single line or with indentation as shown below:
|
Lightweight syntax mode: Either on a single line or with indentation as shown below: if condition thenVerbose syntax mode: Same as Standard ML. | ||
| Standard ML | if condition then «(»instructions «)»
|
if condition then «(»instructions «)»
|
pattern2 => expressionScript error: No such module "Indent"....Script error: No such module "Indent".«| _ => expression»[b] | |
| Haskell (GHC) | if condition then expression else expressionor when condition (do instructions)or unless notcondition (do instructions)
|
condition = expressionScript error: No such module "Indent".| condition = expressionScript error: No such module "Indent".| otherwise = expression | case value of { Script error: No such module "Indent".pattern1 -> expression;Script error: No such module "Indent".pattern2 -> expression;Script error: No such module "Indent"....Script error: No such module "Indent".«_ -> expression»[b]
| |
| Bash shell | if condition-command; thenScript error: No such module "Indent".expression
|
if condition-command; thenScript error: No such module "Indent".expression
|
case "$variable" inScript error: No such module "Indent"."$condition1" )Script error: No such module "Indent".command...Script error: No such module "Indent"."$condition2" )Script error: No such module "Indent".command...
|
|
| CoffeeScript | if condition then expression «else expression»or if conditionScript error: No such module "Indent".expressionor expression if conditionor unless conditionScript error: No such module "Indent".expressionor expression unless condition
|
if condition then expression else if condition then expression «else expression»or if conditionScript error: No such module "Indent".expressionor unless conditionScript error: No such module "Indent".expression
|
switch expressionScript error: No such module "Indent".when condition then expressionScript error: No such module "Indent".else expressionor switch expressionScript error: No such module "Indent".when conditionScript error: No such module "Indent".expressionScript error: No such module "Indent".«elseScript error: No such module "Indent".expression»
|
All conditions are expressions. |
| COBOL | IF condition «THEN»Script error: No such module "Indent".expression[d]
|
EVALUATE expression «ALSO expression...»Script error: No such module "Indent".WHEN case-or-condition «ALSO case-or-condition...»Script error: No such module "Indent".expressionScript error: No such module "Indent"....Script error: No such module "Indent".«WHEN OTHERScript error: No such module "Indent".expression»
|
||
| Rust | if condition { Script error: No such module "Indent".expression
|
if condition { Script error: No such module "Indent".expression
|
match variable { Script error: No such module "Indent".pattern1 => expression,Script error: No such module "Indent".pattern2 => expression,Script error: No such module "Indent".pattern3 => expression,Script error: No such module "Indent".«_ => expression»[b][e]
|
All conditions are expressions |
| if | else if | select case | conditional expression |
- <templatestyles src="Citation/styles.css"/>^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.
- <templatestyles src="Citation/styles.css"/>^b This is pattern matching and is similar to select case but not the same. It is usually used to deconstruct algebraic data types.
- <templatestyles src="Citation/styles.css"/>^c In languages of the Pascal family, the semicolon is not part of the statement. It is a separator between statements, not a terminator.
- <templatestyles src="Citation/styles.css"/>^d
END-IFmay be used instead of the period at the end. - <templatestyles src="Citation/styles.css"/>^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{}).
Loop statements
| while loop | do while loop | (count-controlled) for loop | foreach | |
|---|---|---|---|---|
| Ada[1] | while condition loopScript error: No such module "Indent".statements
|
loopScript error: No such module "Indent".statementsScript error: No such module "Indent".exit when not condition
|
discrete_type] loopScript error: No such module "Indent".statements end loop |
some] [in | of] [first .. last | discrete_type | iterator] => predicate)[b] |
| ALGOL 68 | «for index» «from first» «by increment» «to last» «while condition» do statements od
|
for key «to upb list» do «typename val=list[key];» statements od
| ||
«while condition»Script error: No such module "Indent".do statements od
|
«while statements; condition»Script error: No such module "Indent".do statements od
|
«for index» «from first» «by increment» «to last» do statements od
| ||
| APL | :While conditionScript error: No such module "Indent".statements
|
:RepeatScript error: No such module "Indent".statements
|
:For var«s» :In listScript error: No such module "Indent".statements
|
:For var«s» :InEach listScript error: No such module "Indent".statements
|
| C (C99) | instructions can be a single statement or a block in the form of: { statements }while (condition) instructions
|
do instructions while (condition);
|
for («type» i = first; i <= last; i++) instructions
|
— |
| Objective-C | for (type item in set) instructions
| |||
| C++ (STL) | «std::»for_each(start, end, function)Since C++11: for (type item : set) instructions
| |||
| C# | foreach (type item in set) instructions
| |||
| Java | for (type item : set) instructions
| |||
| JavaScript | for (var i = first; i <= last; i++) instructions
|
Since EcmaScript 2015:[11]
| ||
| PHP | foreach (range(first, last) as $i) instructionsor for ($i = first; $i <= last; $i++) instructions
|
foreach (set as item) instructionsor foreach (set as key => item) instructions
| ||
| Windows PowerShell | for ($i = first; $i -le last; $i++) instructions
|
foreach (item in set) instructions
| ||
| D | foreach (i; first ... last) instructions
|
foreach («type» item; set) instructions
| ||
| Go | for condition { instructions }
|
for i := first; i <= last; i++ { instructions }
|
for key, item := range set { instructions }
| |
| Swift | while condition { instructions }
|
2.x:repeat { instructions } while condition1.x: do { instructions } while condition
|
for i = first ... last { instructions }or for i = first ..< last+1 { instructions }or for var i = first; i <= last; i++ { instructions }
|
for item in set { instructions }
|
| Perl | while (condition) { instructions }or until (notcondition) { instructions }
|
do { instructions } while (condition)or do { instructions } until (notcondition)
|
for«each» «$i» (first .. last) { instructions }or for ($i = first; $i <= last; $i++) { instructions }
|
for«each» «$item» (set) { instructions }
|
| Raku | while condition { instructions }or until notcondition { instructions }
|
repeat { instructions } while conditionor repeat { instructions } until notcondition
|
for first..last -> $i { instructions }or loop ($i = first; $i <=last; $i++) { instructions }
|
for set« -> $item» { instructions }
|
| Ruby | while conditionScript error: No such module "Indent".instructionsor until notconditionScript error: No such module "Indent".instructions
|
beginScript error: No such module "Indent".instructionsor beginScript error: No such module "Indent".instructions
|
for i in first..lastScript error: No such module "Indent".instructionsor for i in first...last+1Script error: No such module "Indent".instructionsor first.Template:Not a typo(last) { |i| instructions }
|
item| instructions } |
| Bash shell | while condition ;doScript error: No such module "Indent".instructionsor until notcondition ;doScript error: No such module "Indent".instructions
|
— | for ((i = first; i <= last; ++i)) ; doScript error: No such module "Indent".instructions
|
for item in set ;doScript error: No such module "Indent".instructions
|
| Scala | while (condition) { instructions }
|
do { instructions } while (condition)
|
for (i <- first to last «by 1») { instructions }or first to last «by 1» foreach (i => { instructions })
|
for (item <- set) { instructions }or set foreach (item => { instructions })
|
| Smalltalk | conditionBlock whileTrue:Script error: No such module "Indent".loopBlock
|
loopBlock doWhile:Script error: No such module "Indent".conditionBlock
|
first to: last do:Script error: No such module "Indent".loopBlock
|
collection do:Script error: No such module "Indent".loopBlock
|
| Common Lisp | (loopScript error: No such module "Indent".while conditionScript error: No such module "Indent".doScript error: No such module "Indent".instructions)or (do () (notcondition)Script error: No such module "Indent".instructions)
|
(loopScript error: No such module "Indent".doScript error: No such module "Indent".instructionsScript error: No such module "Indent".while condition)
|
(loopScript error: No such module "Indent".for i from first to last «by 1»Script error: No such module "Indent".doScript error: No such module "Indent".instructions)or (dotimes (i N)Script error: No such module "Indent".instructions)or (do ((i first (1+ i))) ((>=i last))
|
(loopScript error: No such module "Indent".for item in listScript error: No such module "Indent".doScript error: No such module "Indent".instructions)or (loopScript error: No such module "Indent".for item across vectorScript error: No such module "Indent".doScript error: No such module "Indent".instructions)or (dolist (item list)Script error: No such module "Indent".instructions)or (mapc function list)or (map type function sequence)
|
| 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)
|
| ISLISP | (while condition instructions)
|
(tagbody loop instructions (if condition (go loop))
|
(for ((i first (+ i 1))) ((>= i last)) instructions)
|
(mapc (lambda (item) instructions) list)
|
| Pascal | while condition do beginScript error: No such module "Indent".instructions
|
repeatScript error: No such module "Indent".instructions
|
for i := first «step 1» to last do beginScript error: No such module "Indent".instructions[a]
|
for item in set do instructions
|
| Visual Basic | Do While conditionScript error: No such module "Indent".instructionsor Do Until notconditionScript error: No such module "Indent".instructionsor While conditionScript error: No such module "Indent".instructions (Visual Basic .NET uses End While instead)
|
DoScript error: No such module "Indent".instructionsor DoScript error: No such module "Indent".instructions
|
i must be declared beforehand.
|
For Each item In setScript error: No such module "Indent".instructions
|
| Visual Basic .NET | For i« As type» = first To last« Step 1»Script error: No such module "Indent".instructions[a]
|
For Each item« As type» In setScript error: No such module "Indent".instructions
| ||
| Xojo | While conditionScript error: No such module "Indent".instructions
|
Do Until notconditionScript error: No such module "Indent".instructionsor DoScript error: No such module "Indent".instructions
| ||
| Python | while condition :
|
— | Python 3.x:for i in range(first, last+1):Python 2.x: for i in xrange(first, last+1):
|
for item in set:
|
| 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 | DO WHILE (condition)Script error: No such module "Indent".instructions
|
DOScript error: No such module "Indent".instructionsScript error: No such module "Indent".IF (condition) EXIT
|
DO I = first,lastScript error: No such module "Indent".instructions
|
— |
| Forth | BEGIN «instructions» condition WHILE instructions REPEAT
|
BEGIN instructions condition UNTIL
|
limit start DO instructions LOOP
|
— |
| OCaml | while condition do instructions done
|
— | for i = first to last do instructions done
|
Array.iter (fun item -> instructions) arrayor List.iter (fun item -> instructions) list
|
| F# | while condition do
|
— | for i = first to last do
|
foritem in set door Seq.iter (fun item -> instructions) set
|
| Standard ML | while condition do ( instructions )
|
— | Array.app (fn item => instructions) arrayor app (fn item => instructions) list
| |
| Haskell (GHC) | — | Control.Monad.forM_ [first..last] (\i -> do instructions)
|
Control.Monad.forM_list (\item -> do instructions)
| |
| Eiffel | fromScript error: No such module "Indent".setup
| |||
| CoffeeScript | while conditionScript error: No such module "Indent".expressionor expression while conditionor while condition then expressionor until conditionScript error: No such module "Indent".expressionor expression until conditionor until expression then condition
|
— | for i in [first..last]Script error: No such module "Indent".expressionor for i in [first..last] then expressionor expression for i in [first..last]
|
for item in setScript error: No such module "Indent".expressionor for item in set then expressionor expression for item in set
|
| COBOL | PERFORM procedure-1 «THROUGH procedure-2» ««WITH» TEST BEFORE» UNTIL condition[c]or PERFORM ««WITH» TEST BEFORE» UNTIL conditionScript error: No such module "Indent".expression
|
PERFORM procedure-1 «THROUGH procedure-2» «WITH» TEST AFTER UNTIL condition[c]or PERFORM «WITH» TEST AFTER UNTIL conditionScript error: No such module "Indent".expression
|
PERFORM procedure-1 «THROUGH procedure-2» VARYING i FROM first BY increment UNTIL i > last[d]or PERFORM VARYING i FROM first BY increment UNTIL i > lastScript error: No such module "Indent".expression[d]
|
— |
| Rust | while condition { Script error: No such module "Indent".expression
|
loop { Script error: No such module "Indent".expressionScript error: No such module "Indent".if condition { Script error: No such module "Indent".break;Script error: No such module "Indent".}
|
for i in first..last+1 { Script error: No such module "Indent".expressionor for i in first..=last { Script error: No such module "Indent".expression
|
for item in set { Script error: No such module "Indent".expression[e]or set.into_iter().for_each(|item| expression);[e]
|
- <templatestyles src="Citation/styles.css"/>^a "
stepn" is used to change the loop interval. If "step" is omitted, then the loop interval is 1. - <templatestyles src="Citation/styles.css"/>^b This implements the universal quantifier ("for all" or "") as well as the existential quantifier ("there exists" or "").
- <templatestyles src="Citation/styles.css"/>^c
THRUmay be used instead ofTHROUGH. - <templatestyles src="Citation/styles.css"/>^d
«IS» GREATER «THAN»may be used instead of>. - <templatestyles src="Citation/styles.css"/>^e Type of set expression must implement trait
std::iter::IntoIterator.
Exceptions
Script error: No such module "labelled list hatnote".
| throw | handler | assertion | |
|---|---|---|---|
| Ada[1] | raise exception_name «with string_expression»
|
beginScript error: No such module "Indent".statements[b]
|
procedure | entry] withScript error: No such module "Indent".Pre => boolean_expressionScript error: No such module "Indent".Post => boolean_expression any_type with Type_Invariant => boolean_expression |
| APL | «string_expression» ⎕SIGNAL number_expression
|
:Trap number«s»_expressionScript error: No such module "Indent".statements
|
«string_expression» ⎕SIGNAL 98/⍨~condition
|
| C (C99) | longjmp(state, exception);
|
switch (setjmp(state)) { case 0: instructions break; case exception: instructions ... }
|
assert(condition);
|
| C++ | throw exception;
|
try { instructions } catch «(exception)» { instructions } ...
| |
| C# | try { instructions } catch «(exception« name»)» { instructions } ... «finally { instructions }»
|
System.Diagnostics.Debug.Assert(condition);or System.Diagnostics.Trace.Assert(condition);
| |
| Java | try { instructions } catch (exception) { instructions } ... «finally { instructions }»
|
assert condition «: description»;
| |
| JavaScript | try { instructions } catch (exception) { instructions} «finally { instructions }»
|
? | |
| D | try { instructions } catch (exception) { instructions } ... «finally { instructions }»
|
assert(condition);
| |
| PHP | try { instructions } catch (exception) { instructions } ... «finally { instructions }»
|
assert(condition);
| |
| S-Lang | try { instructions } catch «exception» { instructions } ... «finally { instructions }»
|
? | |
| Windows PowerShell | trap «[exception]» { instructions } ... instructionsor try { instructions } catch «[exception]» { instructions } ... «finally { instructions }»
|
[Debug]::Assert(condition)
| |
| Objective-C | @throw exception;
|
@try { instructions } @catch (exception) { instructions } ... «@finally { instructions }»
|
NSAssert(condition, description);
|
| Swift | throw exception (2.x)
|
do { try expression ... instructions } catch exception { instructions } ... (2.x)
|
assert(condition«, description»)
|
| Perl | die exception;
|
eval { instructions }; if ($@) { instructions }
|
? |
| Raku | try { instructions CATCH { when exception { instructions } ...}}
|
? | |
| Ruby | raise exception
|
beginScript error: No such module "Indent".instructions
|
|
| Smalltalk | exception raise
|
instructionBlock on: exception do: handlerBlock
|
assert: conditionBlock
|
| Common Lisp | (error "exception")or (errorScript error: No such module "Indent".typeScript error: No such module "Indent".arguments)or (error (make-conditionScript error: No such module "Indent".typeScript error: No such module "Indent".arguments))
|
(handler-caseScript error: No such module "Indent".(progn instructions)Script error: No such module "Indent".(exception instructions)Script error: No such module "Indent"....)or (handler-bindScript error: No such module "Indent".(conditionScript error: No such module "Indent".(lambdaScript error: No such module "Indent".instructionsScript error: No such module "Indent".«invoke-restart restart args»))[a]
|
(assert condition)or (assert conditionScript error: No such module "Indent".«(place)Script error: No such module "Indent".«error»»)or (check-type var type)
|
| Scheme (R6RS) | (raise exception)
|
(guard (con (condition instructions) ...) instructions)
|
? |
| ISLISP | (error "error-string" objects)or (signal-condition condition continuable)
|
(with-handlerScript error: No such module "Indent".handler form*
|
? |
| Pascal | raise Exception.Create()
|
try Except on E: exception do begin instructions end; end;
|
? |
| Visual Basic | Err.Raise ERRORNUMBER
|
With New Try: On Error Resume NextScript error: No such module "Indent".OneInstruction'*** 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
End Property
|
Debug.Assert condition
|
| Visual Basic .NET | Throw exceptionor Error errorcode
|
TryScript error: No such module "Indent".instructions
|
System.Diagnostics.Debug.Assert(condition)or System.Diagnostics.Trace.Assert(condition)
|
| Xojo | Raise exception
|
TryScript error: No such module "Indent".instructions
|
— |
| Python | raise exception
|
try:
|
assert condition
|
| Fortran | — | ||
| Forth | code THROW
|
xt CATCH ( code or 0 )
|
— |
| 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 exceptionor throwError expression
|
catch tryExpression catchExpressionor catchError tryExpression catchExpression
|
assert condition expression
|
| COBOL | RAISE «EXCEPTION» exception
|
USE «AFTER» EXCEPTION OBJECT class-name.or USE «AFTER» EO class-name.or USE «AFTER» EXCEPTION CONDITION exception-name «FILE file-name».or USE «AFTER» EC exception-name «FILE file-name».
|
— |
| Rust | No[13] | assert!(condition)
| |
- <templatestyles src="Citation/styles.css"/>^a Common Lisp allows
with-simple-restart,restart-caseandrestart-bindto define restarts for use withinvoke-restart. Unhandled conditions may cause the implementation to show a restarts menu to the user before unwinding the stack. - <templatestyles src="Citation/styles.css"/>^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
| exit block (break) | continue | label | branch (goto) | return value from generator | |
|---|---|---|---|---|---|
| Ada[1] | exit «loop_name» «when condition»
|
— | label:
|
goto label
|
— |
| ALGOL 68 | value exit; ...
|
do statements; skip exit; label: statements od
|
label: ...
|
go to label; ...
|
yield(value)
|
| APL | :Leave
|
:Continue
|
label:
|
→labelor :GoTo label
|
— |
| C (C99) | break;
|
continue;
|
label:
|
goto label;
|
— |
| Objective-C | |||||
| C++ (STL) | |||||
| D | |||||
| C# | yield return value;
| ||||
| Java | break «label»;
|
continue «label»;
|
— | ||
| JavaScript | yield value«;»
| ||||
| PHP | break «levels»;
|
continue «levels»;
|
goto label;
|
yield «key =>» value;
| |
| Perl | last «label»;
|
next «label»;
|
|||
| Raku | |||||
| Go | break «label»
|
continue «label»
|
goto label
|
||
| Swift | break «label»
|
continue «label»
|
— | ||
| Bash shell | break «levels»
|
continue «levels»
|
— | — | — |
| Common Lisp | (return)or (return-from block)or (loop-finish)
|
(tagbody tagScript error: No such module "Indent"....Script error: No such module "Indent".tag
|
(go tag)
|
||
| Scheme | |||||
| ISLISP | (return-from block)
|
(tagbody tagScript error: No such module "Indent"....Script error: No such module "Indent".tag
|
(go tag)
|
||
| Pascal (ISO) | — | label:[a]
|
goto label;
|
— | |
| Pascal (FPC) | break;
|
continue;
| |||
| Visual Basic | Exit blockAlternatively, for methods,Return
|
— | label:
|
GoTo label
| |
| Xojo | Continue block
| ||||
| Visual Basic .NET | Yield value
| ||||
| Python | break
|
continue
|
— | yield value
| |
| RPG IV | LEAVE;
|
ITER;
|
|||
| S-Lang | break;
|
continue;
|
|||
| Fortran | EXIT
|
CYCLE
|
label[b]
|
GOTO label
|
— |
| Ruby | break
|
next
|
|||
| Windows PowerShell | break «label»
|
continue
|
|||
| OCaml | — | ||||
| F# | |||||
| Standard ML | |||||
| Haskell (GHC) | |||||
| COBOL | EXIT PERFORM or EXIT PARAGRAPH or EXIT SECTION or EXIT.
|
EXIT PERFORM CYCLE
|
label «SECTION».
|
GO TO label
|
— |
- <templatestyles src="Citation/styles.css"/>^a Pascal has declaration blocks. See functions.
- <templatestyles src="Citation/styles.css"/>^b label must be a number between 1 and 99999.
Functions
See reflective programming for calling and declaring functions by strings. Template:Sort-under
| calling a function | basic/void function | value-returning function | required main function | |
|---|---|---|---|---|
| Ada[1] | foo «(parameters)»
|
procedure foo «(parameters)» is begin statements end foo
|
function foo «(parameters)» return type is begin statements end foo
|
— |
| ALGOL 68 | foo «(parameters)»;
|
proc foo = «(parameters)» void: ( instructions );
|
proc foo = «(parameters)» rettype: ( instructions ...; retvalue );
|
— |
| APL | «parameters» foo parameters
|
foo←{ statements }
|
foo←{ statements }
|
— |
| C (C99) | foo(«parameters»)
|
void foo(«parameters») { instructions }
|
type foo(«parameters») { instructions ... return value; }
|
«global declarations»
|
| Objective-C | ||||
| C++ (STL) | ||||
| Java | public static void main(String[] args) { instructions }or public static void main(String... args) { instructions }
| |||
| D | int main(«char[][] args») { instructions}or int main(«string[] args») { instructions}or void main(«char[][] args») { instructions}or void main(«string[] args») { instructions}
| |||
| C# | Same as above; alternatively, if only one statement:
|
Same as above; alternatively, if simple enough to be an expression:
|
static void Main(«string[] args») method_bodyMay instead return int.(starting with C# 7.1:) May return Task or Task<int>, and if so, may be async.
| |
| 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; }
|
— | |
| Go | func foo(«parameters») { instructions }
|
func foo(«parameters») type { instructions ... return value }
|
func main() { instructions }
| |
| Swift | func foo(«parameters») { instructions }
|
func foo(«parameters») -> type { instructions ... return value }
|
— | |
| Common Lisp | (foo «parameters»)
|
(defun foo («parameters»)Script error: No such module "Indent".instructions)or (setf (symbol-function 'symbol)Script error: No such module "Indent".function)
|
(defun foo («parameters»)Script error: No such module "Indent"....Script error: No such module "Indent".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))
| ||
| ISLISP | (defun foo («parameters»)Script error: No such module "Indent".instructions)
|
(defun foo («parameters»)Script error: No such module "Indent"....Script error: No such module "Indent".value)
| ||
| Pascal | foo«(parameters)»
|
procedure foo«(parameters)»; «forward;»[a]
|
function foo«(parameters)»: type; «forward;»[a]
|
program name;
|
| Visual Basic | Foo(«parameters»)
|
Sub Foo«(parameters)»Script error: No such module "Indent".instructions
|
Function Foo«(parameters)»« As type»Script error: No such module "Indent".instructionsScript error: No such module "Indent".Foo = value
|
Sub Main()Script error: No such module "Indent".instructions
|
| Visual Basic .NET | Same as above; alternatively:
|
Sub Main(««ByVal »args() As String»)Script error: No such module "Indent".instructionsorFunction Main(««ByVal »args() As String») As IntegerScript error: No such module "Indent".instructions
| ||
| Xojo | ||||
| Python | foo(«parameters»)
|
def foo(«parameters»):
|
def foo(«parameters»):
|
— |
| S-Lang | foo(«parameters» «;qualifiers»)
|
define foo («parameters») { instructions }
|
define foo («parameters») { instructions ... return value; }
|
public define slsh_main () { instructions }
|
| Fortran | foo («arguments»)[c]
|
SUBROUTINE sub_foo («arguments»)Script error: No such module "Indent".instructions[c]
|
type FUNCTION foo («arguments»)Script error: No such module "Indent".instructionsScript error: No such module "Indent"....Script error: No such module "Indent".foo = value[c]
|
PROGRAM mainScript error: No such module "Indent".instructions
|
| Forth | «parameters» FOO
|
: FOO « stack effect comment: ( before -- ) »Script error: No such module "Indent".instructions
|
: FOO « stack effect comment: ( before -- after ) »Script error: No such module "Indent".instructions
|
— |
| PHP | foo(«parameters»)
|
function foo(«parameters») { instructions }
|
function foo(«parameters») { instructions ... return value; }
|
— |
| Perl | foo(«parameters»)or &foo«(parameters)»
|
sub foo { «my (parameters) = @_;» instructions }
|
sub foo { «my (parameters) = @_;» instructions... «return» value; }
| |
| Raku | foo(«parameters»)or &foo«(parameters)»
|
«multi »sub foo(parameters) { instructions }
|
«our «type» »«multi »sub foo(parameters) { instructions ... «return» value; }
| |
| Ruby | foo«(parameters)»
|
def foo«(parameters)»Script error: No such module "Indent".instructions
|
def foo«(parameters)»Script error: No such module "Indent".instructionsScript error: No such module "Indent".«return» value
| |
| Rust | foo(«parameters»)
|
fn foo(«parameters») { instructions }
|
fn foo(«parameters») -> type { instructions }
|
fn main() { instructions }
|
| Scala | foo«(parameters)»
|
def foo«(parameters)»«: Unit =» { instructions }
|
def foo«(parameters)»«: type» = { instructions ... «return» value }
|
def main(args: Array[String]) { instructions }
|
| Windows PowerShell | foo «parameters»
|
function foo { instructions };or function foo { «param(parameters)» instructions }
|
function foo «(parameters)» { instructions ... return value };or function foo { «param(parameters)» instructions ... return value }
|
— |
| Bash shell | foo «parameters»
|
function foo {Script error: No such module "Indent".instructionsor foo () {Script error: No such module "Indent".instructions
|
function foo {Script error: No such module "Indent".instructionsScript error: No such module "Indent".return «exit_code»or foo () { Script error: No such module "Indent".instructionsScript error: No such module "Indent".return «exit_code»
| |
| ||||
| 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
|
foo parameters = return_valueor foo parameters = do
|
«main :: IO ()»
| |
| Eiffel | foo («parameters»)
|
foo («parameters»)Script error: No such module "Indent".requireScript error: No such module "Indent".preconditionsScript error: No such module "Indent".doScript error: No such module "Indent".instructionsScript error: No such module "Indent".ensureScript error: No such module "Indent".postconditionsScript error: No such module "Indent".end
|
foo («parameters»): typeScript error: No such module "Indent".requireScript error: No such module "Indent".preconditionsScript error: No such module "Indent".doScript error: No such module "Indent".instructionsScript error: No such module "Indent".Result := valueScript error: No such module "Indent".ensureScript error: No such module "Indent".postconditionsScript error: No such module "Indent".end
|
[b] |
| CoffeeScript | foo()
|
foo = ->
|
foo = -> value
|
— |
foo parameters
|
foo = () ->
|
foo = ( parameters ) -> value
| ||
| COBOL | CALL "foo" «USING parameters»Script error: No such module "Indent".«exception-handling»[d]
|
«IDENTIFICATION DIVISION.»
|
«IDENTIFICATION DIVISION.»
|
— |
«FUNCTION» foo«(«parameters»)»
|
— | |||
- <templatestyles src="Citation/styles.css"/>^a Pascal requires "
forward;" for forward declarations. - <templatestyles src="Citation/styles.css"/>^b Eiffel allows the specification of an application's root class and feature.
- <templatestyles src="Citation/styles.css"/>^c In Fortran, function/subroutine parameters are called arguments (since
PARAMETERis a language keyword); theCALLkeyword is required for subroutines. - <templatestyles src="Citation/styles.css"/>^d Instead of using
"foo", a string variable may be used instead containing the same value.
Type conversionsScript error: No such module "anchor".
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 | |
|---|---|---|---|---|---|
| Ada[1] | Integer'Value (string_expression)
|
Long_Integer'Value (string_expression)
|
Float'Value (string_expression)
|
Integer'Image (integer_expression)
|
Float'Image (float_expression)
|
| ALGOL 68 with general, and then specific formats | With prior declarations and association of: string buf := "12345678.9012e34 "; file proxy; associate(proxy, buf);
| ||||
get(proxy, ivar);
|
get(proxy, livar);
|
get(proxy, rvar);
|
put(proxy, ival);
|
put(proxy, rval);
| |
getf(proxy, ($g$, ivar));or getf(proxy, ($dddd$, ivar));
|
getf(proxy, ($g$, livar));or getf(proxy, ($8d$, livar));
|
getf(proxy, ($g$, rvar));or getf(proxy, ($8d.4dE2d$, rvar));
|
putf(proxy, ($g$, ival));or putf(proxy, ($4d$, ival));
|
putf(proxy, ($g(width, places, exp)$, rval));or putf(proxy, ($8d.4dE2d$, rval));
| |
| APL | ⍎string_expression
|
⍎string_expression
|
⍎string_expression
|
⍕integer_expression
|
⍕float_expression
|
| 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
|
string = [NSString stringWithFormat
|
| C++ (STL) | «std::»istringstream(string) >> number;
|
«std::»ostringstream o; o << number; string = o.str();
| |||
| C++11 | integer = «std::»stoi(string);
|
long = «std::»stol(string);
|
float = «std::»stof(string); double = «std::»stod(string);
|
string = «std::»to_string(number);
| |
| C# | integer = int.Parse
|
long = long.Parse
|
float = float.Parsedouble = double.Parse
|
string = number
| |
| D | integer = std.conv.to!int
|
long = std.conv.to!long
|
float = std.conv.to!floatdouble = std.conv.to!double
|
string = std.conv.to!string
| |
| Java | integer = Integer.parseInt
|
long = Long.parseLong
|
float = Float.parseFloatdouble = Double.parseDouble
|
string = Integer.toStringstring = String.valueOf
|
string = Float.toStringstring = Double.toString
|
| JavaScript[a] | integer = parseInt(string);
|
float = parseFloat(string);float = new Number (string);float = Number (string);float = +string;
|
string = number.toString ();string = String (number);string = number+"";string = `${number}`
| ||
| Go | integer, error = strconv.Atoi(string)
|
long, error = strconv.ParseInt
|
float, error = strconv.ParseFloat
|
string = strconv.Itoa(integer)string = strconv.FormatIntstring = fmt.Sprint(integer)
|
string = strconv.FormatFloatstring = fmt.Sprint
|
| Rust[d] | string.parse::<i32>()i32::from_str(string)
|
string.parse::<i64>()i64::from_str(string)
|
string.parse::<f64>()f64::from_str(string)
|
integer.to_string()
|
float.to_string()
|
| 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))
| |||
| ISLISP | (setf integer (convert string <integer>))
|
(setf float (convert string <float>))
|
(setf string (convert number <string>))
| ||
| Pascal | integer := StrToInt(string);
|
float := StrToFloat(string);
|
string := IntToStr(integer);
|
string := FloatToStr(float);
| |
| Visual Basic | integer = CInt(string)
|
long = CLng(string)
|
float = CSng(string)
|
string = CStr(number)
|
|
| Visual Basic .NET (can use both VB syntax above and .NET methods shown right) |
integer = Integer.Parse
|
long = Long.Parse
|
float = Single.Parsedouble = Double.Parse
|
string = number
| |
| Xojo | integer = Val(string)
|
long = Val(string)
|
double = Val(string)double = CDbl(string)
|
string = CStr(number)or string = Str(number)
| |
| 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 | READ(string,format) number
|
WRITE(string,format) number
| |||
| PHP | integer = intval(string);or integer = (int)string;
|
float = floatval(string);float = (float)string;
|
string = "$number";or string = strval(number);or string = (string)number;
| ||
| Perl[b] | number = 0 + string;
|
string = "number";
| |||
| Raku | number = +string;
|
string = ~number;
| |||
| Ruby | integer = string.to_ior integer = Integer(string)
|
float = string.to_ffloat = Float(string)
|
string = number.to_s
| ||
| Scala | integer = string.toInt
|
long = string.toLong
|
float = string.toFloatdouble = string.toDouble
|
string = number.toString
| |
| Smalltalk | integer := Integer readFrom: string
|
float := Float readFrom: string
|
string := number asString
| ||
| 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_
|
let float = float_
|
let string = string_
|
let string = string_
| |
| F# | let integer = int string
|
let integer = int64 string
|
let float = float string
|
let string = string number
| |
| Standard ML | val integer = Int
|
val float = Real
|
val string = Int
|
val string = Real
| |
| Haskell (GHC) | number = read string
|
string = show number
| |||
| COBOL | MOVE «FUNCTION» NUMVAL(string)[c] TO number
|
MOVE number TO numeric-edited
| |||
- <templatestyles src="Citation/styles.css"/>^a JavaScript only uses floating point numbers so there are some technicalities.[7]
- <templatestyles src="Citation/styles.css"/>^b Perl doesn't have separate types. Strings and numbers are interchangeable.
- <templatestyles src="Citation/styles.css"/>^c
NUMVAL-CorNUMVAL-Fmay be used instead ofNUMVAL. - <templatestyles src="Citation/styles.css"/>^
str::parseis available to convert any type that has an implementation of thestd::str::FromStrtrait. Bothstr::parseandFromStr::from_strreturn aResultthat contains the specified type if there is no error. The turbofish (::<_>) onstr::parsecan be omitted if the type can be inferred from context.
Standard stream I/O
| read from | write to | ||
|---|---|---|---|
| stdin | stdout | stderr | |
| Ada[1] | Get (x)
|
Put (x)
|
Put (Standard_Error, x)
|
| ALGOL 68 | readf(($format$, x));or getf(stand in, ($format$, x));
|
printf(($format$, x));or putf(stand out, ($format$, x));
|
putf(stand error, ($format$, x));[a]
|
| APL | x←⎕
|
⎕←x
|
⍞←x
|
| C (C99) | scanf(format, &x);or fscanf(stdin, format, &x);[b]
|
printf(format, x);or fprintf(stdout, format, x);[c]
|
fprintf(stderr, format, x);[d]
|
| Objective-C | data = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
|
[[NSFileHandle fileHandleWithStandardOutput] writeData:data];
|
[[NSFileHandle fileHandleWithStandardError] writeData:data];
|
| 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.Erroror Console.Error
|
| D | x = std.stdio.readln()
|
std.stdio.write(x)or std.stdio.writeln(x)or std.stdio.writef(format, x)or std.stdio.writefln(format, x)
|
stderr.write(x)or stderr.writeln(x)or std.stdioor std.stdio
|
| 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);
|
| Go | fmt.Scan(&x)or fmt.Scanf(format, &x)or x = bufio.NewReader(os.Stdin).ReadString('\n')
|
fmt.Println(x)or fmt.Printf(format, x)
|
fmt.Fprintln(osTemplate:Not a typoStderr, x)or fmt.Fprintf(osTemplate:Not a typoStderr, format, x)
|
| Swift | x = readLine() (2.x)
|
print(x) (2.x)println(x) (1.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 Template:Codett format x)
|
| Scheme (R6RS) | (define x (read-line))
|
(display x)or Template:Codett format x)
|
(display x (current-error-port))or Template:Codett format x)
|
| ISLISP | (setf x (read-line))
|
Template:Codett format x)
|
Template:Codett format x)
|
| Pascal | read(x);or readln(x);
|
write(x);or writeln(x);
|
write(stderr, x);or writeln(stderr, x);
|
| Visual Basic | Input« prompt,» x
|
Print xor ? x
|
— |
| Visual Basic .NET | x = Console.Read()or x = Console.ReadLine()
|
Console.Write(«format,»x)or Console.WriteLine(«format, »x)
|
Console.Erroror Console.Error
|
| Xojo | x = StandardInputStream.Read()or x = StandardInputStreame.ReadLine()
|
StandardOutputStream.Write(x)or StandardOutputStream.WriteLine(x)
|
StdErr.Write(x)or StdErr.WriteLine(x)
|
| Python 2.x | x = raw_input(«prompt»)
|
print xor sys.stdout.write(x)
|
Template:Codett xor 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 | READ(*,format) variable names or READ(INPUT_UNIT,format) variable names[e]
|
WRITE(*,format) expressions or WRITE(OUTPUT_UNIT,format) expressions[e]
|
WRITE(ERROR_UNIT,format) expressions[e]
|
| Forth | buffer length ACCEPT ( # chars read )
|
buffer length TYPE
|
— |
| 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;
|
| Raku | $x = $*IN.get;
|
x.printor x.say
|
x.noteor $*ERR.print(x)or $*ERR.say(x)
|
| Ruby | x = gets
|
puts xor 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 xor print_endline stror Printf.printf format x ...
|
prerr_int xor prerr_endline stror 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
|
Template:Codett str)
|
| Haskell (GHC) | x <- readLnor str <- getLine
|
print xor putStrLn str
|
hPrint stderr xor hPutStrLn stderr str
|
| COBOL | ACCEPT x
|
DISPLAY x
|
|
- <templatestyles src="Citation/styles.css"/>^a ALGOL 68 additionally as the "unformatted" transput routines:
read,write,get, andput. - <templatestyles src="Citation/styles.css"/>^b
gets(x)andfgets(x, length, stdin)read unformatted text from stdin. Use of gets is not recommended. - <templatestyles src="Citation/styles.css"/>^c
puts(x)andfputs(x, stdout)write unformatted text to stdout. - <templatestyles src="Citation/styles.css"/>^d
fputs(x, stderr)writes unformatted text to stderr - <templatestyles src="Citation/styles.css"/>^e Template:Mono are defined in the Template:Mono module.[15]
Reading command-line arguments
| Argument values | Argument counts | Program name / Script name | |
|---|---|---|---|
| Ada[1] | Argument (n)
|
Argument_Count
|
Command_Name
|
| C (C99) | argv[n]
|
argc
|
first argument |
| Objective-C | |||
| C++ | |||
| C# | args[n]
|
args.Length
|
Assembly.GetEntryAssembly()
|
| Java | args.length
|
||
| D | first argument | ||
| JavaScript Windows Script Host implementation |
WScript.Arguments(n)
|
WScript.Arguments.length
|
WScript.ScriptNameor WScript.ScriptFullName
|
| Go | os.Args[n]
|
len(os.Args)
|
first argument |
| Rust[a] | std::env::args().nth(n)std::env::args_os().nth(n)
|
std::env::args().count()std::env::args_os().count()
|
std::env::args().next()std::env::args_os().next()
|
| Swift | Process.arguments[n] orProcess.unsafeArgv[n]
|
Process.arguments.count orProcess.argc
|
first argument |
| Common Lisp | ? | ? | ? |
| Scheme (R6RS) | (list-ref (command-line) n)
|
(length (command-line))
|
first argument |
| ISLISP | — | — | — |
| Pascal | ParamStr(n)
|
ParamCount
|
first argument |
| Visual Basic | Command[b]
|
— | App.Path
|
| Visual Basic .NET | CmdArgs(n)
|
CmdArgs.Length
|
[Assembly].GetEntryAssembly().Location
|
| Xojo | System.CommandLine
|
(string parsing) | Application.ExecutableFile.Name
|
| Python | sys.argv[n]
|
len(sys.argv)
|
first argument |
| S-Lang | __argv[n]
|
__argc
|
first argument |
| Fortran | DO i = 1,argcScript error: No such module "Indent".CALL GET_COMMAND_ARGUMENT (i,argv(i))
|
argc = COMMAND_ARGUMENT_COUNT ()
|
CALL GET_COMMAND_ARGUMENT (0,progname)
|
| PHP | $argv[n]
|
$argc
|
first argument |
| Bash shell | $n ($1, $2, $3, ...)$@ (all arguments)
|
$#
|
$0
|
| Perl | $ARGV[n]
|
scalar(@ARGV)
|
$0
|
| Raku | @*ARGS[n]
|
@*ARGS.elems
|
$PROGRAM_NAME
|
| Ruby | ARGV[n]
|
ARGV.size
|
$0
|
| Windows PowerShell | $args[n]
|
$args.Length
|
$MyInvocation.MyCommand
|
| OCaml | Sys.argv.(n)
|
Array.length Sys.argv
|
first argument |
| F# | args.[n]
|
args.Length
|
Assembly.GetEntryAssembly()
|
| Standard ML | List.nth (CommandLine.arguments (), n)
|
length (CommandLine.arguments ())
|
CommandLine.name ()
|
| Haskell (GHC) | do { args <- System.getArgs; return length args !! n}
|
do { args <- System.getArgs; return length args}
|
System.getProgName
|
| COBOL | [c] | — | |
- <templatestyles src="Citation/styles.css"/>^a In Rust,
std::env::argsandstd::env::args_osreturn iterators,std::env::Argsandstd::env::ArgsOsrespectively.Argsconverts each argument to aStringand it panics if it reaches an argument that cannot be converted to UTF-8.ArgsOsreturns a non-lossy representation of the raw strings from the operating system (std::ffi::OsString), which can be invalid UTF-8. - <templatestyles src="Citation/styles.css"/>^b In Visual Basic, command-line arguments are not separated. Separating them requires a split function
Split(string). - <templatestyles src="Citation/styles.css"/>^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
ACCEPTstatements.
Execution of commands
| Shell command | Execute program | Replace current program with new executed program | |
|---|---|---|---|
| Ada[1] | Not part of the language standard. Commonly done by compiler provided packages or by interfacing to C or POSIX.[16] | ||
| C | system("command");
|
execl(path, args);or execv(path, arglist);
| |
| C++ | |||
| Objective-C | [NSTask launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments];
|
||
| C# | System.Diagnostics
|
||
| F# | |||
| Go | exec.Run(path, argv, envv, dir, exec.DevNull, exec.DevNull, exec.DevNull)
|
os.Exec(path, argv, envv)
| |
| Visual Basic | Interaction.Shell(command «, WindowStyle» «, isWaitOnReturn»)
|
||
| Visual Basic .NET | Microsoft.VisualBasic
|
System.Diagnostics
|
|
| Xojo | Shell.Execute(command «, Parameters»)
|
FolderItem.Launch(parameters, activate)
|
— |
| D | std.process.system("command");
|
std.process.execv(path, arglist);
| |
| Java | Runtime.exec(command);or new ProcessBuilder(command).start();
|
||
| JavaScript Windows Script Host implementation |
Template:Codettcommand «, WindowStyle» «, isWaitOnReturn»);
|
WshShell.Exec(command)
|
|
| Common Lisp | (uiop:run-program command)
|
||
| Scheme | (system command)
|
||
| ISLISP | — | — | — |
| Pascal | system(command);
|
||
| OCaml | Sys.command command, Unix.open_process_full command env (stdout, stdin, stderr),...
|
Unix.create_process prog args new_stdin new_stdout new_stderr, ...
|
Unix.execv prog argsor Unix.execve prog args env
|
| Standard ML | OS.Process.system command
|
Unix.execute (path, args)
|
Posix.Process.exec (path, args)
|
| Haskell (GHC) | System.system command
|
System.Process
|
Posix.Process
|
| Perl | system(command)or $output = `command`or $output = qx(command)
|
exec(path, args)
| |
| Ruby | system(command)or output = `command`
|
exec(path, args)
| |
| PHP | system(command)or $output = `command`or exec(command)or passthru(command)
|
||
| Python | os.system(command)or subprocess.Popen(command)
|
subprocess.call(["program", "arg1", "arg2", ...])
|
os.execv(path, args)
|
| S-Lang | system(command)
|
||
| Fortran | CALL EXECUTE_COMMAND_LINE (COMMAND «, WAIT» «, EXITSTAT» «, CMDSTAT» «, CMDMSG»)[a]
|
||
| Windows PowerShell | [Diagnostics.Process]::Start(command)
|
«Invoke-Item »program arg1 arg2 ...
|
|
| Bash shell | output=`command`or output=$(command)
|
program arg1 arg2 ...
|
|
<templatestyles src="Citation/styles.css"/>^a Fortran 2008 or newer.[17]
See also
References
<templatestyles src="Reflist/styles.css" />
- ↑ a b c d e f g h i j k l m n o p Ada Reference Manual – Language and Standard Libraries; ISO/IEC 8652:201x (E), Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ a b Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ a b 8.5 The Number Type
- ↑ a b Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
Script error: No such module "Check for unknown parameters".