Neatest Way to Loop Over a Range of Integers

Neatest way to loop over a range of integers

While its not provided by C++11, you can write your own view or use the one from boost:

#include <boost/range/irange.hpp>
#include <iostream>

int main(int argc, char **argv)
{
for (auto i : boost::irange(1, 10))
std::cout << i << "\n";
}

Moreover, Boost.Range contains a few more interesting ranges which you could find pretty useful combined with the new for loop. For example, you can get a reversed view.

Is there a way to iterate over a range of integers?

The idiomatic approach in Go is to write a for loop like this.

for i := 1; i <= 10; i++ {
fmt.Println(i)
}

There's definitely advantages in ranges and they're used in many other languages, but a Go design principle is to only introduce an abstraction if the benefits significantly outweigh the costs (including the cost of making the language larger). Reasonable people disagree about the costs and benefits of ranges, but this answer is my attempt to describe what I think idiomatic Go is.

Best way to Iterate over a range of Integers in Java without regex

int min_range = 8230;
int max_range = 8239;

for(int i = min_range * 100000; i < max_range * 100000; ++i)
System.out.println(i);

... if i understood what you meant correctly.

I would also want to ask someone with more knowledge whether the max_range * 100000 would be considered as loop invariant by javac and pulled out of the loop.

what is the cleanest way to loop in a range of numbers in python?

for i in range(start, end+1):
print i

will give you the output from 10 to inclusive 30.

NOTE: The reason I added +1 to your end value is because the range() function specifies a half-closed interval. This means the first value is included, but the end value is not.

I.e.,

  range(5, 10)

would give you a list of

  5, 6, 7, 8, 9

but not include 10.

You can also specify the step size for range() and a host of other things, see the documenation.

Finally, if you are using Python 2.x you can also use xrange() in place of range(), though in Python 3.x you will only have range() which is why I used it here.

If you are curious about the differences between range() and xrange() (though it is not really directly relevant to your question) the following two SO questions are a good place to start: What is the difference between range and xrange functions in Python 2.X? and Should you always favor xrange() over range()?

Shortest way to get an Iterator over a range of Integers in Java

Straight-forward implementation of your homework:

List<Integer> ints = new ArrayList<Integer>();
for (int i = 0; i < count; i++) {
ints.add(first + i);
}

functional way to iterate over range (ES6/7)

One can create an empty array, fill it (otherwise map will skip it) and then map indexes to values:

Array(8).fill(0).map((_, i) => i * i);

How do I iterate over a range of numbers defined by variables in Bash?

for i in $(seq 1 $END); do echo $i; done

edit: I prefer seq over the other methods because I can actually remember it ;)

How do I loop through a list by twos?

You can use a range with a step size of 2:

Python 2

for i in xrange(0,10,2):
print(i)

Python 3

for i in range(0,10,2):
print(i)

Note: Use xrange in Python 2 instead of range because it is more efficient as it generates an iterable object, and not the whole list.

Ruby: How to iterate over a range, but in set increments?

See http://ruby-doc.org/core/classes/Range.html#M000695 for the full API.

Basically you use the step() method. For example:

(10..100).step(10) do |n|
# n = 10
# n = 20
# n = 30
# ...
end


Related Topics



Leave a reply



Submit