FreeBasic | Byte or <nowiki>Integer<8></nowiki> | UByte or <nowiki>UInteger<8></nowiki> | Short or <nowiki>Integer<16></nowiki> | UShort or <nowiki>UInteger<16></nowiki> | Long or <nowiki>Integer<32></nowiki> | ULong or <nowiki>UInteger<32></nowiki> | LongInt or <nowiki>Integer<64></nowiki> | ULongInt or <nowiki>UInteger<64></nowiki> | Integer | UInteger | |-| Python 2.x| colspan=2 | colspan=2 | colspan=2 | colspan=2 | int | | long |-| Python 3.x| colspan=2 | colspan=2 | colspan=2 | colspan=2 | colspan=2 | int |-| S-Lang| colspan=2 | colspan=2 | colspan=2 | colspan=2 | colspan=2 | |-| Fortran| INTEGER<wbr/>(KIND = n) | | INTEGER<wbr/>(KIND = n) | | INTEGER<wbr/>(KIND = n) | | INTEGER<wbr/>(KIND = n) | ||||-| PHP| colspan=2 | colspan=2 | int | | int | | colspan=2 | |-| Perl 5| colspan=2 | colspan=2 | colspan=2 | colspan=2 | colspan=2 | Math::BigInt |-| Raku| int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | Int | colspan=2 |-| Ruby| colspan=2 | colspan=2 | colspan=2 | colspan=2 | Fixnum | | Bignum |-| Erlang| colspan=2 | colspan=2 | colspan=2 | colspan=2 | integer | | integer |-| Scala| Byte | | Short | Char | Int | | Long | | | | scala.math.BigInt |-| Seed7| | | | | | | integer | | | | bigInteger |-| Smalltalk| colspan=2 | colspan=2 | colspan=2 | colspan=2 | SmallInteger | | LargeInteger |-| Windows PowerShell| colspan=2 | colspan=2 | colspan=2 | colspan=2 | colspan=2 | |-| OCaml| colspan=2 | colspan=2 | int32 | | rowspan=2| 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 | colspan=2 | Int32.int | Word32.word | Int64.int | Word64.word | int | word | LargeInt.int or
IntInf.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| 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| colspan=2 | colspan=2 | colspan=2 | colspan=2 | colspan=2 | Integer |-| Wolfram Language| colspan=2 | colspan=2 | colspan=2 | colspan=2 | colspan=2 | Integer |}- The standard constants
int 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 actual sizes of short int , int , and long int are available as the 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 (C99, C++11) 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 (i.e. on a 32-bit machine it is often 32 bits wide; on 64-bit machines it is sometimes 64 bits wide). C99 and C++11 also define the [u]intN_t exact-width types in the stdint.h header. See C syntax#Integral types for more information. In addition the types size_t and ptrdiff_t are defined in relation to the address size to hold unsigned and signed integers sufficiently large to handle array indices and the difference between pointers.
- 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 [4] intrinsic function.
- ALGOL 68G's runtime 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 (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) and Long_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.
- 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.[5]
- When it exceeds one word.[6]
- The standard constants
real 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 actual sizes of short real , real , and long real are available as the 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 [8] intrinsic function.
- ALGOL 68G's runtime 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.
- The value of
n is provided by the SELECTED_REAL_KIND intrinsic function.
- Generic type which can be instantiated with any base floating point type.
Other variable types
| Text | Boolean | Enumeration | Object/Universal |
---|
Character | String |
---|
Ada | Character | String , Bounded_String , Unbounded_String | Boolean | (''item<sub>1</sub>'', ''item<sub>2</sub>'', ''...'') | tagged null record | ALGOL 68 | char | string , bytes | bool , bits | - User defined | | C (C99) | char , wchar_t | | bool | enum ''«name»'' { ''item<sub>1</sub>'', ''item<sub>2</sub>'', ''...'' }; | [[void type|void]] [[pointer (computer programming)|*]] | C++ (STL) | «std::»string | Objective-C | unichar | NSString * | BOOL | id | C# | char | string | bool | enum ''name'' { ''item<sub>1</sub>''« = ''value''», ''item<sub>2</sub>''« = ''value''», ''...'' } | object | Java | String | boolean | enum ''name'' { ''item<sub>1</sub>'', ''item<sub>2</sub>'', ''...'' } | Object | Go | byte , rune | string | bool | const (<br/>{{Spaces|3}}''item{{sub|1}}'' = ''iota''<br/>{{Spaces|3}}''item{{sub|2}}''<br/>{{Spaces|3}}''...''<br/>) | interface{} | Rust | char | String | bool | enum ''name'' { ''item{{sub|1}}''« = ''value''», ''item{{sub|2}}''« = ''value''», ''...'' } | std::any::Any | Swift | Character | String | Bool | enum ''name'' { case ''item<sub>1</sub>'', ''item<sub>2</sub>'', ''...'' } | Any | D | char | string | bool | enum ''name'' { ''item<sub>1</sub>'', ''item<sub>2</sub>'', ''...'' } | std.variant.Variant | Common Lisp | character | string | boolean | (member ''item<sub>1</sub>'' ''item<sub>2</sub>'' ''...'') | t | Scheme | | | | | | ISLISP | | | | | | Pascal (ISO) | char | | boolean | (''item<sub>1</sub>'', ''item<sub>2</sub>'', ''...'') | | Object Pascal (Delphi) | string | variant | Visual Basic | | String | Boolean | Enum ''name''<br/>{{Spaces|3}}''item{{sub|1}}'' «= ''value''»<br/>{{Spaces|3}}''item{{sub|2}}'' «= ''value»<br/>{{Spaces|3}}...''<br/>End Enum | <nowiki>[[Variant type|Variant]]</nowiki> | Visual Basic .NET | Char | Object | Xojo | | Object or Variant | Python | | str | bool | from enum import Enum<br/>class Name(Enum):<br/>{{Spaces|3}}''item{{sub|1}}'' = ''value''<br/>{{Spaces|3}}''item{{sub|2}}'' = ''value''<br/>{{Spaces|3}}''...'' | object | JavaScript | | String | Boolean | | Object | S-Lang | | | | | | Fortran | CHARACTER(LEN = *) | CHARACTER(LEN = :), allocatable | LOGICAL(KIND = n) | | CLASS(*) | PHP | | string | bool | | (type declaration omitted) | Perl | | | | | UNIVERSAL | Raku | Char | Str | Bool | enum ''name<item<sub>1</sub> item<sub>2</sub> ...>''
enum ''name <<:item<sub>1</sub>(value) :item<sub>2</sub>(value) ..>>'' | Mu | Ruby | | String | Object | | Object | Scala | Char | String | Boolean | object ''name'' extends Enumeration {<br/>{{Spaces|3}}''val item{{sub|1}}, item{{sub|2}}, ... = Value''<br/>} | Any | Seed7 | char | string | boolean | const type'': name'' is new enum<br/>{{Spaces|3}}''item{{sub|1}},''<br/>{{Spaces|3}}''item{{sub|2}},''<br/>{{Spaces|3}}''...''<br/>end enum; | | Windows PowerShell | | | | | | OCaml | char | string | bool | | | F# | type ''name'' = ''item<sub>1</sub>'' = ''value'' {{pipe}}''item<sub>2</sub>'' = ''value'' {{pipe}} ''...'' | obj | Standard ML | | | Haskell (GHC) | Char | String | Bool | | | 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 | | String | | | | |
- 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
| fixed size array | dynamic size array |
---|
one-dimensional array | multidimensional array | one-dimensional array | multidimensional array |
---|
Ada | array (''<first> ''..'' <last>'') of'' <type>'' or
array (''<discrete_type>'') of'' <type>'' | array (''<first<sub>1</sub>> ''..'' <last<sub>1</sub>>'','' <first<sub>2</sub>> ''..'' <last<sub>2</sub>>'', ''...'') of'' <type>'' or
array (''<discrete_type<sub>1</sub>>'','' <discrete_type<sub>2</sub>>'','' ...'') of'' <type>'' | array (''<discrete_type> ''range <>) of'' <type>'' | array (''<discrete_type<sub>1</sub>> ''range <>,'' <discrete_type<sub>2</sub>> ''range <>, ''...'') of'' <type>'' | ALGOL 68 | [''first'':''last'']<wbr/>«modename» or simply:
[''size'']<wbr/>«modename» | [''first<sub>1</sub>'':''last<sub>1</sub>'','' first<sub>2</sub>'':''last<sub>2</sub>'']<wbr/>«modename» or
[''first<sub>1</sub>'':''last<sub>1</sub>''][''first<sub>2</sub>'':''last<sub>2</sub>'']<wbr/>«modename» etc. | flex[''first'':''last'']<wbr/>«modename» or simply:
flex[''size'']<wbr/>«modename» | flex[''first<sub>1</sub>'':''last<sub>1</sub>'','' first<sub>2</sub>'':''last<sub>2</sub>'']<wbr/>«modename» or
flex[''first<sub>1</sub>'':''last<sub>1</sub>'']<wbr/>flex[''first<sub>2</sub>'':''last<sub>2</sub>'']<wbr/>«modename» ''etc.'' | C (C99) | ''type name''[''size''] | ''type name''[''size<sub>1</sub>''][''size<sub>2</sub>''] | ''type'' *''name'' or within a block:
''int n = ...; type name''[''n''] | | C++ (STL) | [[std::array|«std::»array]]<''type, size''> (C++11) | | [[std::vector|«std::»vector]]<''type''> | C# | ''type''[] | ''type''[{{Not a typo|,,}}''...''] | System<wbr/>.Collections<wbr/>.ArrayList or
System<wbr/>.Collections<wbr/>.Generic<wbr/>.List<wbr/><''type''> | | Java | ''type''[] | ''type''[][]''...'' | ArrayList ''or'' ArrayList<''type''> | | D | ''type''[''size''] | ''type''[''size<sub>1</sub>''][''size<sub>2</sub>''] | ''type''[] | | Go | [''size'']''type'' | [''size<sub>1</sub>''][''size<sub>2</sub>'']''...type'' | []type | [][]type | Rust | [''type; size''] | [<nowiki/>[''type; size<sub>1</sub>'']''; size<sub>2</sub>''] | Vec<''type''> | Vec<Vec<''type''>> | Swift | | | [''type''] or Array<''type''> | <nowiki>[[</nowiki>''type''<nowiki>]]</nowiki> or Array<Array<''type''>> | Objective-C | NSArray | | NSMutableArray | | JavaScript | | | Array | 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'' | array[''first<sub>1</sub>''..''last<sub>1</sub>''] of array[''first<sub>2</sub>''..''last<sub>2</sub>'']'' ...'' of ''type'' or
array[''first<sub>1</sub>''..''last<sub>1</sub>'','' first<sub>2</sub>''..''last<sub>2</sub>'','' ...''] of ''type'' | | | Object Pascal (Delphi) | array of'' type'' | array of array ''...'' of ''type'' | Visual Basic | Dim x(''last'') As ''type'' | Dim x(''last<sub>1</sub>'', ''last<sub>2</sub>'',''...'') As ''type'' | | | Visual Basic .NET | ''type'' | ''type''({{Not a typo|,,}}''...'') | System<wbr/>.Collections<wbr/>.ArrayList or
System<wbr/>.Collections<wbr/>.Generic<wbr/>.List<wbr/>(Of ''type'') | | Python | | | list | | S-Lang | x = ''type''[''size'']; | x = ''type''[''size<sub>1</sub>'','' size<sub>2</sub>'', ''...'']; | | | Fortran | ''type'' :: ''name''(''size'') | ''type'' :: ''name''(''size''<sub>1</sub>, ''size<sub>2</sub>'',...) | ''type'', ALLOCATABLE :: ''name''(:) | ''type'', ALLOCATABLE :: ''name''(:,:,...) | PHP | | | array | | Perl | | | | | Raku | | | Array[''type''] ''or'' Array of ''type'' | | Ruby | | x = Array.new(''size<sub>1</sub>''){ Array.new(''size<sub>2</sub>'') } | Array | | Scala | Array[''type''] | Array[''...''[Array[''type'']]''...''] | ArrayBuffer[''type''] | | Seed7 | array ''type'' or
array [''idxType''] ''type'' | array array ''type'' or
array [''idxType''] array [''idxType''] ''type'' | array ''type'' or
array [''idxType''] ''type'' | array array ''type'' or
array [''idxType''] array [''idxType''] ''type'' | Smalltalk | Array | | OrderedCollection | | Windows PowerShell | ''type''[] | ''type''[{{Not a typo|,,}}''...''] | | | OCaml | ''type'' array | ''type'' array ''...'' array | | | F# | ''type'' [] or
''type'' array | ''type'' [{{Not a typo|,,}}''...''] | System<wbr/>.Collections<wbr/>.ArrayList or
System<wbr/>.Collections<wbr/>.Generic<wbr/>.List<wbr/><''type''> | | Standard ML | ''type'' vector ''or type'' array | | | | Haskell (GHC) | x = Array.array (0, ''size''-1) ''list_of_association_pairs'' | x = Array.array ((0, 0,''...''), (''size<sub>1</sub>''-1, ''size<sub>2</sub>''-1,''...'')) ''list_of_association_pairs'' | | | COBOL | ''level-number type'' OCCURS ''size'' «TIMES». | | ''level-number type'' OCCURS ''min-size'' TO ''max-size'' «TIMES» DEPENDING «ON» ''size''. | | |
- In most expressions (except the
[[sizeof]] and & operators), values of array types in C are automatically converted to a pointer of its first argument. See C syntax#Arrays for further details of syntax and pointer operations.
- 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
| Simple composite types | Algebraic data types | Unions |
---|
Records | Tuple expression |
---|
Ada | type ''name'' is «abstract» «tagged» «limited» ''[''record{{indent|2}}''field{{sub|1}}'' : ''type'';{{indent|2}}''field{{sub|2}}'' : ''type'';{{indent|2}}''...''<br/>end record ''{{pipe}}'' null record'']'' | | Any combination of records, unions, and enumerations (as well as references to those, enabling recursive types). | type ''name'' (''variation'' : ''discrete_type'') is record{{indent|2}}case ''variation'' is{{indent|4}}when ''choice_list{{sub|1}}'' =>{{indent|6}}''fieldname{{sub|1}}'' : ''type'';{{indent|6}}''...''{{indent|4}}when ''choice_list{{sub|2}}'' =>{{indent|6}}''fieldname{{sub|2}}'' : ''type'';{{indent|6}}''...''{{indent|4}}''...''{{indent|2}}end case;<br/>end record | 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'';''...''}; | [[C++11#Tuple types|«std::»tuple]]<''type{{sub|1}}..type{{sub|n}}''> | C# | struct ''name'' {''type name'';''...''} | (''val{{sub|1}}'', ''val{{sub|2}}'', ''...'') | | rowspan=3 | Java | | | | JavaScript | | | | D | struct ''name'' {''type name'';''...''} | | std.variant.Algebraic''!(type,...)'' | union {''type name'';''...''} | Go | struct { {{indent|2}}''«name» type''{{indent|2}}''...''<br/>} | | | | Rust | struct name {''name:'' type'', ...''} | (''val{{sub|1}}'','' val{{sub|2}}'', ''...'') | enum name { ''Foo''(''types'')'', ...''} | union name {''name:'' type'', ...''} | Swift | struct ''name'' {''{{indent|2}}''var'' name «'':'' type»{{indent|2}}...<br/>''} | (''«name{{sub|1}}'':''» val{{sub|1}}'', ''«name{{sub|2}}'':''» val{{sub|2}}'', ''«name{{sub|3}}'':''» val{{sub|3}}'', ''...'') | 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 ''val{{sub|1}} val{{sub|2}}'') | | | Scheme | | | | ISLISP | | | | Pascal | record{{indent|2}}''name'': ''type'';{{indent|2}}''...<br/>''end | | | record{{indent|2}}case ''type'' of{{indent|2}}''value'': (''types'');{{indent|2}}''...''<br/>end | Visual Basic | | | | | Visual Basic .NET | Structure ''name''{{indent|2}}Dim ''name'' As ''type''{{indent|2}}''...''<br/>End Structure | (''val{{sub|1}}'', ''val{{sub|2}}'', ''...'') | | | Python | | «(''»val{{sub|1}}'', ''val{{sub|2}}'', ''val{{sub|3}}'', ''...'' «)» | | | S-Lang | struct {''name [=value], ...''} | | | | Fortran | TYPE ''name{{indent|2}}type'' :: ''name{{indent|2}}...<br/>''END TYPE | | | | PHP | | | | | Perl | | | | rowspan=3 | Raku | | | | Ruby | | | | Scala | case class ''name''(«''var''» ''name'': ''type'', ''...'') | (''val{{sub|1}}'', ''val{{sub|2}}'', ''val{{sub|3}}'', ''...'') | abstract class ''name''{{indent|2}}case class ''Foo''(«''parameters''») extends ''name''{{indent|2}}case class ''Bar''(«''parameters''») extends ''name''{{indent|2}}''...'' or
abstract class ''name''{{indent|2}}case object ''Foo'' extends ''name''{{indent|2}}case object ''Bar'' extends ''name''{{indent|2}}''...'' or a combination of case classes and case objects | | Windows PowerShell | | | | | OCaml | type ''name'' = {«''mutable''» ''name'' : ''type'';''...''} | «(»''val{{sub|1}}'', ''val{{sub|2}}'', ''val{{sub|3}}'', ''...'' «)» | type ''name'' = ''Foo'' «of ''type''» {{pipe}} ''Bar'' «of ''type''» {{pipe}} ''...'' | rowspan=4 | F# | Standard ML | type ''name'' = {''name'' : ''type'',''...''} | (''val{{sub|1}}'', ''val{{sub|2}}'', ''val{{sub|3}}'', ''...'') | datatype ''name'' = ''Foo'' «of ''type''» {{pipe}} ''Bar'' «of ''type''» {{pipe}} ''...'' | Haskell | data ''Name'' = ''Constr'' {''name'' :: ''type'',''...''} | data ''Name'' = ''Foo'' «''types''» {{pipe}} ''Bar'' «''types''» {{pipe}} ''...'' | COBOL | ''level-number name type clauses''.{{indent|2}}''level-number+n name type clauses''.{{indent|2}}''...'' | | | ''name'' REDEFINES ''variable type''. | |
- 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" (associative arrays) 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
| variable | constant | type synonym |
---|
Ada | ''identifier'' : ''type«'' := ''initial_value»'' | ''identifier'' : constant ''type'' := ''final_value'' | subtype ''identifier'' is ''type'' | ALGOL 68 | modename name« := ''initial_value''»; | modename name = ''value''; | [[typedef|mode]] synonym = modename; | C (C99) | ''type name''« = ''initial_value''»; | enum{ ''name'' = ''value'' }; | [[typedef]] ''type synonym''; | Objective-C | C++ | const ''type name'' = ''value''; | C# | ''type name<sub>1</sub>''« = ''initial_value''», ''name<sub>2</sub>''« = ''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''; | rowspan=2 | JavaScript | var ''name''« = ''initial_value''»; or
let ''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 | 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'' <nowiki>'</nowiki>''type'') |-| Scheme| (define ''name initial_value'') |||-| ISLISP| (defglobal ''name initial_value'') or
(defdynamic ''name initial_value'') | (defconstant ''name value'') | |-| Pascal| ''name'': ''type''« = ''initial_value''» | ''name'' = ''value'' | ''synonym'' = ''type'' |-| Visual Basic| Dim ''name'' «As ''type''» | rowspan=3| See notes to left.Constants use the same syntax, and:- use
Const instead of Dim
- have a restriction to only certain primitive types
Const ''name<sub>1</sub>'' «As ''type''» = ''value'', ''name<sub>2</sub>'' «As ''type»'' = ''value, ...''
||-| 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"):
type_character , available as an alternative to an As clause for some primitive data types;
nullable_specifier ; and
array_specifier ;
and that
- a
modified_identifier is of the form identifier«type_character»«nullable_specifier»«array_specifier» ;
- a
modified_identifier_list is a comma-separated list of two or more occurrences of modified_identifier ; and
- a
declarator_list is a comma-separated list of declarators, which can be of the form''identifier'' As ''object_creation_expression'' (object initializer declarator),
''modified_identifier'' «As ''non_array_type''«''array_rank_specifier''»»« = ''initial_value»'' (single declarator), or
''modified_identifier_list'' «As ''«non_array_type''««''array_rank_specifier''»» (multiple declarator);
valid declaration statements are of the formDim ''declarator_list'' ,where, for the purpose of semantic analysis, to convert the declarator_list to a list of only single declarators:
- The
As clauses of each multiple declarator is distributed over its modified_identifier_list
- The
As New ''type...'' of each object initializer declarator is replaced with As ''type'' = New ''type...''
and for which, for each identifier ,
- a
type_character and As clause do not both appear;
- if an
As clause is present,- an
array_rank_specifier does not appear both as a modification of the identifier and on the type of the As clause;
- an
unmodified_type can be determined, by the rule that,- if a
type_character or As clause is present,unmodified_type is that specified by such construct,
- and that otherwise,
- either
Option Infer must be on and the identifier must have an initializer, in which case unmodified_type is that of the initializer, or
Option Strict must be off, in which case unmodified_type is Object ;
- its
final_type is its unmodified_type prepended before its modifiers;
- its
final_type is a valid type; and
- if an
initial_value is present,- either
Option Strict is on and initial_value has a widening conversion to final_type , or
Option Strict is off and initial_value has a narrowing conversion to final_type .
If Option Explicit is off, variables do not require explicit declaration; they are declared implicitly when used:<nowiki>name = initial_value</nowiki> | Imports ''synonym'' = ''type'' |-| Xojo| Dim ''name'' «As ''type''»« = ''initial_value»'' | |-| Python| ''name«: type»'' = ''initial_value'' | rowspan=2 | ''synonym'' = ''type'' |-| CoffeeScript| ''name'' = ''initial_value'' | ''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'');<br/>const ''name'' = ''value (5.3+)'' | rowspan=2 |-| Perl| «my» $''name''« = ''initial_value''»; | use constant ''name'' => ''value''; |-| Raku| «my «''type''»» ''$name«'' = ''initial_value''»; | «my «''type''»» constant ''name'' = ''value''; | ::''synonym'' ::= ''type'' |-| Ruby| ''name'' = ''initial_value'' | ''Name'' = ''value'' | ''synonym'' = ''type'' |-| 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'' | rowspan=2| let ''name'' «: ''type''» = ''value'' | rowspan=3| type ''synonym'' = ''type'' |-| F#| let mutable ''name'' «: ''type''» = ''value'' |-| Standard ML| val ''name'' «: ''type'' ref» = ref ''value'' | 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'' | | |}
- Pascal has declaration blocks. See functions.
- Types are just regular objects, so you can just assign them.
- In Perl, the "my" keyword scopes the variable into the block.
- Technically, this does not declare name to be a mutable variable—in ML, all names can only be bound once; rather, it declares name to point to a "reference" data structure, which is a simple mutable cell. The data structure can then be read and written to using the
! and <nowiki>:=</nowiki> operators, respectively.
- 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.
- In Rust, if no initial value is given to a
let or let mut variable and it is never assigned to later, there is an "unused variable" warning. If no value is provided for a const or static or static mut variable, there is an error. There is a "non-upper-case globals" error for non-uppercase const variables. After it is defined, a static mut variable can only be assigned to in an unsafe block or function.
| if | else if | select case | conditional expression |
---|
Ada | if ''condition'' then{{indent|2}}''statements''<br/>«else{{indent|2}}''statements''»<br/>end if | if ''condition{{sub|1}}'' then{{indent|2}}''statements''<br/>elsif ''condition{{sub|2}}'' then{{indent|2}}''statements''<br/>''...''<br/>«else{{indent|2}}''statements''»<br/>end if | case ''expression'' is{{indent|2}}when ''value_list''{{sub|1}} => ''statements''{{indent|2}}when ''value_list{{sub|2}}'' => ''statements''{{indent|2}}''...''{{indent|2}}«when others => ''statements''»<br/>end case | (if ''condition{{sub|1}}'' then{{indent|2}}''expression{{sub|1}}''<br/>«elsif ''condition{{sub|2}}'' then{{indent|2}}''expression{{sub|2}}''»<br/>''...''<br/>else{{indent|2}}''expression{{sub|n}}''<br/>) or
(case ''expression'' is{{indent|2}}when ''value_list{{sub|1}}'' => ''expression{{sub|1}}''{{indent|2}}when ''value_list{{sub|2}}'' => ''expression{{sub|2}}''{{indent|2}}''...''{{indent|2}}«when others => ''expression{{sub|n}}''»<br/>) | Seed7 | if ''condition'' then{{indent|2}}''statements''<br/>«else{{indent|2}}''statements''»<br/>end if | if ''condition{{sub|1}}'' then{{indent|2}}''statements''<br/>elsif ''condition{{sub|2}}'' then{{indent|2}}''statements''<br/>''...''<br/>«else{{indent|2}}''statements''»<br/>end if | case ''expression'' of{{indent|2}}when ''set1'' : ''statements''{{indent|2}}''...''{{indent|2}}«otherwise: ''statements''»<br/>end case | | Modula-2 | if ''condition'' then{{indent|2}}''statements''<br/>«else{{indent|2}}''statements''»<br/>end | if ''condition{{sub|1}}'' then{{indent|2}}''statements''<br/>elsif ''condition{{sub|2}}'' then{{indent|2}}''statements''<br/>''...''<br/>«else{{indent|2}}''statements''»<br/>end | case ''expression'' of{{indent|2}}''caseLabelList'' : ''statements'' {{pipe}}{{indent|2}}''...''{{indent|2}}«else ''statements''»<br/>end | | 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 ''condition''{{indent|2}}''instructions''<br/>«:Else{{indent|2}}''instructions''»<br/>:EndIf | :If ''condition''{{indent|2}}''instructions''<br/>:ElseIf ''condition''{{indent|2}}''instructions''<br/>''...''<br/>«:Else{{indent|2}}''instructions''»<br/>:EndIf | :Select ''expression''{{indent|2}}:Case ''case1''{{indent|4}}''instructions''{{indent|2}}''...''{{indent|2}}«:Else{{indent|4}}''instructions''»<br/>:EndSelect | {''condition'':''valueIfTrue'' ⋄ ''valueIfFalse''} | C (C99) | if (''condition'') ''instructions''<br/>«else ''instructions''»
instructions can be a single statement or a block in the form of: { ''statements'' } | if (''condition'') ''instructions''<br/>else if (''condition'') ''instructions''<br/>''...''<br/>«else ''instructions»'' or
if (''condition'') ''instructions''<br/>else { if (''condition'') ''instructions'' } | switch (''variable'') { {{indent|2}}case ''case1'': ''instructions'' «; break;»{{indent|2}}''...''{{indent|2}}«default: ''instructions''»<br/>} | ''condition'' [[?:|?]] ''valueIfTrue'' [[?:|:]] ''valueIfFalse'' | Objective-C | C++ (STL) | D | Java | JavaScript | PHP | C# | if (''condition'') ''instructions''<br/>«else ''instructions''» instructions can be a single statement or a block in the form of: { ''statements'' } | if (''condition'') ''instructions''<br/>else if (''condition'') ''instructions''<br/>''...''<br/>«else ''instructions''» | switch (''variable'')<br/>{ {{indent|2}}case ''case{{sub|1}}'':{{indent|4}}''instructions''{{indent|4}}«''break_or_jump_statement''»{{indent|2}}''...''{{indent|2}}«default:{{indent|4}}''instructions''{{indent|4}}''break_or_jump_statement''»<br/>} All non-empty cases must end with a break or goto case statement (that is, they are not allowed to fall-through to the next case).The default case is not required to come last. | ''condition'' [[?:|?]] ''valueIfTrue'' [[?:|:]] ''valueIfFalse'' | Windows PowerShell | if (''condition'') ''instruction''<br/>«else ''instructions''» | if (''condition'') { ''instructions'' }<br/>elseif (''condition'') { ''instructions'' }<br/>''...''<br/>«else { ''instructions'' }» | switch (''variable'') { ''case1''{''instructions'' «break;» } ''...'' «default { ''instructions'' }»} | | Go | if ''condition'' {''instructions''}<br/>«else {''instructions''}» | if ''condition'' {''instructions''}<br/>else if ''condition'' {''instructions''}<br/>''...''<br/>«else {''instructions''}» or
switch { {{indent|2}}case ''condition'': ''instructions''{{indent|2}}''...''{{indent|2}}«default: ''instructions''»<br/>} | switch ''variable'' { {{indent|2}}case ''case<sub>1</sub>'': ''instructions''{{indent|2}}''...''{{indent|2}}«default: ''instructions''»<br/>} | | Swift | if ''condition'' {''instructions''}<br/>«else {''instructions''}» | if ''condition'' {''instructions''}<br/>else if ''condition'' {''instructions''}<br/>''...''<br/>«else {''instructions''}» | switch ''variable'' { {{indent|2}}case ''case<sub>1</sub>'': ''instructions''{{indent|2}}''...''{{indent|2}}«default: ''instructions''»<br/>} | | Perl | if (''condition'') {''instructions''}<br/>«else {''instructions''}» or
unless (''notcondition'') {''instructions''}<br/>«else {''instructions''}» | if (''condition'') {''instructions''}<br/>elsif (''condition'') {''instructions''}<br/>''...''<br/>«else {''instructions''}» or
unless (''notcondition'') {''instructions''}<br/>elsif (''condition'') {''instructions''}<br/>''...''<br/>«else {''instructions''}» | use feature "switch";<br/>''...''<br/>given (''variable'') { {{indent|2}}when (''case<sub>1</sub>'') { ''instructions'' }{{indent|2}}''...''{{indent|2}}«default { ''instructions'' }»<br/>} | ''condition'' [[?:|?]] ''valueIfTrue'' [[?:|:]] ''valueIfFalse'' | Raku | if ''condition'' {''instructions''}<br/>«else {''instructions''}» or
unless ''notcondition'' {''instructions''} | if ''condition'' {''instructions''}<br/>elsif ''condition'' {''instructions''}<br/>...<br/>«else {''instructions''} | given ''variable'' { {{indent|2}}when ''case<sub>1</sub>'' { ''instructions'' }{{indent|2}}''...''{{indent|2}}«default { ''instructions'' }»<br/>} | ''condition'' [[?:|??]] ''valueIfTrue'' ! | valueIfFalse |
---|
Ruby | if ''condition''{{indent|2}}''instructions''<br/>«else{{indent|2}}''instructions»'' | if ''condition''{{indent|2}}''instructions''<br/>elsif ''condition''{{indent|2}}''instructions''<br/>''...''<br/>«else{{indent|2}}''instructions''»<br/>end | case ''variable''{{indent|2}}when ''case<sub>1</sub>''{{indent|4}}''instructions''{{indent|2}}''...''{{indent|2}}«else{{indent|4}}''instructions''»<br/>end | ''condition'' [[?:|?]] ''valueIfTrue'' [[?:|:]] ''valueIfFalse'' | Scala | if (''condition'') {''instructions''}<br/>«else {''instructions''}» | if (''condition'') {''instructions''}<br/>else if (''condition'') {''instructions''}<br/>...<br/>«else {''instructions''}» | ''expression'' match { {{indent|2}}case ''pattern1'' => ''expression''{{indent|2}}case ''pattern2'' => ''expression''{{indent|2}}''...''{{indent|2}}«case _ => ''expression''»<br/>} | if (''condition'') ''valueIfTrue'' else ''valueIfFalse'' | Smalltalk | ''condition'' ifTrue:{{indent|2}}''trueBlock''<br/>«ifFalse:{{indent|2}}''falseBlock''»<br/>end | | | ''condition'' ifTrue: ''trueBlock'' ifFalse: ''falseBlock'' | Common Lisp | ''(when'' condition{{indent|2}}''instructions'') or
(unless ''condition''{{indent|2}}''instructions'') or
(if ''condition''{{indent|2}}(progn ''instructions''){{indent|2}}«(progn ''instructions'')») | (cond (''condition1 instructions''){{indent|2}}(''condition2 instructions''){{indent|2}}''...''{{indent|2}}«(t ''instructions'')») | (case ''expression''{{indent|2}}(''case1 instructions''){{indent|2}}(''case2 instructions''){{indent|2}}''...''{{indent|2}}«(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 ''condition''{{indent|2}}(progn ''instructions''){{indent|2}}«(progn ''instructions'')») | (cond (''condition1 instructions''){{indent|2}}(''condition2 instructions''){{indent|2}}''...''{{indent|2}}«(t ''instructions'')») | (case ''expression''{{indent|2}}(''case1 instructions''){{indent|2}}(''case2 instructions''){{indent|2}}''...''{{indent|2}}«(t ''instructions'')») | (if ''condition valueIfTrue valueIfFalse'') | Pascal | if ''condition'' then begin{{indent|2}}''instructions''<br/>end<br/>«else begin{{indent|2}}''instructions''<br/>end»' | if ''condition'' then begin{{indent|2}}''instructions''<br/>end<br/>else if ''condition'' then begin{{indent|2}}''instructions''<br/>end<br/>''...''<br/>«else begin{{indent|2}}''instructions''<br/>end» | case ''variable'' of{{indent|2}}''case1'': ''instructions''{{indent|2}}''...''{{indent|2}}«else: ''instructions''»<br/>end | Visual Basic | If ''condition'' Then{{indent|2}}''instructions''<br/>«Else{{indent|2}}''instructions''»<br/>End If Single-line, when instructions are instruction{{sub|1}} : instruction{{sub|2}} : ... :
If ''condition'' Then ''instructions'' «Else ''instructions»'' | If ''condition'' Then{{indent|2}}''instructions''<br/>ElseIf ''condition'' Then{{indent|2}}''instructions''<br/>''...''<br/>«Else{{indent|2}}''instructions''»<br/>End If Single-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» ''variable''{{indent|2}}Case ''case_pattern{{sub|1}}''{{indent|4}}''instructions''{{indent|2}}''...''{{indent|2}}«Case Else{{indent|4}}''instructions''»<br/>End Select | [[IIf]](''condition'', ''valueIfTrue'', ''valueIfFalse'') | Visual Basic .NET | If(''condition'', ''valueIfTrue'', ''valueIfFalse'') | Xojo | Python | if ''condition'' :<br/>{{keypress|Tab}}''instructions''<br/>«else:<br/>{{keypress|Tab}}''instructions»'' | if ''condition'' :<br/>{{keypress|Tab}}''instructions''<br/>elif ''condition'' :<br/>{{keypress|Tab}}''instructions''<br/>''...''<br/>«else:<br/>{{keypress|Tab}}''instructions»'' | Python 3.10+:
match ''variable'':<br/>{{keypress|Tab}}case ''case1'':<br/>{{keypress|Tab}}{{keypress|Tab}}''instructions''<br/>{{keypress|Tab}}case ''case2'':<br/>{{keypress|Tab}}{{keypress|Tab}}''instructions'' | 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'') THEN{{indent|2}}''instructions''<br/>ELSE{{indent|2}}''instructions''<br/>ENDIF | IF (''condition'') THEN{{indent|2}}''instructions''<br/>ELSEIF (''condition'') THEN{{indent|2}}''instructions''<br/>''...''<br/>ELSE{{indent|2}}''instructions''<br/>ENDIF | SELECT CASE(''variable''){{indent|2}}CASE (''case1''){{indent|4}}''instructions''{{indent|2}}''...''{{indent|2}}CASE DEFAULT{{indent|4}}''instructions''<br/>END SELECT | | Forth | ''condition'' IF ''instructions'' « ELSE ''instructions''» THEN | ''condition'' IF ''instructions'' ELSE ''condition'' IF ''instructions'' THEN THEN | ''value'' CASE{{indent|2}}''case'' OF ''instructions'' ENDOF{{indent|2}}''case'' OF ''instructions'' ENDOF{{indent|4}}''default instructions''<br/>ENDCASE | ''condition'' IF ''valueIfTrue ''ELSE ''valueIfFalse'' THEN | OCaml | if ''condition'' then begin ''instructions'' end «else begin ''instructions'' end» | if ''condition'' then begin ''instructions'' end else if ''condition'' then begin ''instructions'' end ''...'' «else begin ''instructions'' end» | match ''value'' with {{indent|2}}''pattern1'' -> ''expression''{{indent|2}}{{pipe}} ''pattern2'' -> ''expression''{{indent|2}}''...''{{indent|2}}«{{pipe}} _ -> ''expression''» | if ''condition'' then ''valueIfTrue'' else ''valueIfFalse'' | F# | Lightweight syntax mode:Either on a single line or with indentation as shown below:if ''condition'' then<br/>{{keypress|Tab}}''instructions''<br/>«else<br/>{{keypress|Tab}}''instructions»'' Verbose syntax mode: Same as Standard ML. | Lightweight syntax mode: Either on a single line or with indentation as shown below:
if ''condition'' then<br/>{{keypress|Tab}}''instructions''<br/>elif ''condition'' then<br/>{{keypress|Tab}}''instructions''<br/>''...''<br/>«else<br/>{{keypress|Tab}}''instructions»'' Verbose syntax mode: Same as Standard ML. | Standard ML | if ''condition'' then «(''»instructions «'')»<br/>else «(''» instructions «'')» | if ''condition'' then «(''»instructions «'')»<br/>else if ''condition'' then «(''» instructions «'')»<br/>''...''<br/>else «(''» instructions «'')» | case ''value'' of{{indent|2}}''pattern1'' => ''expression''{{indent|2}}{{pipe}} ''pattern2'' => ''expression''{{indent|2}}''...''{{indent|2}}«{{pipe}} _ => ''expression»'' | Haskell (GHC) | if ''condition'' then ''expression'' else ''expression'' or
when ''condition'' (do ''instructions'') or
unless ''notcondition'' (do ''instructions'') | ''result'' {{pipe}} ''condition'' = ''expression''{{indent|2}}{{pipe}} ''condition'' = ''expression''{{indent|2}}{{pipe}} otherwise = ''expression'' | case ''value'' of { {{indent|2}}''pattern1'' -> ''expression'';{{indent|2}}''pattern2'' -> ''expression'';{{indent|2}}''...''{{indent|2}}«_ -> ''expression»''<br/>} | Bash shell | if ''condition-command;'' then{{indent|2}}''expression''<br/>«else{{indent|2}}''expression''»<br/>fi | if ''condition-command;'' then{{indent|2}}''expression''<br/>elif ''condition-command;'' then{{indent|2}}''expression''<br/>«else{{indent|2}}''expression''»<br/>fi | case ''"$variable"'' in{{indent|2}}''"$condition1"){{indent|4}}command...{{indent|2}}"$condition2"){{indent|4}}command...''<br/>esac | | CoffeeScript | if ''condition'' then ''expression'' «else ''expression»'' or
if ''condition{{indent|2}}expression''<br/>«else{{indent|2}}''expression»'' or
''expression'' if ''condition'' or
unless ''condition''{{indent|2}}''expression''<br/>«else{{indent|2}}''expression»'' or
''expression'' unless ''condition'' | if ''condition'' then ''expression'' else if ''condition'' then ''expression'' «else ''expression»'' or
if ''condition''{{indent|2}}''expression''<br/>else if ''condition{{indent|2}}expression''<br/>«else{{indent|2}}''expression»'' or
unless ''condition''{{indent|2}}''expression''<br/>else unless ''condition{{indent|2}}expression''<br/>«else{{indent|2}}''expression»'' | switch ''expression''{{indent|2}}when ''condition'' then ''expression''{{indent|2}}else ''expression'' or
switch ''expression''{{indent|2}}when ''condition''{{indent|4}}''expression''{{indent|2}}«else{{indent|4}}''expression»'' | All conditions are expressions. | COBOL | IF ''condition'' «THEN»{{indent|2}}''expression''<br/>«ELSE{{indent|2}}''expression»''. | | EVALUATE ''expression'' «ALSO ''expression...''»{{indent|2}}WHEN ''case-or-condition'' «ALSO ''case-or-condition...»{{indent|4}}expression{{indent|2}}...''{{indent|2}}«WHEN OTHER{{indent|4}}''expression''»<br/>END-EVALUATE | | Rust | if ''condition'' { {{indent|2}}''expression''<br/>}« else { {{indent|2}}''expression''<br/>}» | if ''condition'' { {{indent|2}}''expression''<br/>} else if ''condition'' {''{{indent|2}}expression<br/>''}« else {''{{indent|2}}expression<br/>''}''»'' | match ''variable'' { {{indent|2}}''pattern1'' => ''expression,''{{indent|2}}''pattern2'' => ''expression,''{{indent|2}}''pattern3'' => ''expression,''{{indent|2}}«_ => ''expression''»<br/>} | All conditions are expressions | | if | else if | select case | conditional expression | |
---|
- 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.
- 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.
- 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 {} ).
| while loop | do while loop | (count-controlled) for loop | foreach |
---|
Ada | while ''condition'' loop{{indent|2}}''statements''<br/>end loop | loop{{indent|2}}''statements''{{indent|2}}exit when not ''condition''<br/>end loop | for ''index'' in «reverse» ''[first'' .. ''last {{pipe}} discrete_type]'' loop{{indent|2}}''statements''<br/>end loop | for ''item'' of «reverse» ''iterator'' loop{{indent|2}}''statements''<br/>end loop or
(for [all {{pipe}} some] [in {{pipe}} of] ''[first'' .. ''last {{pipe}} discrete_type {{pipe}} iterator]'' => ''predicate'') | | «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''»{{indent|2}}do ''statements'' od | «while ''statements; condition''»{{indent|2}}do ''statements'' od | «for ''index''» «from ''first''» «by ''increment''» «to ''last''» do ''statements'' od | APL | :While ''condition''{{indent|2}}''statements''<br/>:EndWhile | :Repeat{{indent|2}}''statements''<br/>:Until ''condition'' | :For ''var«s»'' :In ''list''{{indent|2}}''statements''<br/>:EndFor | :For ''var«s»'' :InEach ''list''{{indent|2}}''statements''<br/>:EndFor | 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] for (var ''item'' of ''set'') ''instructions'' | PHP | foreach (range(''first'', ''last'') as $i) ''instructions'' or
for ($i = ''first''; $i <= ''last''; $i++) ''instructions'' | foreach (''set'' as ''item'') ''instructions'' or
foreach (''set'' as ''key'' => ''item'') ''instructions'' | Windows PowerShell | for ($i = ''first''; $i -le ''last''; $i++) ''instructions'' | foreach (''item'' in ''set'') ''instructions'' | 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 ''condition'' 1.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 ''condition'' or
repeat { ''instructions'' } until ''notcondition'' | for ''first''..''last'' -> $i { ''instructions'' } or
loop ($i = ''first''; $i <=''last''; $i++) { ''instructions'' } | for ''set«'' -> ''$item»'' { ''instructions'' } | Ruby | while ''condition''{{indent|2}}''instructions''<br/>end or
until ''notcondition''{{indent|2}}''instructions''<br/>end | begin{{indent|2}}''instructions''<br/>end while ''condition'' or
begin{{indent|2}}''instructions''<br/>end until ''notcondition'' | for i in ''first''..''last''{{indent|2}}''instructions''<br/>end or
for i in ''first''...''last+1''{{indent|2}}''instructions''<br/>end or
''first''.{{not a typo|upto}}(''last'') { {{pipe}}i{{pipe}} ''instructions'' } | for ''item'' in ''set''{{indent|2}}''instructions''<br/>end or
''set''.each { {{pipe}}''item''{{pipe}} ''instructions'' } | Bash shell | while ''condition ;''do{{indent|2}}''instructions''<br/>done or
until ''notcondition ;''do{{indent|2}}''instructions''<br/>done | | for ((''i'' = ''first''; ''i'' <= ''last''; ++''i'')) ; do{{indent|2}}''instructions''<br/>done | for ''item'' in ''set ;''do{{indent|2}}''instructions''<br/>done | 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:{{indent|2}}''loopBlock'' | ''loopBlock'' doWhile:{{indent|2}}''conditionBlock'' | ''first'' to: ''last'' do:{{indent|2}}''loopBlock'' | ''collection'' do:{{indent|2}}''loopBlock'' | Common Lisp | (loop{{indent|2}}while ''condition''{{indent|2}}do{{indent|2}}''instructions'') or
(do (''notcondition''){{indent|2}}''instructions'') | (loop{{indent|2}}do{{indent|2}}''instructions''{{indent|2}}while ''condition'') | (loop{{indent|2}}for i from ''first'' to ''last «by 1»''{{indent|2}}do{{indent|2}}''instructions'') or
(dotimes (i N){{indent|2}}''instructions'') or
(do ((i ''first'' (1+ i))) ((>=i ''last''))<br/>{{indent|2}}''instructions'') | (loop{{indent|2}}for ''item'' in ''list''{{indent|2}}do{{indent|2}}''instructions'') or
(loop{{indent|2}}for ''item'' across ''vector''{{indent|2}}do{{indent|2}}''instructions'') or
(dolist (''item list''){{indent|2}}''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 begin{{indent|2}}''instructions''<br/>end | repeat{{indent|2}}''instructions''<br/>until ''notcondition''; | for ''i'' := ''first'' «step 1» to ''last'' do begin{{indent|2}}''instructions''<br/>end; | for ''item'' in ''set'' do ''instructions'' | Visual Basic | Do While ''condition''{{indent|2}}''instructions''<br/>Loop or
Do Until ''notcondition''{{indent|2}}''instructions''<br/>Loop or
While ''condition''{{indent|2}}''instructions''<br/>Wend (Visual Basic .NET uses End While instead) | Do{{indent|2}}''instructions''<br/>Loop While ''condition'' or
Do{{indent|2}}''instructions''<br/>Loop Until ''notcondition'' | i must be declared beforehand.For ''i'' = ''first'' To ''last'' «Step ''1»{{indent|2}}instructions''<br/>Next i | For Each ''item'' In ''set''{{indent|2}}''instructions''<br/>Next ''item'' | Visual Basic .NET | For i« As ''type»'' = ''first'' To ''last«'' Step ''1»{{indent|2}}instructions''<br/>Next« i» | For Each ''item«'' As ''type»'' In ''set''{{indent|2}}''instructions''<br/>Next''« item»'' | Xojo | While ''condition''{{indent|2}}''instructions''<br/>Wend | Do Until ''notcondition''{{indent|2}}''instructions''<br/>Loop or
Do{{indent|2}}''instructions''<br/>Loop Until ''notcondition'' | Python | while ''condition'' :<br/>{{keypress|Tab}}''instructions''<br/>«else:<br/>{{keypress|Tab}}''instructions»'' | | Python 3.x:
for i in range(''first'', ''last+1''):<br/>{{keypress|Tab}}''instructions''<br/>«else:<br/>{{keypress|Tab}}''instructions»'' Python 2.x:
for i in xrange(''first'', ''last+1''):<br/>{{keypress|Tab}}''instructions''<br/>«else:<br/>{{keypress|Tab}}''instructions»'' | for ''item'' in ''set'':<br/>{{keypress|Tab}}''instructions''<br/>«else:<br/>{{keypress|Tab}}''instructions»'' | S-Lang | while (''condition'') { ''instructions'' } «then ''optional-block»'' | do { ''instructions'' } while (''condition'') «then ''optional-block»'' | for (i = ''first''; i <= ''last''; i++) { ''instructions'' } «then ''optional-block»'' | foreach ''item''(''set'') «using (''what'')» { ''instructions'' } «then ''optional-block»'' | Fortran | DO WHILE (''condition''){{indent|2}}''instructions''<br/>ENDDO | DO{{indent|2}}''instructions''{{indent|2}}IF (''condition'') EXIT<br/>ENDDO | DO ''I'' = ''first'',''last''{{indent|2}}''instructions''<br/>ENDDO | | 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'') ''array'' or
List.iter (fun ''item'' -> ''instructions'') ''list'' | F# | while ''condition'' do<br/>{{keypress|Tab}}''instructions'' | | for i = ''first'' to ''last'' do<br/>{{keypress|Tab}}''instructions'' | for''item'' in ''set'' do<br/>{{keypress|Tab}}''instructions'' or
Seq.iter (fun ''item'' -> ''instructions'') ''set'' | Standard ML | while ''condition'' do (''instructions'') | Array.app (fn ''item'' => ''instructions'') ''array'' or
app (fn ''item'' => ''instructions'') ''list'' | Haskell (GHC) | Control.Monad.forM_ [''first''..''last''] (\i -> do ''instructions'') | Control.Monad.forM_''list'' (\item -> do ''instructions'') | Eiffel | from{{indent|2}}''setup''<br/>until{{indent|2}}''condition''<br/>loop{{indent|2}}''instructions''<br/>end | CoffeeScript | while ''condition''{{indent|2}}''expression'' or
''expression'' while ''condition'' or
while ''condition'' then ''expression'' or
until ''condition''{{indent|2}}''expression'' or
''expression'' until ''condition'' or
until ''expression'' then ''condition'' | | for ''i'' in [''first''..''last'']{{indent|2}}''expression'' or
for ''i'' in [''first''..''last''] then ''expression'' or
''expression'' for ''i'' in [''first''..''last''] | for ''item'' in ''set''{{indent|2}}''expression'' or
for ''item'' in ''set'' then ''expression'' or
''expression'' for ''item'' in ''set'' | COBOL | PERFORM ''procedure-1'' «THROUGH ''procedure-2''» ««WITH» TEST BEFORE» UNTIL ''condition'' or
PERFORM ««WITH» TEST BEFORE» UNTIL ''condition''{{indent|2}}''expression''<br/>END-PERFORM | PERFORM ''procedure-1'' «THROUGH ''procedure-2''» «WITH» TEST AFTER UNTIL ''condition'' or
PERFORM «WITH» TEST AFTER UNTIL ''condition''{{indent|2}}''expression''<br/>END-PERFORM | PERFORM ''procedure-1'' «THROUGH ''procedure-2»'' VARYING ''i'' FROM ''first'' BY ''increment'' UNTIL ''i'' > ''last'' or
PERFORM VARYING ''i'' FROM ''first'' BY ''increment'' UNTIL ''i'' > ''last''{{indent|2}}''expression''<br/>END-PERFORM | | Rust | while ''condition'' { {{indent|2}}''expression''<br/>} | loop { {{indent|2}}''expression''{{indent|4}}if ''condition'' { {{indent|6}}break;{{indent|2}}}<br/>} | for i in ''first''..''last+1'' { {{indent|2}}''expression''<br/>} or
for i in ''first''..=''last'' { {{indent|2}}''expression''<br/>} | for ''item'' in ''set'' { {{indent|2}}''expression''<br/>} or
''set''.into_iter.for_each({{pipe}}''item''{{pipe}} expression); | |
- "
step n" is used to change the loop interval. If "step " is omitted, then the loop interval is 1.
- This implements the universal quantifier ("for all" or "
") as well as the existential quantifier ("there exists" or "
").THRU may be used instead of THROUGH .
«IS» GREATER «THAN» may be used instead of > .
- Type of set expression must implement trait
std::iter::IntoIterator .
|
|