This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Range" computer programming – news ·newspapers ·books ·scholar ·JSTOR(December 2006) (Learn how and when to remove this message) |
Incomputer science, the termrange may refer to one of three things:
The range of a variable is given as the set of possible values that that variable can hold. In the case of an integer, the variable definition is restricted to whole numbers only, and the range will cover every number within its range (including the maximum and minimum). For example, the range of asigned16-bitinteger variable is all the integers from −32,768 to +32,767.
When an array is numerically indexed, its range is the upper and lower bound of the array. Depending on the environment, a warning, afatal exception, or unpredictable behavior will occur if the program attempts to access an array element that is outside the range. In someprogramming languages, such asC, arrays have a fixed lower bound (zero) and will contain data at each position up to the upper bound (so an array with 5 elements will have a range of 0 to 4). In others, such asPHP, an array may have holes where no element is defined, and therefore an array with a range of 0 to 4 will haveup to 5 elements (and a minimum of 2).
Another meaning ofrange in computer science is an alternative toiterator. When used in this sense, range is defined as "a pair of begin/end iterators packed together".[1] It is argued[1] that "Ranges are a superior abstraction" (compared to iterators) for several reasons, including better safety.
In particular, such ranges are supported inC++20,[2]Boost C++ Libraries[3] and theD standard library.[4]

A data type for ranges can be implemented usinggenerics.
Example inC#.
publicrecordRange<T>(TStart,TEnd)whereT:IComparable;
Example inKotlin.
dataclassRange<T:Comparable<T>>(valstart:T,valend:T)
Example inPHP.
readonlyclassRange<T>{publicfunction__construct(publicT$start,publicT$end,){}}
Example inPython.
fromdataclassesimportdataclass@dataclassclassRange[T]:start:Tend:T
Rust has a built-in range struct in the standard library instd::ops::Range.[5]C++ has astd::ranges library as well sinceC++20.[6]
Rust has the.. and..= operators.
letheartwarming="heartwarming!".to_string();letwarm=&heartwarming[5..9];
Zig also has the.. operator.
// To iterate over consecutive integers, use the range syntax.varsum:usize=0;for(0..5)|i|{sum+=i;}
string[]items=["one","two","three","four"];string[]firstThreeItems=items[0..2];
[1..4]// Outputs: [1; 2; 3; 4]
for(iin1..5)print(i)
for(1..5){print}
Python andPHP does not have any range operator but they do have arange function.[11][12]
Thiscomputer-programming-related article is astub. You can help Wikipedia byexpanding it. |