Comparison of programming languages (basic instructions)#Data conversions

{{Short description|None}}

{{Refimprove|date=February 2009}}

{{ProgLangCompare}}

This article compares a large number of programming languages by tabulating their data types, their expression, statement, and declaration syntax, and some common operating-system interfaces.

__TOC__

Conventions of this article

Generally, var, {{var|var}}, or {{var serif|var}} is how variable names or other non-literal values to be interpreted by the reader are represented. The rest is literal code. Guillemets ({{code|«}} and {{code|»}}) enclose optional sections. {{keypress|Tab}} indicates a necessary (whitespace) indentation.

The tables are not sorted lexicographically ascending by programming language name by default, and that some languages have entries in some tables but not others.

{{Clear}}

Type identifiers

= [[Integer (computer science)|Integer]]s =

class="wikitable sortable"

! rowspan=2|

! colspan=2| 8 bit (byte)

! colspan=2| 16 bit (short integer)

! colspan=2| 32 bit

! colspan=2| 64 bit (long integer)

! colspan=2| Word size

! rowspan=2| Arbitrarily precise (bignum)

Signed

! Unsigned

! Signed

! Unsigned

! Signed

! Unsigned

! Signed

! Unsigned

! Signed

! Unsigned

AdaAda Reference Manual – Language and Standard Libraries; ISO/IEC 8652:201x (E), {{cite web |url=http://www.ada-auth.org/standards/12rm/RM-Final.pdf |title=Reference Manual |access-date=2013-07-19 |url-status=dead |archive-url=https://web.archive.org/web/20110427190723/http://www.ada-auth.org/standards/12rm/RM-Final.pdf |archive-date=2011-04-27 }}

| range -2**7 .. 2**7 - 1{{ref|Ada_range|[j]}}

| range 0 .. 2**8 - 1{{ref|Ada_range|[j]}} or mod 2**8{{ref|Ada_mod|[k]}}

| range -2**15 .. 2**15 - 1{{ref|Ada_range|[j]}}

| range 0 .. 2**16 - 1{{ref|Ada_range|[j]}} or mod 2**16{{ref|Ada_mod|[k]}}

| range -2**31 .. 2**31 - 1{{ref|Ada_range|[j]}}

| range 0 .. 2**32 - 1{{ref|Ada_range|[j]}} or mod 2**32{{ref|Ada_mod|[k]}}

| range -2**63 .. 2**63 - 1{{ref|Ada_range|[j]}}

| mod 2**64{{ref|Ada_mod|[k]}}

| Integer{{ref|Ada_range|[j]}}

| range 0 .. 2**Integer'Size - 1{{ref|Ada_range|[j]}} or mod Integer'Size{{ref|Ada_mod|[k]}}

| {{n/a}}

rowspan=2| ALGOL 68 (variable-width)

| rowspan=2| short short int{{ref|CInt|[c]}}

| rowspan=2 {{n/a}}

| rowspan=2| short int{{ref|CInt|[c]}}

| rowspan=2 {{n/a}}

| rowspan=2| int{{ref|CInt|[c]}}

| rowspan=2 {{n/a}}

| rowspan=2| long int{{ref|CInt|[c]}}

| rowspan=2 {{n/a}}

| int{{ref|CInt|[c]}}

| {{n/a}}

| rowspan=2| long long int{{ref|a68|[a]}}{{ref|a68g|[g]}}

colspan=2 align=center|bytes and bits
C (C99 fixed-width)

| rowspan=2| int8_t

| rowspan=2| uint8_t

| rowspan=2| int16_t

| rowspan=2| uint16_t

| rowspan=2| int32_t

| rowspan=2| uint32_t

| rowspan=2| int64_t

| rowspan=2| uint64_t

| rowspan=2| intptr_t{{ref|CInt|[c]}}

| rowspan=2| size_t{{ref|CInt|[c]}}

| rowspan=5 {{n/a}}

C++ (C++11 fixed-width)
C (C99 variable-width)

| rowspan=2| signed char

| rowspan=2| unsigned char

| rowspan=2| short{{ref|CInt|[c]}}

| rowspan=2| unsigned short{{ref|CInt|[c]}}

| rowspan=2| long{{ref|CInt|[c]}}

| rowspan=2| unsigned long{{ref|CInt|[c]}}

| rowspan=2| long long{{ref|CInt|[c]}}

| rowspan=2| unsigned long long{{ref|CInt|[c]}}

| rowspan=2| int{{ref|CInt|[c]}}

| rowspan=2| unsigned int{{ref|CInt|[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

| rowspan=2| short

| ushort

| rowspan=2| int

| uint

| rowspan=2| long

| ulong

| IntPtr

| UIntPtr

| System.Numerics.BigInteger
(.NET 4.0)

Java

| byte

| {{n/a}}

| char{{ref|Java char|[b]}}

| {{n/a}}

| {{n/a}}

| {{n/a}}

| {{n/a}}

| java.math.BigInteger

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

| {{n/a}}

Swift

| Int8

| UInt8

| Int16

| UInt16

| Int32

| UInt32

| Int64

| UInt64

| Int

| UInt

D

| byte

| ubyte

| short

| ushort

| int

| uint

| long

| ulong

| {{n/a}}

| {{n/a}}

| BigInt

Common Lisp{{cite web|url=http://www.lispworks.com/documentation/HyperSpec/Front/index.htm|title=Common Lisp HyperSpec (TM)|work=lispworks.com|access-date=30 January 2017}}

| (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{{cite web|url=http://www.islisp.info/specification.html|title=www.islisp.info: Specification|work=islisp.info|access-date=30 January 2017|archive-url=https://web.archive.org/web/20160122121427/http://islisp.info/specification.html|archive-date=22 January 2016|url-status=dead}}

|

|

|

|

|

|

|

|

|

|

| bignum

Pascal (FPC)

| shortint

| byte

| smallint

| word

| longint

| longword

| int64

| qword

| integer

| cardinal

| {{n/a}}

Visual Basic

| {{n/a}}

| rowspan=2|Byte

| Integer

| {{n/a}}

| Long

| {{n/a}}

| colspan=2 {{n/a}}

| rowspan=2 colspan=2 {{n/a}}

| rowspan=1 {{n/a}}

Visual Basic .NET

| SByte

| Short

| UShort

| Integer

| UInteger

| Long

| ULong

| System.Numerics.BigInteger
(.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

| {{n/a}}

Python 2.x

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| int

| {{n/a}}

| long

Python 3.x

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| int

S-Lang

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| {{n/a}}

Fortran

| INTEGER(KIND = n){{ref|Kinds|[f]}}

| {{n/a}}

| INTEGER(KIND = n){{ref|Kinds|[f]}}

| {{n/a}}

| INTEGER(KIND = n){{ref|Kinds|[f]}}

| {{n/a}}

| INTEGER(KIND = n){{ref|Kinds|[f]}}

| {{n/a}}

|

|

|

PHP

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| int{{ref|PHP 32/64 bit long|[m]}}

| {{n/a}}

| int{{ref|PHP 32/64 bit long|[m]}}

| {{n/a}}

| colspan=2 {{n/a}}

| {{ref|PHP bignum|[e]}}

Perl 5

| colspan=2 {{n/a}}{{ref|scalars|[d]}}

| colspan=2 {{n/a}}{{ref|scalars|[d]}}

| colspan=2 {{n/a}}{{ref|scalars|[d]}}

| colspan=2 {{n/a}}{{ref|scalars|[d]}}

| colspan=2 {{n/a}}{{ref|scalars|[d]}}

| Math::BigInt

Raku

| int8

| uint8

| int16

| uint16

| int32

| uint32

| int64

| uint64

| Int

| colspan=2 {{n/a}}

Ruby

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| Fixnum

| {{n/a}}

| Bignum

Erlang{{ref|Erlang_int|[n]}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| integer()

| {{n/a}}

| integer(){{ref|Erlang_arb|[o]}}

Scala

| Byte

| {{n/a}}

| Short

| Char{{ref|Scala Char|[l]}}

| Int

| {{n/a}}

| Long

| {{n/a}}

| {{n/a}}

| {{n/a}}

| scala.math.BigInt

Seed7

| {{n/a}}

| {{n/a}}

| {{n/a}}

| {{n/a}}

| {{n/a}}

| {{n/a}}

| integer

| {{n/a}}

| {{n/a}}

| {{n/a}}

| bigInteger

Smalltalk

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| SmallInteger{{ref|Smalltalk|[i]}}

| {{n/a}}

| LargeInteger{{ref|Smalltalk|[i]}}

Windows PowerShell

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| {{n/a}}

OCaml

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| int32

| {{n/a}}

| rowspan=2| int64

| {{n/a}}

| int or nativeint

|

| open Big_int;; or big_int

F#

| sbyte

| byte

| int16

| uint16

| int32 or int

| uint32

| uint64

| nativeint

| unativeint

| bigint

Standard ML

| {{n/a}}

| Word8.word

| colspan=2 {{n/a}}

| 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

| {{n/a}}

COBOL{{ref|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

| {{n/a}}

| {{n/a}}

| {{n/a}}

Mathematica

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| Integer

Wolfram Language

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| colspan=2 {{n/a}}

| Integer

  • {{note|a68|a}} The standard constants int shorts and int lengths can be used to determine how many shorts and longs 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.
  • {{note|Java char|b}} Commonly used for characters.
  • {{note|CInt|c}} The ALGOL 68, C and C++ languages do not specify the exact width of the integer types short, int, long, and (C99, C++11) long long, so they are implementation-dependent. In C and C++ short, long, 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{{citation needed|date=December 2011}} also define the [u]intN_t exact-width types in the stdint.h header. See C syntax#Integral types for more information. In addition the 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.
  • {{note|scalars|d}} Perl 5 does not have distinct types. Integers, floating point numbers, strings, etc. are all considered "scalars".
  • {{note|PHP bignum|e}} PHP has two arbitrary-precision libraries. The BCMath library just uses strings as datatype. The GMP library uses an internal "resource" type.
  • {{note|Kinds|f}} The value of n is provided by the SELECTED_INT_KIND{{cite web|url=http://fortranwiki.org/fortran/show/selected_int_kind|title=selected_int_kind in Fortran Wiki|work=fortranwiki.org|access-date=30 January 2017}} intrinsic function.
  • {{note|a68g|g}} ALGOL 68G's runtime option --precision "number" can set precision for long long ints 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.
  • {{note|Cobol|h}} COBOL allows the specification of a required precision and will automatically select an available type capable of representing the specified precision. "PIC S9999", for example, would require a signed variable of four decimal digits precision. If specified as a binary field, this would select a 16-bit signed type on most platforms.
  • {{note|Smalltalk|i}} Smalltalk automatically chooses an appropriate representation for integral numbers. Typically, two representations are present, one for integers fitting the native word size minus any tag bit ({{mono|SmallInteger}}) and one supporting arbitrary sized integers ({{mono|LargeInteger}}). Arithmetic operations support polymorphic arguments and return the result in the most appropriate compact representation.
  • {{note|Ada_range|j}} Ada range types are checked for boundary violations at run-time (as well as at compile-time for static expressions). Run-time boundary violations raise a "constraint error" exception. Ranges are not restricted to powers of two. Commonly predefined Integer subtypes are: Positive (range 1 .. Integer'Last) and Natural (range 0 .. Integer'Last). Short_Short_Integer (8 bits), Short_Integer (16 bits) 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.
  • {{note|Ada mod|k}} Ada modulo types implement modulo arithmetic in all operations, i.e. no range violations are possible. Modulos are not restricted to powers of two.
  • {{note|Scala char|l}} Commonly used for characters like Java's char.
  • {{note|PHP 32/64 bit long|m}} int in PHP has the same width as long type in C has on that system.{{ref|CInt|[c]}}
  • {{note|Erlang int|n}} Erlang is dynamically typed. The type identifiers are usually used to specify types of record fields and the argument and return types of functions.{{cite web|url=http://www.erlang.org/doc/reference_manual/typespec.html|title=Erlang {{--}} Types and Function Specifications|work=erlang.org|access-date=30 January 2017}}
  • {{note|Erlang arb|o}} When it exceeds one word.{{cite web|url=http://www.erlang.org/doc/efficiency_guide/advanced.html|title=Erlang {{--}} Advanced|work=erlang.org|access-date=30 January 2017}}

= [[Floating point]] =

class="wikitable sortable"

!

! Single precision

! Double precision

! Other precision

! Processor dependent

Ada

| Float

| Long_Float

| {{n/a}}

ALGOL 68

| real{{ref|a68|[a]}}

| long real{{ref|a68|[a]}}

| short real, long long real, etc.{{ref|a68g|[d]}}

C

| rowspan=3| float{{ref|lax floats|[b]}}

| rowspan=5| double

| rowspan=3| long double{{ref|C-long-double|[f]}}

C++ (STL)
Objective-C (Cocoa)

| CGFloat

C#

| rowspan=2| float

| rowspan=3 {{n/a}}

Java
Go

| float32

| float64

Rust

| f32

| f64

| f16, f128

Swift

| Float or Float32

| Double or Float64

| Float80{{ref|Swift-long-double|[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

| rowspan=3| Single

| rowspan=3| Double

| rowspan=3 {{n/a}}

Visual Basic .NET
Xojo
Python

| rowspan=2 {{n/a}}

| float

|

JavaScript

| Number[https://www.mozilla.org/js/language/E262-3.pdf 8.5 The Number Type]

| {{n/a}}

S-Lang

|

|

|

Fortran

| colspan=2|REAL(KIND = n){{ref|real inds|[c]}}

|

PHP

|

| float

|

Perl

|

|

|

Raku

| num32

| num64

|

| Num

Ruby

| {{n/a}}

| Float

| rowspan=4 {{n/a}}

Scala

| Float

| Double

Seed7

| {{n/a}}

| float

Smalltalk

| Float

| Double

Windows PowerShell

|

|

|

OCaml

| {{n/a}}

| rowspan=2| float

| rowspan=3 {{n/a}}

F#

| float32

Standard ML

| {{n/a}}

|

| real

Haskell (GHC)

| Float

| Double

|

Eiffel

| REAL_32

| REAL_64

|

COBOL

| FLOAT-BINARY-7{{ref|Cobol_ieee|[e]}}

| FLOAT-BINARY-34{{ref|Cobol_ieee|[e]}}

| FLOAT-SHORT, FLOAT-LONG, FLOAT-EXTENDED

Mathematica

| {{n/a}}

| {{n/a}}

|

| Real

  • {{note|a68_real|a}} The standard constants real shorts and real lengths can be used to determine how many shorts and longs 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.
  • {{note|lax floats|b}} declarations of single precision often are not honored
  • {{note|real kinds|c}} The value of n is provided by the SELECTED_REAL_KIND{{cite web|url=http://fortranwiki.org/fortran/show/selected_real_kind|title=selected_real_kind in Fortran Wiki|work=fortranwiki.org|access-date=30 January 2017}} intrinsic function.
  • {{note|a68g-real|d}} ALGOL 68G's runtime option --precision "number" can set precision for long long reals 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.
  • {{note|Cobol-ieee|e}} These IEEE floating-point types will be introduced in the next COBOL standard.
  • {{note|C-long-double|f}} Same size as double on many implementations.
  • {{note|Swift-long-double|g}} Swift supports 80-bit extended precision floating point type, equivalent to long double in C languages.

= [[Complex number]]s =

class="wikitable sortable"

!

! Integer

! Single precision

! Double precision

! Half and Quadruple precision etc.

Ada

| {{n/a}}

| Complex{{ref|generic type|[b]}}

| Complex{{ref|generic type|[b]}}

| Complex{{ref|generic type|[b]}}

ALGOL 68

| {{n/a}}

| compl

| long compl etc.

| short compl etc. and long long compl etc.

C (C99){{cite web|url=https://www.gnu.org/software/libc/manual/html_node/Complex-Numbers.html#Complex-Numbers|title=The GNU C Library: Complex Numbers|work=gnu.org|access-date=30 January 2017}}

| {{n/a}}

| float complex

| double complex

| rowspan=7 {{n/a}}

C++ (STL)

| {{n/a}}

| std::complex

| std::complex

C#

| {{n/a}}

| {{n/a}}

| System.Numerics.Complex
(.NET 4.0)

Java

| {{n/a}}

| {{n/a}}

| {{n/a}}

Go

| {{n/a}}

| complex64

| complex128

D

| {{n/a}}

| cfloat

| cdouble

Objective-C

| {{n/a}}

| {{n/a}}

| {{n/a}}

Common Lisp

| (complex integer)

| (complex single-float)

| (complex double-float)

| complex

Scheme

|

|

|

| rowspan=4 {{n/a}}

Pascal

| {{n/a}}

| {{n/a}}

|

Visual Basic

| {{n/a}}

| {{n/a}}

|

Visual Basic .NET

| {{n/a}}

| {{n/a}}

| System.Numerics.Complex
(.NET 4.0)

Perl

|

|

| Math::Complex

|

Raku

|

| complex64

| complex128

| Complex

Python

|

|

| complex

| rowspan=15 {{n/a}}

JavaScript

| {{n/a}}

| {{n/a}}

|

S-Lang

| {{n/a}}

| {{n/a}}

|

Fortran

|

| colspan=2|COMPLEX(KIND = n){{ref|complex kinds|[a]}}

Ruby

| Complex

| {{n/a}}

| Complex

Scala

| {{n/a}}

| {{n/a}}

| {{n/a}}

Seed7

| {{n/a}}

| {{n/a}}

| complex

Smalltalk

| Complex

| Complex

| Complex

Windows PowerShell

| {{n/a}}

| {{n/a}}

|

OCaml

| {{n/a}}

| {{n/a}}

| Complex.t

F#

|

|

| System.Numerics.Complex
(.NET 4.0)

Standard ML

| {{n/a}}

| {{n/a}}

| {{n/a}}

Haskell (GHC)

| {{n/a}}

| Complex{{Not a typo|.}}Complex Float

| Complex{{Not a typo|.}}Complex Double

Eiffel

| {{n/a}}

| {{n/a}}

| {{n/a}}

COBOL

| {{n/a}}

| {{n/a}}

| {{n/a}}

Mathematica

| Complex

| {{n/a}}

| {{n/a}}

| Complex

  • {{note|complex kinds|a}} The value of n is provided by the SELECTED_REAL_KIND intrinsic function.
  • {{note|generic type|b}} Generic type which can be instantiated with any base floating point type.

= Other variable types =

class="wikitable sortable"

! rowspan=2|

! colspan=2| Text

! rowspan=2| Boolean

! rowspan=2| Enumeration

! rowspan=2| Object/Universal

Character

! String{{ref|string|[a]}}

Ada

| Character

| String, Bounded_String, Unbounded_String

| Boolean

| (item1, item2, ...)

| tagged null record

ALGOL 68

| char

| string, bytes

| bool, bits

| {{n/a}} - [http://rosettacode.org/wiki/Enumerations#ALGOL_68 User defined]

| {{n/a}}

C (C99)

| rowspan=2| char, wchar_t

| {{n/a}}

| rowspan=2| bool{{ref|int bool|[b]}}

| rowspan=3| enum «name» { item1, item2, ... };

| rowspan=2| void *

C++ (STL)

| «std::»string

Objective-C

| unichar

| NSString *

| BOOL

| id

C#

| rowspan=2| char

| string

| bool

| enum name { item1« = value», item2« = value», ... }

| object

Java

| String

| boolean

| enum name { item1, item2, ... }

| Object

Go

| byte, rune

| string

| bool

| const (
{{Spaces|3}}item{{sub|1}} = iota
{{Spaces|3}}item{{sub|2}}
{{Spaces|3}}...
)

| 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 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)

| rowspan=2| char

| {{n/a}}

| rowspan=2| boolean

| rowspan=2| ( item1, item2, ... )

| {{n/a}}

Object Pascal (Delphi)

| string

| variant

Visual Basic

| {{n/a}}

| rowspan=3| String

| rowspan=3| Boolean

| rowspan=3| Enum name
{{Spaces|3}}item{{sub|1}} «= value»
{{Spaces|3}}item{{sub|2}} «= value»
{{Spaces|3}}...

End Enum

| Variant

Visual Basic .NET

| Char

| Object

Xojo

| {{n/a}}

| Object or Variant

Python

| {{n/a}}{{ref|string as char|[d]}}

| str

| bool

| from enum import Enum
class Name(Enum):
{{Spaces|3}}item{{sub|1}} = value
{{Spaces|3}}item{{sub|2}} = value
{{Spaces|3}}...

| object

JavaScript

| {{n/a}}{{ref|string as char|[d]}}

| String

| Boolean

|

| Object

S-Lang

|

|

|

|

|

Fortran

| CHARACTER(LEN = *)

| CHARACTER(LEN = :), allocatable

| LOGICAL(KIND = n){{ref|logical kinds|[f]}}

|

| CLASS(*)

PHP

| {{n/a}}{{ref|string as char|[d]}}

| string

| bool

|

| (type declaration omitted)

Perl

| {{n/a}}{{ref|string as char|[d]}}

|

|

|

| UNIVERSAL

Raku

| Char

| Str

| Bool

| enum name1 item2 ...>
enum name <<:item1(value) :item2(value) ..>>

| Mu

Ruby

| {{n/a}}{{ref|string as char|[d]}}

| String

| Object{{ref|Ruby's bool|[c]}}

|

| Object

Scala

| Char

| String

| Boolean

| object name extends Enumeration {
{{Spaces|3}}val item{{sub|1}}, item{{sub|2}}, ... = Value
}

| Any

Seed7

| char

| string

| boolean

| const type: name is new enum
{{Spaces|3}}item{{sub|1}},
{{Spaces|3}}item{{sub|2}},
{{Spaces|3}}...
end enum;

|

Windows PowerShell

|

|

|

|

|

OCaml

| rowspan=3| char

| rowspan=3| string

| rowspan=3| bool

| {{n/a}}{{ref|enum|[e]}}

| {{n/a}}

F#

| type name = item1 = value {{pipe}}item2 = value {{pipe}} ...

| obj

Standard ML

| {{n/a}}{{ref|enum|[e]}}

| {{n/a}}

Haskell (GHC)

| Char

| String

| Bool

| {{n/a}}{{ref|enum|[e]}}

| {{n/a}}

Eiffel

| CHARACTER

| STRING

| BOOLEAN

| {{n/a}}

| ANY

COBOL

| PIC X

| PIC X(string length) or PIC X«X...»

| PIC 1«(number of digits or PIC 1«1...»

| {{n/a}}

| OBJECT REFERENCE

Mathematica

| {{n/a}}{{ref|string as char|[d]}}

| String

|

|

| {{n/a}}

  • {{note|string|a}} specifically, strings of arbitrary length and automatically managed.
  • {{note|int bool|b}} This language represents a boolean as an integer where false is represented as a value of zero and true by a non-zero value.
  • {{note|Ruby's bool|c}} All values evaluate to either true or false. Everything in TrueClass evaluates to true and everything in FalseClass evaluates to false.
  • {{note|string as char|d}}This language does not have a separate character type. Characters are represented as strings of length 1.
  • {{note|enum|e}} Enumerations in this language are algebraic types with only nullary constructors
  • {{note|logical kinds|f}} The value of n is provided by the SELECTED_INT_KIND intrinsic function.

Derived types

= [[Array data type|Array]] =

{{Further|Comparison of programming languages (array)}}

class="wikitable sortable"
valign="top"

! rowspan=2|

! colspan=2| fixed size array

! colspan=2| dynamic size array

valign="top"

! one-dimensional array

! multidimensional array

! one-dimensional array

! multidimensional array

valign="top"

| Ada

| 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 <>, 2> range <>, ...) of <type>

valign="top"

| ALGOL 68

| [first:last]«modename»
or simply:
[size]«modename»

| [first1:last1, first2:last2]«modename»
or
[first1:last1][first2:last2]«modename»
etc.

| flex[first:last]«modename»
or simply:
flex[size]«modename»

| flex[first1:last1, first2:last2]«modename»
or
flex[first1:last1]flex[first2:last2]«modename» etc.

valign="top"

| C (C99)

| type name[size]{{ref|C's array|[a]}}

| type name[size1][size2]{{ref|C's array|[a]}}

| type *name
or within a block:
int n = ...; type name[n]

|

valign="top"

| C++ (STL)

| colspan=1| «std::»array<type, size>(C++11)

|

| colspan=2| «std::»vector<type>

valign="top"

| C#

| type[]

| type[{{Not a typo|,,}}...]

| System.Collections.ArrayList
or
System.Collections.Generic.List<type>

|

valign="top"

| Java

| type[]{{ref|Java's array|[b]}}

| type[][]...{{ref|Java's array|[b]}}

| ArrayList or ArrayList<type>

|

valign="top"

| D

| type[size]

| type[size1][size2]

| type[]

|

valign="top"

| Go

| [size]type

| [size1][size2]...type

| []type

| [][]type

valign="top"

| Rust

| [type; size]

| [[type; size1]; size2]

| Vec<type>

| Vectype>>

valign="top"

| Swift

|

|

| [type] or Array<type>

| type or Arraytype>>

valign="top"

| Objective-C

| NSArray

|

| NSMutableArray

|

valign="top"

| JavaScript

| {{n/a}}

| {{n/a}}

| colspan=2|Array{{ref|JavaScript's array|[d]}}

valign="top"

| Common Lisp

| (simple-array type (dimension))

| (simple-array type (dimension1 dimension2))

| (array type (dimension))

| (array type (dimension1 dimension2))

valign="top"

| Scheme

|

|

|

|

valign="top"

| ISLISP

|

|

|

|

valign="top"

| Pascal

| rowspan=2|array[first..last] of type{{ref|subrange|[c]}}

| rowspan=2|array[first1..last1] of array[first2..last2] ... of type{{ref|subrange|[c]}}
or
array[first1..last1, first2..last2, ...] of type{{ref|subrange|[c]}}

| {{n/a}}

| {{n/a}}

valign="top"

| Object Pascal (Delphi)

| array of type

| array of array ... of type

valign="top"

| Visual Basic

| Dim x(last) As type

| Dim x(last1, last2,...) As type

|

|

valign="top"

| Visual Basic .NET

| type()

| type({{Not a typo|,,}}...)

| System.Collections.ArrayList
or
System.Collections.Generic.List(Of type)

|

valign="top"

| Python

|

|

| list

|

valign="top"

| S-Lang

| x = type[size];

| x = type[size1, size2, ...];

|

|

valign="top"

| Fortran

| type :: name(size)

| type :: name(size1, size2,...)

| type, ALLOCATABLE :: name(:)

| type, ALLOCATABLE :: name(:,:,...)

valign="top"

| PHP

|

|

| array

|

valign="top"

| Perl

|

|

|

|

valign="top"

| Raku

|

|

| Array[type] or Array of type

|

valign="top"

| Ruby

|

| x = Array.new(size1){ Array.new(size2) }

| Array

|

valign="top"

| Scala

| Array[type]

| Array[...[Array[type]]...]

| ArrayBuffer[type]

|

valign="top"

| 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

valign="top"

| Smalltalk

| Array

|

| OrderedCollection

|

valign="top"

| Windows PowerShell

| type[]

| type[{{Not a typo|,,}}...]

|

|

valign="top"

| OCaml

| type array

| type array ... array

|

|

valign="top"

| F#

| type []
or
type array

| type [{{Not a typo|,,}}...]

| System.Collections.ArrayList
or
System.Collections.Generic.List<type>

|

valign="top"

| Standard ML

| type vector or type array

|

|

|

valign="top"

| 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

|

|

valign="top"

| COBOL

| level-number type OCCURS size «TIMES».

| {{em|one-dimensional array definition...}}

| level-number type OCCURS min-size TO max-size «TIMES» DEPENDING «ON» size.{{ref|COBOL DEPENDING ON clause|[e]}}

| {{n/a}}

  • {{note|C's array|a}}In most expressions (except the sizeof and & operators), values of array types in C are automatically converted to a pointer of its first argument. See C syntax#Arrays for further details of syntax and pointer operations.
  • {{note|Java's array|b}} The C-like type x[] works in Java, however type[] x is the preferred form of array declaration.
  • {{note|subrange|c}} Subranges are used to define the bounds of the array.
  • {{note|JavaScript's array|d}} JavaScript's array are a special kind of object.
  • {{note|COBOL DEPENDING ON clause|e}} The DEPENDING ON clause in COBOL does not create a true variable length array and will always allocate the maximum size of the array.

= Other types =

class="wikitable sortable"

! rowspan=2|

! colspan=2|Simple composite types

! rowspan=2|Algebraic data types

! rowspan=2|Unions

Records

! Tuple expression

Ada

| type name is «abstract» «tagged» «limited» [record{{indent|2}}field{{sub|1}} : type;{{indent|2}}field{{sub|2}} : type;{{indent|2}}...
end record {{pipe}} null record]

| {{n/a}}

| 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;
end record

ALGOL 68

| struct (modename «fieldname», ...);

| colspan=2 align=center| Required types and operators can be user-defined

| union (modename, ...);

C (C99)

| rowspan=2| struct «name» {type name;...};

| rowspan=2 {{n/a}}

| rowspan=3 {{n/a}}

| rowspan=3| union {type name;...};

Objective-C
C++

| struct «name» {type name;...};{{ref|C++'s struct|[b]}}

| «std::»tuple<type{{sub|1}}..type{{sub|n}}>

C#

| struct name {type name;...}

| (val{{sub|1}}, val{{sub|2}}, ... )

|

| rowspan=3 {{n/a}}

Java

| {{n/a}}{{ref|just classes|[a]}}

|

|

JavaScript

|

| {{n/a}}

|

D

| struct name {type name;...}

|

| std.variant.Algebraic!(type,...)

| union {type name;...}

Go

| struct { {{indent|2}}«name» type{{indent|2}}...
}

|

|

|

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}}...
}

| («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) ...)

| rowspan=3| (cons val{{sub|1}} val{{sub|2}}){{ref|pair only|[c]}}

|

|

Scheme

| {{n/a}}

|

|

ISLISP

|

|

|

Pascal

| record{{indent|2}}name: type;{{indent|2}}...
end

| {{n/a}}

| {{n/a}}

| record{{indent|2}}case type of{{indent|2}}value: (types);{{indent|2}}...
end

Visual Basic

|

|

|

|

Visual Basic .NET

| Structure name{{indent|2}}Dim name As type{{indent|2}}...
End Structure

| (val{{sub|1}}, val{{sub|2}}, ... )

|

|

Python

| {{n/a}}{{ref|just classes|[a]}}

| «(»val{{sub|1}}, val{{sub|2}}, val{{sub|3}}, ... «)»

|

| {{n/a}}

S-Lang

| struct {name [=value], ...}

|

|

|

Fortran

| TYPE name{{indent|2}}type :: name{{indent|2}}...
END TYPE

|

|

|

PHP

| {{n/a}}{{ref|just classes|[a]}}

|

|

|

Perl

| {{n/a}}{{ref|Perl's records|[d]}}

|

|

| rowspan=3 {{n/a}}

Raku

| {{n/a}}{{ref|just classes|[a]}}

|

|

Ruby

| {{code|lang=ruby|1=OpenStruct.new({:name => value})}}

|

|

Scala

| case class namevar» name: type, ...)

| (val{{sub|1}}, val{{sub|2}}, val{{sub|3}}, ... )

| abstract class name{{indent|2}}case class Fooparameters») extends name{{indent|2}}case class Barparameters») 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

| rowspan=2| type name = {«mutable» name : type;...}

| rowspan=2| «(»val{{sub|1}}, val{{sub|2}}, val{{sub|3}}, ... «)»

| rowspan=2| type name = Foo «of type» {{pipe}} Bar «of type» {{pipe}} ...

| rowspan=4 {{n/a}}

F#
Standard ML

| type name = {name : type,...}

| rowspan=2| (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}}...

| {{n/a}}

| {{n/a}}

| name REDEFINES variable type.

  • {{note|just classes|a}} Only classes are supported.
  • {{note|C++'s struct|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.
  • {{note|pair only|c}} pair only
  • {{note|Perl's records|d}} Although Perl doesn't have records, because Perl's type system allows different data types to be in an array, "hashes" (associative arrays) that don't have a variable index would effectively be the same as records.
  • {{note|enum|e}} Enumerations in this language are algebraic types with only nullary constructors

Variable and constant declarations

class="wikitable sortable"

!

! style="width: 30%" | variable

! constant

! type synonym

Ada

| identifier : type« := initial_value»{{ref|Ada declaration|[e]}}

| identifier : constant type := final_value

| subtype identifier is type

ALGOL 68

| modename name« := initial_value»;

| modename name = value;

| mode synonym = modename;

C (C99)

| rowspan=3| type name« = initial_value»;

| rowspan=2| enum{ name = value };

| rowspan=3| 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;

| rowspan=2 {{n/a}}

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{{ref|Rust declaration|[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)

| {{n/a}}

Pascal{{ref|Pascal's declarations|[a]}}

| 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 name1 «As type» = value, name2 «As type» = value, ...

|

Visual Basic .NET{{Cite web|url=https://ljw1004.github.io/vbspec/vb.html|title=Grammar vb|date=2016-06-17|website=Visual Basic Language Specification|archive-url=https://web.archive.org/web/20190829225051/https://ljw1004.github.io/vbspec/vb.html|archive-date=2019-08-29|url-status=live|access-date=2019-08-29}}

| The variable declaration syntax of VB.NET is unusually difficult to precisely describe.

Given that there exist the identifier suffixes ("modifiers"):

  • 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 form

{{In5}}Dim 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:

name = initial_value

| Imports synonym = type

Xojo

| Dim name «As type»« = initial_value»

| {{n/a}}

Python

| name«: type» = initial_value

| rowspan=2 {{n/a}}

| synonym = type{{ref|variable types|[b]}}

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);
const name = value (5.3+)

| rowspan=2 {{n/a}}

Perl

| «my» $name« = initial_value»;{{ref|Perl's my keyword|[c]}}

| use constant name => value;

Raku

| «my «type»» $name« = initial_value»;{{ref|Perl's my keyword|[c]}}

| «my «type»» constant name = value;

| ::synonym ::= type

Ruby

| name = initial_value

| Name = value

| synonym = type{{ref|variable types|[b]}}

Scala

| var name«: type» = initial_value

| val name«: type» = value

| type synonym = type

Windows PowerShell

| «[type]» $name = initial_value

| {{n/a}}

| {{n/a}}

Bash shell

| name=initial_value

| {{n/a}}

| {{n/a}}

OCaml

| let name« : type ref» = ref value{{ref|ML ref|[d]}}

| 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{{ref|ML ref|[d]}}

| val name «: type» = value

Haskell

|

| «name::typename = 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

| {{n/a}}

| {{n/a}}

  • {{note|Pascal's declarations|a}} Pascal has declaration blocks. See functions.
  • {{note|variable types|b}}Types are just regular objects, so you can just assign them.
  • {{note|Perl's my keyword|c}} In Perl, the "my" keyword scopes the variable into the block.
  • {{note|ML ref|d}} Technically, this does not declare name to be a mutable variable—in ML, all names can only be bound once; rather, it declares name to point to a "reference" data structure, which is a simple mutable cell. The data structure can then be read and written to using the ! and := operators, respectively.
  • {{note|Ada declaration|e}} If no initial value is given, an invalid value is automatically assigned (which will trigger a run-time exception if it used before a valid value has been assigned). While this behaviour can be suppressed it is recommended in the interest of predictability. If no invalid value can be found for a type (for example in case of an unconstraint integer type), a valid, yet predictable value is chosen instead.
  • {{note|Rust declaration|f}} In Rust, if no initial value is given to a let or let mut variable and it is never assigned to later, there is an [https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#unused-variables "unused variable" warning]. If no value is provided for a const or static or static mut variable, there is an error. There is a [https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html?#non-upper-case-globals "non-upper-case globals"] error for non-uppercase const variables. After it is defined, a static mut variable can only be assigned to in an unsafe block or function.

[[Control flow]]

= [[Conditional (programming)|Conditional]] statements =

class="wikitable sortable"
valign="top"

!

! if

! else if

! select case

! conditional expression

valign="top"

| Ada

| if condition then{{indent|2}}statements
«else{{indent|2}}statements»
end if

| if condition{{sub|1}} then{{indent|2}}statements
elsif condition{{sub|2}} then{{indent|2}}statements
...
«else{{indent|2}}statements»
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»
end case

| (if condition{{sub|1}} then{{indent|2}}expression{{sub|1}}
«elsif condition{{sub|2}} then{{indent|2}}expression{{sub|2}}»
...
else{{indent|2}}expression{{sub|n}}
)

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}}»
)

valign="top"

| Seed7

| if condition then{{indent|2}}statements
«else{{indent|2}}statements»
end if

| if condition{{sub|1}} then{{indent|2}}statements
elsif condition{{sub|2}} then{{indent|2}}statements
...
«else{{indent|2}}statements»
end if

| case expression of{{indent|2}}when set1 : statements{{indent|2}}...{{indent|2}}«otherwise: statements»
end case

|

valign="top"

| Modula-2

| if condition then{{indent|2}}statements
«else{{indent|2}}statements»
end

| if condition{{sub|1}} then{{indent|2}}statements
elsif condition{{sub|2}} then{{indent|2}}statements
...
«else{{indent|2}}statements»
end

| rowspan=1 | case expression of{{indent|2}}caseLabelList : statements {{pipe}}{{indent|2}}...{{indent|2}}«else statements»
end

|

valign="top"

| 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 )

valign="top"

| ALGOL 68
(brief form)

| ( condition | statements «| statements» )

| ( condition | statements |: condition | statements )

| ( variable | statements,... «| statements» )

|

valign="top"

| APL

| :If condition{{indent|2}}instructions
«:Else{{indent|2}}instructions»
:EndIf

| :If condition{{indent|2}}instructions
:ElseIf condition{{indent|2}}instructions
...
«:Else{{indent|2}}instructions»
:EndIf

| :Select expression{{indent|2}}:Case case1{{indent|4}}instructions{{indent|2}}...{{indent|2}}«:Else{{indent|4}}instructions»
:EndSelect

| {condition:valueIfTruevalueIfFalse}

valign="top"

| C (C99)

| rowspan=7| if (condition) instructions
«else instructions»

instructions can be a single statement or a block in the form of: { statements }

| rowspan=7| if (condition) instructions
else if (condition) instructions
...
«else instructions»

or
if (condition) instructions
else { if (condition) instructions }

| rowspan=7| switch (variable) { {{indent|2}}case case1: instructions «; break;»{{indent|2}}...{{indent|2}}«default: instructions»
}

| rowspan=7| condition ? valueIfTrue : valueIfFalse

valign="top"

| Objective-C

valign="top"

| C++ (STL)

valign="top"

| D

valign="top"

| Java

valign="top"

| JavaScript

valign="top"

| PHP

valign="top"

| C#

| if (condition) instructions
«else instructions»

instructions can be a single statement or a block in the form of: { statements }

| if (condition) instructions
else if (condition) instructions
...
«else instructions»

| switch (variable)
{ {{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»
}

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

valign="top"

| Windows PowerShell

| if (condition) instruction
«else instructions»

| if (condition) { instructions }
elseif (condition) { instructions }
...
«else { instructions

| switch (variable) { case1{instructions «break;» } ... «default { instructions }»}

|

valign="top"

| Go

| if condition {instructions}
«else {instructions

| if condition {instructions}
else if condition {instructions}
...
«else {instructions

or
switch { {{indent|2}}case condition: instructions{{indent|2}}...{{indent|2}}«default: instructions»
}

| switch variable { {{indent|2}}case case1: instructions{{indent|2}}...{{indent|2}}«default: instructions»
}

|

valign="top"

| Swift

| if condition {instructions}
«else {instructions

| if condition {instructions}
else if condition {instructions}
...
«else {instructions

| switch variable { {{indent|2}}case case1: instructions{{indent|2}}...{{indent|2}}«default: instructions»
}

|

valign="top"

| Perl

| if (condition) {instructions}
«else {instructions

or
unless (notcondition) {instructions}
«else {instructions

| if (condition) {instructions}
elsif (condition) {instructions}
...
«else {instructions

or
unless (notcondition) {instructions}
elsif (condition) {instructions}
...
«else {instructions

| use feature "switch";
...
given (variable) { {{indent|2}}when (case1) { instructions }{{indent|2}}...{{indent|2}}«default { instructions
}

| condition ? valueIfTrue : valueIfFalse

valign="top"

| Raku

| if condition {instructions}
«else {instructions

or
unless notcondition {instructions}

| if condition {instructions}
elsif condition {instructions}
...
«else {instructions}

| given variable { {{indent|2}}when case1 { instructions }{{indent|2}}...{{indent|2}}«default { instructions
}

| condition ?? valueIfTrue !! valueIfFalse

valign="top"

| Ruby

| if condition{{indent|2}}instructions
«else{{indent|2}}instructions»

| if condition{{indent|2}}instructions
elsif condition{{indent|2}}instructions
...
«else{{indent|2}}instructions»
end

| case variable{{indent|2}}when case1{{indent|4}}instructions{{indent|2}}...{{indent|2}}«else{{indent|4}}instructions»
end

| condition ? valueIfTrue : valueIfFalse

valign="top"

| Scala

| if (condition) {instructions}
«else {instructions

| if (condition) {instructions}
else if (condition) {instructions}
...
«else {instructions

| expression match { {{indent|2}}case pattern1 => expression{{indent|2}}case pattern2 => expression{{indent|2}}...{{indent|2}}«case _ => expression»
}
{{ref|pattern matching|[b]}}

| if (condition) valueIfTrue else valueIfFalse

valign="top"

| Smalltalk

| condition ifTrue:{{indent|2}}trueBlock
«ifFalse:{{indent|2}}falseBlock»
end

|

|

| condition ifTrue: trueBlock ifFalse: falseBlock

valign="top"

| 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) ...))

valign="top"

| 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)

valign="top"

| 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)

valign="top"

| Pascal

| if condition then begin{{indent|2}}instructions
end
«else begin{{indent|2}}instructions
end»'
{{ref|pascal semicolon|[c]}}

| if condition then begin{{indent|2}}instructions
end
else if condition then begin{{indent|2}}instructions
end
...
«else begin{{indent|2}}instructions
end»
{{ref|pascal semicolon|[c]}}

| case variable of{{indent|2}}case1: instructions{{indent|2}}...{{indent|2}}«else: instructions»
end
{{ref|pascal semicolon|[c]}}

valign="top"

| Visual Basic

| rowspan=3| If condition Then{{indent|2}}instructions
«Else{{indent|2}}instructions»
End If

Single-line, when instructions are instruction{{sub|1}} : instruction{{sub|2}} : ...:
If condition Then instructions «Else instructions»

| rowspan=3| If condition Then{{indent|2}}instructions
ElseIf condition Then{{indent|2}}instructions
...
«Else{{indent|2}}instructions»
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.

| rowspan=3| Select« Case» variable{{indent|2}}Case case_pattern{{sub|1}}{{indent|4}}instructions{{indent|2}}...{{indent|2}}«Case Else{{indent|4}}instructions»
End Select

| IIf(condition, valueIfTrue, valueIfFalse)

valign="top"

| Visual Basic .NET

| rowspan=2| If(condition, valueIfTrue, valueIfFalse)

valign="top"

| Xojo

valign="top"

| Python{{ref|python indent|[a]}}

| if condition :
{{keypress|Tab}}instructions
«else:
{{keypress|Tab}}instructions»

| if condition :
{{keypress|Tab}}instructions
elif condition :
{{keypress|Tab}}instructions
...
«else:
{{keypress|Tab}}instructions»

| Python 3.10+:
match variable:
{{keypress|Tab}}case case1:
{{keypress|Tab}}{{keypress|Tab}}instructions
{{keypress|Tab}}case case2:
{{keypress|Tab}}{{keypress|Tab}}instructions

| Python 2.5+:
valueIfTrue if condition else valueIfFalse

valign="top"

| S-Lang

| if (condition) { instructions } «else { instructions

| if (condition) { instructions } else if (condition) { instructions } ... «else { instructions

| switch (variable) { case case1: instructions } { case case2: instructions } ...

|

valign="top"

| Fortran

| IF (condition) THEN{{indent|2}}instructions
ELSE{{indent|2}}instructions
ENDIF

| IF (condition) THEN{{indent|2}}instructions
ELSEIF (condition) THEN{{indent|2}}instructions
...
ELSE{{indent|2}}instructions
ENDIF

| SELECT CASE(variable){{indent|2}}CASE (case1){{indent|4}}instructions{{indent|2}}...{{indent|2}}CASE DEFAULT{{indent|4}}instructions
END SELECT

|

valign="top"

| 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
ENDCASE

| condition IF valueIfTrue ELSE valueIfFalse THEN

valign="top"

| 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»

| rowspan=2| match value with {{indent|2}}pattern1 -> expression{{indent|2}}{{pipe}} pattern2 -> expression{{indent|2}}...{{indent|2}}«{{pipe}} _ -> expression»{{ref|pattern matching|[b]}}

| rowspan=4| if condition then valueIfTrue else valueIfFalse

valign="top"

| F#

| Lightweight syntax mode:

Either on a single line or with indentation as shown below:

if condition then
{{keypress|Tab}}instructions
«else
{{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
{{keypress|Tab}}instructions
elif condition then
{{keypress|Tab}}instructions
...
«else
{{keypress|Tab}}instructions»

Verbose syntax mode:
Same as Standard ML.

valign="top"

| Standard ML

| if condition then «(»instructions «
else «(» instructions «

| if condition then «(»instructions «
else if condition then «(» instructions «
...
else «(» instructions «

| case value of{{indent|2}}pattern1 => expression{{indent|2}}{{pipe}} pattern2 => expression{{indent|2}}...{{indent|2}}«{{pipe}} _ => expression»{{ref|pattern matching|[b]}}

valign="top"

| 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»
}
{{ref|pattern matching|[b]}}

valign="top"

| Bash shell

| if condition-command; then{{indent|2}}expression
«else{{indent|2}}expression»
fi

| if condition-command; then{{indent|2}}expression
elif condition-command; then{{indent|2}}expression
«else{{indent|2}}expression»
fi

| case "$variable" in{{indent|2}}"$condition1" ){{indent|4}}command...{{indent|2}}"$condition2" ){{indent|4}}command...
esac

|

valign="top"

| CoffeeScript

| if condition then expression «else expression»
or
if condition{{indent|2}}expression
«else{{indent|2}}expression»

or
expression if condition
or
unless condition{{indent|2}}expression
«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
else if condition{{indent|2}}expression
«else{{indent|2}}expression»

or
unless condition{{indent|2}}expression
else unless condition{{indent|2}}expression
«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.

valign="top"

| COBOL

| IF condition «THEN»{{indent|2}}expression
«ELSE{{indent|2}}expression».
{{ref|COBOL END-IF|[d]}}

|

| 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»
END-EVALUATE

|

valign="top"

| Rust

| if condition { {{indent|2}}expression
}« else { {{indent|2}}expression

| if condition { {{indent|2}}expression
} else if condition {{{indent|2}}expression
}« else {{{indent|2}}expression
}»

| match variable { {{indent|2}}pattern1 => expression,{{indent|2}}pattern2 => expression,{{indent|2}}pattern3 => expression,{{indent|2}}«_ => expression»
}
{{ref|pattern matching|[b]}}{{ref|Rust match expression|[e]}}

| All conditions are expressions

valign="top"

!

! if

! else if

! select case

! conditional expression

  • {{note|python indent|a}} A single instruction can be written on the same line following the colon. Multiple instructions are grouped together in a block which starts on a newline (The indentation is required). The conditional expression syntax does not follow this rule.
  • {{note|pattern matching|b}} This is pattern matching and is similar to select case but not the same. It is usually used to deconstruct algebraic data types.
  • {{note|pascal semicolon|c}} In languages of the Pascal family, the semicolon is not part of the statement. It is a separator between statements, not a terminator.
  • {{note|COBOL END-IF|d}} END-IF may be used instead of the period at the end.
  • {{note|Rust match expression|e}} In Rust, the comma (,) at the end of a match arm can be omitted after the last match arm, or after any match arm in which the expression is a block (ends in possibly empty matching brackets {}).

= [[Control flow#Loops|Loop statements]] =

class="wikitable sortable"
! while loop

! do while loop

! (count-controlled) for loop

! foreach

Ada

| while condition loop{{indent|2}}statements
end loop

| loop{{indent|2}}statements{{indent|2}}exit when not condition
end loop

| for index in «reverse» [first .. last {{pipe}} discrete_type] loop{{indent|2}}statements
end loop

| for item of «reverse» iterator loop{{indent|2}}statements
end loop

or
(for [all {{pipe}} some] [in {{pipe}} of] [first .. last {{pipe}} discrete_type {{pipe}} iterator] => predicate){{ref|Ada_quantifiers|[b]}}

rowspan=2| ALGOL 68colspan=3 align=center| «for index» «from first» «by increment» «to last» «while condition» do statements od

| rowspan=2| 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
:EndWhile

| :Repeat{{indent|2}}statements
:Until condition

| :For var«s» :In list{{indent|2}}statements
:EndFor

| :For var«s» :InEach list{{indent|2}}statements
:EndFor

C (C99)

| rowspan=9| instructions can be a single statement or a block in the form of: { statements }
while (condition) instructions

| rowspan=9| do instructions while (condition);

| rowspan=5| for («type» i = first; i <= last; i++) instructions

| {{n/a}}

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:{{cite web|url=https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...of|title=for...of|work=mozilla.org|access-date=30 January 2017}}

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
end

or
until notcondition{{indent|2}}instructions
end

| begin{{indent|2}}instructions
end while condition

or
begin{{indent|2}}instructions
end until notcondition

| for i in first..last{{indent|2}}instructions
end

or
for i in first...last+1{{indent|2}}instructions
end

or
first.{{not a typo|upto}}(last) { {{pipe}}i{{pipe}} instructions }

| for item in set{{indent|2}}instructions
end

or
set.each { {{pipe}}item{{pipe}} instructions }

Bash shell

| while condition ;do{{indent|2}}instructions
done

or
until notcondition ;do{{indent|2}}instructions
done

| {{n/a}}

| for ((i = first; i <= last; ++i)) ; do{{indent|2}}instructions
done

| for item in set ;do{{indent|2}}instructions
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))
{{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
end

| repeat{{indent|2}}instructions
until notcondition;

| for i := first «step 1» to last do begin{{indent|2}}instructions
end;
{{ref|step|[a]}}

| for item in set do instructions

Visual Basic

| rowspan=2| Do While condition{{indent|2}}instructions
Loop

or
Do Until notcondition{{indent|2}}instructions
Loop

or
While condition{{indent|2}}instructions
Wend
(Visual Basic .NET uses End While instead)

| rowspan=2| Do{{indent|2}}instructions
Loop While condition

or
Do{{indent|2}}instructions
Loop Until notcondition

| i must be declared beforehand.

For i = first To last «Step 1»{{indent|2}}instructions
Next i

| For Each item In set{{indent|2}}instructions
Next item

Visual Basic .NET

| rowspan=2| For i« As type» = first To last« Step 1»{{indent|2}}instructions
Next« i»
{{ref|step|[a]}}

| rowspan=2| For Each item« As type» In set{{indent|2}}instructions
Next« item»

Xojo

| While condition{{indent|2}}instructions
Wend

| Do Until notcondition{{indent|2}}instructions
Loop

or
Do{{indent|2}}instructions
Loop Until notcondition

Python

| while condition :
{{keypress|Tab}}instructions
«else:
{{keypress|Tab}}instructions»

| {{n/a}}

| Python 3.x:
for i in range(first, last+1):
{{keypress|Tab}}instructions
«else:
{{keypress|Tab}}instructions»

Python 2.x:
for i in xrange(first, last+1):
{{keypress|Tab}}instructions
«else:
{{keypress|Tab}}instructions»

| for item in set:
{{keypress|Tab}}instructions
«else:
{{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
ENDDO

| DO{{indent|2}}instructions{{indent|2}}IF (condition) EXIT
ENDDO

| DO I = first,last{{indent|2}}instructions
ENDDO

| {{n/a}}

Forth

| BEGIN «instructions» condition WHILE instructions REPEAT

| BEGIN instructions condition UNTIL

| limit start DO instructions LOOP

| {{n/a}}

OCaml

| while condition do instructions done

| {{n/a}}

| 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
{{keypress|Tab}}instructions

| {{n/a}}

| for i = first to last do
{{keypress|Tab}}instructions

| foritem in set do
{{keypress|Tab}}instructions

or
Seq.iter (fun item -> instructions) set

Standard ML

| while condition do ( instructions )

| colspan=2 {{n/a}}

| Array.app (fn item => instructions) array
or
app (fn item => instructions) list

Haskell (GHC)

| colspan=2 {{n/a}}

| Control.Monad.forM_ [first..last] (\i -> do instructions)

| Control.Monad.forM_list (\item -> do instructions)

Eiffel

| colspan=4| from{{indent|2}}setup
until{{indent|2}}condition
loop{{indent|2}}instructions
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

| {{n/a}}

| 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{{ref|COBOL THRU|[c]}}
or
PERFORM ««WITH» TEST BEFORE» UNTIL condition{{indent|2}}expression
END-PERFORM

| PERFORM procedure-1 «THROUGH procedure-2» «WITH» TEST AFTER UNTIL condition{{ref|COBOL THRU|[c]}}
or
PERFORM «WITH» TEST AFTER UNTIL condition{{indent|2}}expression
END-PERFORM

| PERFORM procedure-1 «THROUGH procedure-2» VARYING i FROM first BY increment UNTIL i > last{{ref|COBOL GREATER THAN|[d]}}
or
PERFORM VARYING i FROM first BY increment UNTIL i > last{{indent|2}}expression
END-PERFORM
{{ref|COBOL GREATER THAN|[d]}}

| {{n/a}}

Rust

| while condition { {{indent|2}}expression
}

| loop { {{indent|2}}expression{{indent|4}}if condition { {{indent|6}}break;{{indent|2}}}
}

| for i in first..last+1 { {{indent|2}}expression
}

or
for i in first..=last { {{indent|2}}expression
}

| for item in set { {{indent|2}}expression
}
{{ref|Rust FOREACH|[e]}}
or
set.into_iter().for_each({{pipe}}item{{pipe}} expression);{{ref|Rust FOREACH|[e]}}

  • {{note|step|a}} "step n" is used to change the loop interval. If "step" is omitted, then the loop interval is 1.
  • {{note|Ada_quantifiers|b}} This implements the universal quantifier ("for all" or "\forall") as well as the existential quantifier ("there exists" or "\exists").
  • {{note|COBOL THRU|c}} THRU may be used instead of THROUGH.
  • {{note|COBOL GREATER THAN|d}} «IS» GREATER «THAN» may be used instead of >.
  • {{note|Rust FOREACH|e}} Type of set expression must implement trait std::iter::IntoIterator.

= [[Exception handling|Exceptions]] =

{{Further|Exception handling syntax}}

class="wikitable sortable"
! throw

! handler

! assertion

Ada

| raise exception_name «with string_expression»

| begin{{indent|2}}statements
exception{{indent|2}}when exception_list1 => statements;{{indent|2}}when exception_list2 => statements;
...{{indent|2}}«when others => statements;»
end
{{ref|Ada_uncaught_exceptions|[b]}}

| pragma Assert («Check =>» boolean_expression ««Message =>» string_expression»)
[function {{pipe}} procedure {{pipe}} entry] with{{indent|2}}Pre => boolean_expression{{indent|2}}Post => boolean_expression
any_type with Type_Invariant => boolean_expression

APL

| «string_expression» ⎕SIGNAL number_expression

| :Trap number«s»_expression{{indent|2}}statements
«:Case number«s»_expression{{indent|2}}statements»
...
«:Else number«s»_expression{{indent|2}}statements»
:EndTrap

| «string_expression» ⎕SIGNAL 98/⍨~condition

C (C99)

| longjmp(state, exception);

| switch (setjmp(state)) { case 0: instructions break; case exception: instructions ... }

| rowspan=2|assert(condition);

C++

| rowspan=8|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

| {{dunno}}

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

| {{dunno}}

Windows PowerShell

| trap «[exception]» { instructions } ... instructions
or
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

| rowspan=2| die exception;

| eval { instructions }; if ($@) { instructions }

| {{dunno}}

Raku

| try { instructions CATCH { when exception { instructions } ...}}

| {{dunno}}

Ruby

| raise exception

| begin{{indent|2}}instructions
rescue exception{{indent|2}}instructions
...
«else{{indent|2}}instructions»
«ensure{{indent|2}}instructions»
end

|

Smalltalk

| exception raise

| instructionBlock on: exception do: handlerBlock

| assert: conditionBlock

Common Lisp

| (error "exception")
or
(error{{indent|2}}type{{indent|2}}arguments)
or
(error (make-condition{{indent|2}}type{{indent|2}}arguments))

| (handler-case{{indent|2}}(progn instructions){{indent|2}}(exception instructions){{indent|2}}...)
or
(handler-bind{{indent|2}}(condition{{indent|4}}(lambda{{indent|4}}instructions{{indent|4}}«invoke-restart restart args»))
...)
{{ref|a|[a]}}

| (assert condition)
or
(assert condition{{indent|2}}«(place){{indent|2}}«error»»)
or
(check-type var type)

Scheme (R6RS)

| (raise exception)

| (guard (con (condition instructions) ...) instructions)

| {{dunno}}

ISLISP

| (error "error-string" objects)
or
(signal-condition condition continuable)

| (with-handler{{indent|2}}handler form*
)

| {{dunno}}

Pascal

| raise Exception.Create()

| try Except on E: exception do begin instructions end; end;

| {{dunno}}

Visual Basic

| Err.Raise ERRORNUMBER

| With New Try: On Error Resume Next{{indent|2}}OneInstruction
.Catch
: On Error GoTo 0: Select Case .Number{{indent|2}}Case SOME_ERRORNUMBER{{indent|4}}instructions
End Select: End With

'*** 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{{cite web|url=https://sites.google.com/site/truetryforvisualbasic/|title=Try-Catch for VB|work=google.com|access-date=30 January 2017|archive-date=16 April 2016|archive-url=https://web.archive.org/web/20160416093023/https://sites.google.com/site/truetryforvisualbasic/|url-status=dead}}

| Debug.Assert condition

Visual Basic .NET

| Throw exception
or
Error errorcode

| Try{{indent|2}}instructions
Catch« name As exception»« When condition»{{indent|2}}instructions
...
«Finally{{indent|2}}instructions»
End Try

| System.Diagnostics.Debug.Assert(condition)
or
System.Diagnostics.Trace.Assert(condition)

Xojo

| Raise exception

| Try{{indent|2}}instructions
Catch «exception»{{indent|2}}instructions
...
«Finally{{indent|2}}instructions»
End Try

| {{n/a}}

Python

| raise exception

| try:
{{keypress|Tab}}instructions
except «exception»:
{{keypress|Tab}}instructions
...
«else:
{{keypress|Tab}}instructions»
«finally:
{{keypress|Tab}}instructions»

| assert condition

Fortran

| colspan=3 {{n/a}}

Forth

| code THROW

| xt CATCH ( code or 0 )

| {{n/a}}

OCaml

| rowspan=2| raise exception

| try expression with pattern -> expression ...

| rowspan=2| 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 exception
or
throwError expression

| catch tryExpression catchExpression
or
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».

| {{n/a}}

Rust

| colspan=2 {{No}}{{cite book |last1=Klabnik |first1=Steve |last2=Nichols |first2=Carol |title=The Rust Programming Language |url=https://doc.rust-lang.org/book/title-page.html |chapter=Error Handling|chapter-url=https://doc.rust-lang.org/book/ch09-00-error-handling.html}}

| assert!(condition)

  • {{note|common lisp restarts|a}} Common Lisp allows with-simple-restart, restart-case and restart-bind to define restarts for use with invoke-restart. Unhandled conditions may cause the implementation to show a restarts menu to the user before unwinding the stack.
  • {{note|Ada uncaught exceptions|b}} Uncaught exceptions are propagated to the innermost dynamically enclosing execution. Exceptions are not propagated across tasks (unless these tasks are currently synchronised in a rendezvous).

= Other control flow statements =

class="wikitable sortable"
! exit block (break)

! continue

! label

! branch (goto)

! return value from generator

Ada

| exit «loop_name» «when condition»

| {{n/a}}

| label:

| goto label

| {{n/a}}

ALGOL 68

| value exit; ...

| do statements; skip exit; label: statements od

| label: ...

| go to label; ...
goto label; ...
label; ...

| yield(value)

(Callback){{cite web|url=http://rosettacode.org/wiki/Prime_decomposition#ALGOL_68|title=Prime decomposition – Rosetta Code|work=rosettacode.org|access-date=30 January 2017}}

APL

| :Leave

| :Continue

| label:

| label
or
:GoTo label

| {{n/a}}

C (C99)

| rowspan=5| break;

| rowspan=5| continue;

| rowspan=12| label:

| rowspan=5| goto label;

| rowspan=4 {{n/a}}

Objective-C
C++ (STL)
D
C#

| yield return value;

Java

| rowspan=2| break «label»;

| rowspan=2| continue «label»;

| rowspan=2 {{n/a}}

|

JavaScript

| yield value«;»

PHP

| break «levels»;

| continue «levels»;

| rowspan=3| goto label;

| yield «key =>» value;

Perl

| rowspan=2| last «label»;

| rowspan=2| next «label»;

|

Raku

|

Go

| break «label»

| continue «label»

| goto label

|

Swift

| break «label»

| continue «label»

| {{n/a}}

|

Bash shell

| break «levels»

| continue «levels»

| {{n/a}}

| {{n/a}}

| {{n/a}}

Common Lisp

| (return)
or
(return-from block)
or
(loop-finish)

|

| (tagbody tag{{indent|2}}...{{indent|2}}tag
...)

| (go tag)

|

Scheme

|

|

|

|

|

ISLISP

| (return-from block)

|

| (tagbody tag{{indent|2}}...{{indent|2}}tag
...)

| (go tag)

|

Pascal (ISO)

| colspan=2 {{n/a}}

| rowspan=2| label:{{ref|Pascal's declarations|[a]}}

| rowspan=2| goto label;

| rowspan=4 {{n/a}}

Pascal (FPC)

| break;

| continue;

Visual Basic

| rowspan=3| Exit blockAlternatively, for methods,
Return

| {{n/a}}

| rowspan=3| label:

| rowspan=3| GoTo label

Xojo

| rowspan=2 |Continue block

Visual Basic .NET

| Yield value

Python

| break

| continue

| colspan=2 {{n/a}}

| yield value

RPG IV

| LEAVE;

| ITER;

|

|

|

S-Lang

| break;

| continue;

|

|

|

Fortran

| EXIT

| CYCLE

| label{{ref|Fortran label|[b]}}

| GOTO label

| {{n/a}}

Ruby

| break

| next

|

|

|

Windows PowerShell

| break «label»

| continue

|

|

|

OCaml

| rowspan=4 colspan=4 {{n/a}}

|

F#

|

Standard ML

|

Haskell (GHC)

|

COBOL

| EXIT PERFORM or EXIT PARAGRAPH or EXIT SECTION or EXIT.

| EXIT PERFORM CYCLE

| label «SECTION».

| GO TO label

| {{n/a}}

  • {{note|Pascal's declarations|a}} Pascal has declaration blocks. See functions.
  • {{note|Fortran label|b}} label must be a number between 1 and 99999.

[[Subroutine|Function]]s

See reflective programming for calling and declaring functions by strings.

{{sort-under}}

class="wikitable sortable sort-under"
valign="top"

!

! calling a function

! basic/void function

! value-returning function

! required main function

valign="top"

| Ada

| foo «(parameters)»

| procedure foo «(parameters)» is begin statements end foo

| function foo «(parameters)» return type is begin statements end foo

| {{n/a}}

valign="top"

| ALGOL 68

| foo «(parameters)»;

| proc foo = «(parameters)» void: ( instructions );

| proc foo = «(parameters)» rettype: ( instructions ...; retvalue );

| {{n/a}}

valign="top"

| APL

| «parameters» foo parameters

| foo←{ statements }

| foo←{ statements }

| {{n/a}}

valign="top"

| C (C99)

| rowspan=9| foo(«parameters»)

| rowspan="5" | void foo(«parameters») { instructions }

| rowspan="5" | type foo(«parameters») { instructions ... return value; }

| rowspan=3| «global declarations»
int main(«int argc, char *argv[]») { {{indent|2}}instructions
}

valign="top"

| Objective-C

valign="top"

| C++ (STL)

valign="top"

| Java

| public static void main(String[] args) { instructions }
or
public static void main(String... args) { instructions }

valign="top"

| D

| int main(«char[][] args») { instructions}
or
int main(«string[] args») { instructions}
or
void main(«char[][] args») { instructions}
or
void main(«string[] args») { instructions}

valign="top"

| C#

| Same as above; alternatively, if only one statement:

void foo(«parameters») => statement;

| Same as above; alternatively, if simple enough to be an expression:

void foo(«parameters») => expression;

| static void Main(«string[] args») method_body
May instead return int.
(starting with C# 7.1:) May return Task or Task, and if so, may be async.

valign="top"

| 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; }

| {{n/a}}

valign="top"

| Go

| func foo(«parameters») { instructions }

| func foo(«parameters») type { instructions ... return value }

| func main() { instructions }

valign="top"

| Swift

| func foo(«parameters») { instructions }

| func foo(«parameters») -> type { instructions ... return value }

| {{n/a}}

valign="top"

| Common Lisp

| rowspan=3| (foo «parameters»)

| (defun foo («parameters»){{indent|2}}instructions)
or
(setf (symbol-function 'symbol){{indent|2}}function)

| (defun foo («parameters»){{indent|2}}...{{indent|2}}value)

| rowspan=3 {{n/a}}

valign="top"

| 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))

valign="top"

| ISLISP

| (defun foo («parameters»){{indent|2}}instructions)

| (defun foo («parameters»){{indent|2}}...{{indent|2}}value)

valign="top"

| Pascal

| foo«(parameters

| procedure foo«(parameters)»; «forward;»{{ref|forward declaration|[a]}}
«label{{indent|2}}label declarations»
«const{{indent|2}}constant declarations»
«type{{indent|2}}type declarations»
«var{{indent|2}}variable declarations»
«local function declarations»

begin{{indent|2}}instructions
end;

| function foo«(parameters)»: type; «forward;»{{ref|forward declaration|[a]}}
«label{{indent|2}}label declarations»
«const{{indent|2}}constant declarations»
«type{{indent|2}}type declarations»
«var{{indent|2}}variable declarations»
«local function declarations»

begin{{indent|2}}instructions;{{indent|2}}foo := value
end;

| program name;
«label{{indent|2}}label declarations»
«const{{indent|2}}constant declarations»
«type{{indent|2}}type declarations»
«var{{indent|2}}variable declarations»
«function declarations»

begin{{indent|2}}instructions
end.

valign="top"

| Visual Basic

| rowspan=3| Foo(«parameters»)

| rowspan=3| Sub Foo«(parameters)»{{indent|2}}instructions
End Sub

| Function Foo«(parameters)»« As type»{{indent|2}}instructions{{indent|2}}Foo = value
End Function

| Sub Main(){{indent|2}}instructions
End Sub

valign="top"

| Visual Basic .NET

| rowspan=2| Same as above; alternatively:

Function Foo«(parameters)»« As type»{{indent|2}}instructions{{indent|2}}Return value
End Function

The As clause is not required if Option Strict is off. A type character may be used instead of the As clause.
If control exits the function without a return value having been explicitly specified, the function returns the default value for the return type.

| rowspan=2| Sub Main(««ByVal »args() As String»){{indent|2}}instructions
End Sub
or
Function Main(««ByVal »args() As String») As Integer{{indent|2}}instructions
End Function

valign="top"

| Xojo

valign="top"

| Python

| foo(«parameters»)

| def foo(«parameters»):
{{keypress|Tab}}instructions

| def foo(«parameters»):
{{keypress|Tab}}instructions
{{keypress|Tab}}return value

| {{n/a}}

valign="top"

| S-Lang

| foo(«parameters» «;qualifiers»)

| define foo («parameters») { instructions }

| define foo («parameters») { instructions ... return value; }

| public define slsh_main () { instructions }

valign="top"

| Fortran

| foo («arguments»)
CALL sub_foo («arguments»)
{{ref|Fortran arguments|[c]}}

| SUBROUTINE sub_foo («arguments»){{indent|2}}instructions
END SUBROUTINE
{{ref|Fortran arguments|[c]}}

| type FUNCTION foo («arguments»){{indent|2}}instructions{{indent|2}}...{{indent|2}}foo = value
END FUNCTION
{{ref|Fortran arguments|[c]}}

| PROGRAM main{{indent|2}}instructions
END PROGRAM

valign="top"

| Forth

| «parameters» FOO

| : FOO « stack effect comment: ( before -- ) »{{indent|2}}instructions
;

| : FOO « stack effect comment: ( before -- after ) »{{indent|2}}instructions
;

| {{n/a}}

valign="top"

| PHP

| foo(«parameters»)

| function foo(«parameters») { instructions }

| function foo(«parameters») { instructions ... return value; }

| rowspan=4 {{n/a}}

valign="top"

| Perl

| foo(«parameters»)
or
&foo«(parameters

| sub foo { «my (parameters) = @_;» instructions }

| sub foo { «my (parameters) = @_;» instructions... «return» value; }

valign="top"

| Raku

| foo(«parameters»)
or
&foo«(parameters

| «multi »sub foo(parameters) { instructions }

| «our «type» »«multi »sub foo(parameters) { instructions ... «return» value; }

valign="top"

| Ruby

| foo«(parameters

| def foo«(parameters)»{{indent|2}}instructions
end

| def foo«(parameters)»{{indent|2}}instructions{{indent|2}}«return» value
end

valign="top"

| Rust

| foo(«parameters»)

| fn foo(«parameters») { instructions }

| fn foo(«parameters») -> type { instructions }

| fn main() { instructions }

valign="top"

| Scala

| foo«(parameters

| def foo«(parameters)»«: Unit =» { instructions }

| def foo«(parameters)»«: type» = { instructions ... «return» value }

| def main(args: Array[String]) { instructions }

valign="top"

| Windows PowerShell

| foo «parameters»

| function foo { instructions };
or
function foo { «param(parametersinstructions }

| function foo «(parameters)» { instructions ... return value };
or
function foo { «param(parametersinstructions ... return value }

| rowspan=4 {{n/a}}

valign="top"

| rowspan=2| Bash shell

| rowspan=2| foo «parameters»

| function foo {{{indent|2}}instructions
}

or
foo () {{{indent|2}}instructions
}

| function foo {{{indent|2}}instructions{{indent|2}}return «exit_code»
}

or
foo () { {{indent|2}}instructions{{indent|2}}return «exit_code»
}

valign="top"

| colspan=2|

  • parameters
  • $n ($1, $2, $3, ...)
  • $@ (all parameters)
  • $# (the number of parameters)
  • $0 (this function name)
valign="top"

| OCaml

| rowspan=4| foo parameters

| rowspan=2| let «rec» foo parameters = instructions

| rowspan=2| let «rec» foo parameters = instructions... return_value

valign="top"

| F#

| [] let main args = instructions

valign="top"

| Standard ML

| fun foo parameters = ( instructions )

| fun foo parameters = ( instructions... return_value )

|

valign="top"

| Haskell

| foo parameters = do
{{keypress|Tab}}instructions

| foo parameters = return_value
or
foo parameters = do
{{keypress|Tab}}instructions
{{keypress|Tab}}return value

| «main :: IO ()»
main = do instructions

valign="top"

| Eiffel

| foo («parameters»)

| foo («parameters»){{indent|2}}require{{indent|4}}preconditions{{indent|2}}do{{indent|4}}instructions{{indent|2}}ensure{{indent|4}}postconditions{{indent|2}}end

| foo («parameters»): type{{indent|2}}require{{indent|4}}preconditions{{indent|2}}do{{indent|4}}instructions{{indent|4}}Result := value{{indent|2}}ensure{{indent|4}}postconditions{{indent|2}}end

| {{ref|root class and feature|[b]}}

valign="top"

| rowspan=2| CoffeeScript

| foo()

| foo = ->

| foo = -> value

| rowspan=2 {{n/a}}

valign="top"

| foo parameters

| foo = () ->

| foo = ( parameters ) -> value

valign="top"

| rowspan=2 | COBOL

| CALL "foo" «USING parameters»{{indent|2}}«exception-handling»
«
END-CALL»
{{ref|COBOL calling programs|[d]}}

| «IDENTIFICATION DIVISION.»
PROGRAM-ID. foo.
«other divisions...»
PROCEDURE DIVISION «USING parameters».{{indent|2}}instructions.

| rowspan=2 | «IDENTIFICATION DIVISION.»
PROGRAM-ID/FUNCTION-ID. foo.
«other divisions...»
DATA DIVISION.
«other sections...»
LINKAGE SECTION.
«parameter definitions...»
variable-to-return definition
«other sections...»
PROCEDURE DIVISION «USING parameters» RETURNING variable-to-return.{{indent|2}}instructions.

| rowspan=2 {{n/a}}

«FUNCTION» foo«(«parameters»)»

| {{n/a}}

  • {{note|forward declaration|a}} Pascal requires "forward;" for forward declarations.
  • {{note|root class and feature|b}} Eiffel allows the specification of an application's root class and feature.
  • {{note|Fortran arguments|c}} In Fortran, function/subroutine parameters are called arguments (since PARAMETER is a language keyword); the CALL keyword is required for subroutines.
  • {{note|COBOL calling programs|d}} Instead of using "foo", a string variable may be used instead containing the same value.

[[Type conversion]]s{{anchor|Data conversions}}

Where string is a signed decimal number:

class="wikitable sortable sort-under"
! string to integer

! string to long integer

! string to floating point

! integer to string

! floating point to string

Ada

| Integer'Value (string_expression)

| Long_Integer'Value (string_expression)

| Float'Value (string_expression)

| Integer'Image (integer_expression)

| Float'Image (float_expression)

rowspan=3 | ALGOL 68 with general, and then specific formats

| colspan=5 align=center| 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:@"%i", integer];

| string = [NSString stringWithFormat:@"%f", float];

C++ (STL)

| colspan=3| «std::»istringstream(string) >> number;

| colspan=2| «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);

| colspan=2| string = «std::»to_string(number);

C#

| integer = int.Parse(string);

| long = long.Parse(string);

| float = float.Parse(string);double = double.Parse(string);

| colspan=2| string = number.ToString();

D

| integer = std.conv.to!int(string)

| long = std.conv.to!long(string)

| float = std.conv.to!float(string)
double = std.conv.to!double(string)

| colspan=2| string = std.conv.to!string(number)

Java

| integer = Integer.parseInt(string);

| long = Long.parseLong(string);

| float = Float.parseFloat(string);
double = Double.parseDouble(string);

| string = Integer.toString(integer);
string = String.valueOf(integer);

| string = Float.toString(float);
string = Double.toString(double);

JavaScript{{ref|JavaScript's technicalities|[a]}}

| colspan=2| integer = parseInt(string);

| float = parseFloat(string);
float = new Number (string);
float = Number (string);
float = +string;

| colspan=2| string = number.toString ();
string = String (number);
string = number+"";
string = `${number}`

Go

| integer, error = strconv.Atoi(string)
integer, error = strconv.ParseInt(string, 10, 0)

| long, error = strconv.ParseInt(string, 10, 64)

| float, error = strconv.ParseFloat(string, 64)

| string = strconv.Itoa(integer)
string = strconv.FormatInt(integer, 10)
string = fmt.Sprint(integer)

| string = strconv.FormatFloat(float)
string = fmt.Sprint(float)

Rust{{ref|Rust type conversion|[d]}}

| string.parse::()
i32::from_str(string)

| string.parse::()
i64::from_str(string)

| string.parse::()
f64::from_str(string)

| integer.to_string()

| float.to_string()

Common Lisp

| colspan=2| (setf integer (parse-integer string))

| (setf float (read-from-string string))

| colspan=2| (setf string (princ-to-string number))

Scheme

| colspan=3| (define number (string->number string))

| colspan=2| (define string (number->string number))

ISLISP

| colspan=2| (setf integer (convert string <integer>))

| (setf float (convert string <float>))

| colspan=2| (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)
double = CDbl(string)

| string = CStr(number)

|

Visual Basic .NET
(can use both VB syntax above and .NET methods shown right)

| integer = Integer.Parse(string)

| long = Long.Parse(string)

| float = Single.Parse(string)
double = Double.Parse(string)

| colspan="2" | string = number.ToString()

Xojo

| integer = Val(string)

| long = Val(string)

| double = Val(string)
double = CDbl(string)

| colspan=2| string = CStr(number)
or
string = Str(number)

Python

| integer = int(string)

| long = long(string)

| float = float(string)

| colspan=2| string = str(number)

S-Lang

| integer = atoi(string);

| long = atol(string);

| float = atof(string);

| colspan=2| string = string(number);

Fortran

| colspan=3| READ(string,format) number

| colspan=2| WRITE(string,format) number

PHP

| colspan=2| integer = intval(string);
or
integer = (int)string;

| float = floatval(string);
float = (float)string;

| colspan=2| string = "$number";
or
string = strval(number);
or
string = (string)number;

Perl{{ref|Perl's technicalities|[b]}}

| colspan=3| number = 0 + string;

| colspan=2| string = "number";

Raku

| colspan=3| number = +string;

| colspan=2| string = ~number;

Ruby

| colspan=2| integer = string.to_i
or
integer = Integer(string)

| float = string.to_f
float = Float(string)

| colspan=2| string = number.to_s

Scala

| integer = string.toInt

| long = string.toLong

| float = string.toFloatdouble = string.toDouble

| colspan=2| string = number.toString

Smalltalk

| colspan=2| integer := Integer readFrom: string

| float := Float readFrom: string

| colspan=2| string := number asString

Windows PowerShell

| integer = [int]string

| long = [long]string

| float = [float]string

| colspan=2| string = [string]number;
or
string = "number";
or
string = (number).ToString()

OCaml

| let integer = int_of_string string

|

| let float = float_of_string string

| let string = string_of_int integer

| let string = string_of_float float

F#

| let integer = int string

| let integer = int64 string

| let float = float string

| colspan=2| let string = string number

Standard ML

| val integer = Int.fromString string

|

| val float = Real.fromString string

| val string = Int.toString integer

| val string = Real.toString float

Haskell (GHC)

| colspan=3| number = read string

| colspan=2| string = show number

COBOL

| colspan=3 | MOVE «FUNCTION» NUMVAL(string){{ref|COBOL's NUMVAL alternatives|[c]}} TO number

| colspan=2 | MOVE number TO numeric-edited

  • {{note|JavaScript's technicalities|a}} JavaScript only uses floating point numbers so there are some technicalities.
  • {{note|Perl's technicalities|b}} Perl doesn't have separate types. Strings and numbers are interchangeable.
  • {{note|COBOL's NUMVAL alternatives|c}} NUMVAL-C or NUMVAL-F may be used instead of NUMVAL.
  • {{note|Rust type conversion}} [https://doc.rust-lang.org/stable/std/primitive.str.html#method.parse str::parse] is available to convert any type that has an implementation of the [https://doc.rust-lang.org/stable/std/str/trait.FromStr.html std::str::FromStr] trait. Both str::parse and [https://doc.rust-lang.org/stable/std/str/trait.FromStr.html#tymethod.from_str FromStr::from_str] return a Result that contains the specified type if there is no error. The turbofish (::<_>) on str::parse can be omitted if the type can be inferred from context.

[[Standard streams|Standard stream I/O]]

class="wikitable sortable"
rowspan=2|

! read from

! colspan=2| write to

stdin

! stdout

! stderr

Ada

| 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));{{ref|ALGOL Unformatted|[a]}}

APL

| x←

| ⎕←x

| ⍞←x

C (C99)

| scanf(format, &x);
or
fscanf(stdin, format, &x);{{ref|more c input|[b]}}

| printf(format, x);
or
fprintf(stdout, format, x);{{ref|more c output|[c]}}

| fprintf(stderr, format, x);{{ref|more c error output|[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.Error.Write(«format, »x);
or
Console.Error.WriteLine(«format, »x);

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.stdio.writef(stderr, format, x)
or
std.stdio.writefln(stderr, format, x)

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(os{{Not a typo|.}}Stderr, x)
or
fmt.Fprintf(os{{Not a typo|.}}Stderr, 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
{{codett|(format *error-output*|lisp}} format x)

Scheme (R6RS)

| (define x (read-line))

| (display x)
or
{{codett|(format #t|scheme}} format x)

| (display x (current-error-port))
or
{{codett|(format (current-error-port)|scheme}} format x)

ISLISP

| (setf x (read-line))

| {{codett|(format (standard-output)|lisp}} format x)

| {{codett|(format (error-output)|lisp}} format x)

Pascal

| read(x);
or
readln(x);

| write(x);
or
writeln(x);

| write(stderr, x);
or
writeln(stderr, x);

Visual Basic

| Input« promptx

| Print x
or
? x

| {{n/a}}

Visual Basic .NET

| x = Console.Read()
or
x = Console.ReadLine()

| Console.Write(«formatx)
or
Console.WriteLine(«format, »x)

| Console.Error.Write(«format, »x)
or
Console.Error.WriteLine(«format, »x)

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 x
or
sys.stdout.write(x)

| {{codett|print >> sys.stderr,|python2}} x
or
sys.stderr.write(x)

Python 3.x

| x = input(«prompt»)

| print(, end=""»)

| print(, 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{{ref|Fortran standard units|[e]}}

| WRITE(*,format) expressions
or
WRITE(OUTPUT_UNIT,format) expressions{{ref|Fortran standard units|[e]}}

| WRITE(ERROR_UNIT,format) expressions{{ref|Fortran standard units|[e]}}

Forth

| buffer length ACCEPT ( # chars read )
KEY ( char )

| buffer length TYPE
char EMIT

| {{n/a}}

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 = ;

| print x;
or
printf format, x;

| print STDERR x;
or
printf STDERR format, x;

Raku

| $x = $*IN.get;

| x.print
or
x.say

| x.note
or
$*ERR.print(x)
or
$*ERR.say(x)

Ruby

| x = gets

| puts x
or
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 x
or
print_endline str
or
Printf.printf format x ...

| prerr_int x
or
prerr_endline str
or
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

| {{codett|TextIO.output (TextIO.stdErr,|sml}} str)

Haskell (GHC)

| x <- readLn
or
str <- getLine

| print x
or
putStrLn str

| hPrint stderr x
or
hPutStrLn stderr str

COBOL

| ACCEPT x

| DISPLAY x

|

  • {{note|ALGOL Unformatted|a}} ALGOL 68 additionally as the "unformatted" transput routines: read, write, get, and put.
  • {{note|more c input|b}} gets(x) and fgets(x, length, stdin) read unformatted text from stdin. Use of gets is not recommended.
  • {{note|more c input|c}} puts(x) and fputs(x, stdout) write unformatted text to stdout.
  • {{note|more c error output|d}} fputs(x, stderr) writes unformatted text to stderr
  • {{note|Fortran standard units|e}} {{mono|INPUT_UNIT, OUTPUT_UNIT, ERROR_UNIT}} are defined in the {{mono|ISO_FORTRAN_ENV}} module.{{cite web|url=http://fortranwiki.org/fortran/show/iso_fortran_env|title=iso_fortran_env in Fortran Wiki|work=fortranwiki.org|access-date=30 January 2017}}

Reading [[command-line argument]]s

class="wikitable sortable"
! Argument values

! Argument counts

! Program name / Script name

Ada

| Argument (n)

| Argument_Count

| Command_Name

C (C99)

| rowspan=3| argv[n]

| rowspan=3| argc

| rowspan=3| first argument

Objective-C
C++
C#

| rowspan=3| args[n]

| args.Length

| Assembly.GetEntryAssembly().Location;

Java

| rowspan=2| args.length

|

D

| first argument

JavaScript
Windows Script Host implementation

| WScript.Arguments(n)

| WScript.Arguments.length

| WScript.ScriptName
or
WScript.ScriptFullName

Go

| os.Args[n]

| len(os.Args)

| first argument

Rust{{ref|Rust args|[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] or
Process.unsafeArgv[n]

| Process.arguments.count or
Process.argc

| first argument

Common Lisp

| {{dunno}}

| {{dunno}}

| {{dunno}}

Scheme (R6RS)

| (list-ref (command-line) n)

| (length (command-line))

| first argument

ISLISP

| {{n/a}}

| {{n/a}}

| {{n/a}}

Pascal

| ParamStr(n)

| ParamCount

| first argument

Visual Basic

| Command{{ref|unseparated|[b]}}

| {{n/a}}

| 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,argc{{indent|2}}CALL GET_COMMAND_ARGUMENT (i,argv(i))
ENDDO

| 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.Name

OCaml

| Sys.argv.(n)

| Array.length Sys.argv

| first argument

F#

| args.[n]

| args.Length

| Assembly.GetEntryAssembly().Location

Standard ML

| {{code|lang=sml|List.nth (CommandLine.arguments (), n)}}

| {{code|lang=sml|length (CommandLine.arguments ())}}

| {{code|lang=sml|CommandLine.name ()}}

Haskell (GHC)

| {{code|lang=haskell|do { args <- System.getArgs; return length args !! n }}}

| {{code|lang=haskell|do { args <- System.getArgs; return length args }}}

| System.getProgName

COBOL

| colspan=2 | {{ref|COBOL Arguments|[c]}}

| {{n/a}}

  • {{note|Rust args|a}} In Rust, std::env::args and std::env::args_os return iterators, std::env::Args and std::env::ArgsOs respectively. Args converts each argument to a String and it panics if it reaches an argument that cannot be converted to UTF-8. ArgsOs returns a non-lossy representation of the raw strings from the operating system (std::ffi::OsString), which can be invalid UTF-8.
  • {{note|unseparated|b}} In Visual Basic, command-line arguments are not separated. Separating them requires a split function Split(string).
  • {{note|COBOL Arguments|c}} The COBOL standard includes no means to access command-line arguments, but common compiler extensions to access them include defining parameters for the main program or using ACCEPT statements.

Execution of commands

class="wikitable sortable"

!

!Shell command

!Execute program

!Replace current program with new executed program

Ada

| colspan=3 align="center"| Not part of the language standard. Commonly done by compiler provided packages or by interfacing to C or POSIX.{{cite web|url=http://rosettacode.org/wiki/Execute_a_system_command#Ada|title=Execute a system command – Rosetta Code|work=rosettacode.org|access-date=30 January 2017}}

C

| rowspan=3| system("command");

|

| rowspan=2| execl(path, args);
or
execv(path, arglist);

C++

|

Objective-C

| [NSTask launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments];

|

C#

|

| rowspan=2| System.Diagnostics.Process.Start(path, argstring);

|

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.Interaction.Shell(command «, WindowStyle» «, isWaitOnReturn»)

| System.Diagnostics.Process.Start(path, argstring)

|

Xojo

| Shell.Execute(command «, Parameters»)

| FolderItem.Launch(parameters, activate)

| {{n/a}}

D

| std.process.system("command");

|

| std.process.execv(path, arglist);

Java

|

| Runtime.exec(command);
or
new ProcessBuilder(command).start();

|

JavaScript
Windows Script Host implementation

| {{codett|WScript.CreateObject ("WScript.Shell").Run(|js}}command «, WindowStyle» «, isWaitOnReturn»);

| WshShell.Exec(command)

|

Common Lisp

| (uiop:run-program command)

|

|

Scheme

| (system command)

|

|

ISLISP

| {{n/a}}

| {{n/a}}

| {{n/a}}

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 args
or
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.runProcess path args ...

| Posix.Process.executeFile path True args ...

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»){{ref|Fortran 2008|[a]}}

|

|

Windows PowerShell

| [Diagnostics.Process]::Start(command)

| «Invoke-Item »program arg1 arg2 ...

|

Bash shell

| output=`command`
or
output=$(command)

| program arg1 arg2 ...

|

{{note|Fortran 2008|a}} Fortran 2008 or newer.{{cite web|url=https://gcc.gnu.org/onlinedocs/gfortran/EXECUTE_005fCOMMAND_005fLINE.html|title=EXECUTE_COMMAND_LINE – The GNU Fortran Compiler|work=gnu.org|access-date=30 January 2017}}

See also

References