Looping through an array with step
Ranges have a step
method which you can use to skip through the indexes:
(0..array.length - 1).step(2).each do |index|
value_you_care_about = array[index]
end
Or if you are comfortable using ...
with ranges the following is a bit more concise:
(0...array.length).step(2).each do |index|
value_you_care_about = array[index]
end
loop through array or list in variable steps
Don't use a for
loop.
for
loops in python are different than in C or Java. In those languages, a for
loop has an initial condition, a termination condition, and an increment for each time the loop runs. Whereas in python, a for
loop is more of a for each
loop - you give it an iterable object, and it runs the code for every item in that iterable object.
Modifying the iterable object while you're running through it is a bad idea that can have difficult-to-predict repercussions and will usually break your code.
However, you can always use a while
loop:
a = [0,0,1,0,0,1,0]
idx = 0
while(idx < len(a) - 2):
print(idx)
if a[idx + 2] == 0:
idx += 2
elif a[idx + 2] == 1:
idx += 1
print(idx)
which produces the expected output
0 1 3 4 6
Or, if you change the increments to 3
and 2
respectively, rather than 2
and 1
,
0 2 5
Javascript for-loop with certain step
Your function needs a separate parameter for the stride and the size of each part. It uses the stride when incrementing the for
loop variable, and the size when taking the slice.
function splitArray(array, stride, size) { var tmp = []; for(var i = 0; i < array.length; i += stride) { tmp.push(array.slice(i, i + size)); } return tmp;}
var m1 = [];for (var i = 0; i < 100; i++) { m1.push(i);}console.log(splitArray(m1, 10, 6));
How does a for loop iterate through an array?
I am going to use a slightly different analogy, since a lot of answers are providing you with the correct information.
An array is just a list of sequential boxes containing values. Imagine it is a list of houses, each numbered from 0 (arrays in Java like many other programming languages start at 0 not 1) to n
(in your case it is 2).
So if you were someone collecting donations from each house, you would start from the first house, 0, then proceed to the next house 1, then to the next house 2. You would keep track of which house you are visiting now, so that you also know which is the next one, and that is what the integer variable i
is doing.
Printing numbers[i]
does not make the loop cycle, in the same way that knocking on the first house does not mean you are going to knock at the second house.
The for
loop is keeping track of which house numbers you need to visit next. It does it by the 3 statements it has in the same line:
for(int i = 0; i < number.length; i++)
The first statement is saying start from the first house (int i = 0
). You could start wherever you like.
The second statement is saying, you want to go up to the last house, irrespective of how many houses there are. (i < number.length
). This literally translates to keep looping as long as i
is less than the size of the array. You might ask why isn't the condition <=
. It is because we start from 0, so the length (size of the array) will be the number of the last house + 1. In your case, length = 2 + 1
.
The third statement, is what is making you move to the next house. i++
is shorthand for saying i = i + 1
. This statement takes place after every iteration of the loop.
So essentially your for loop first initialises i to 0, checks the condition (the second statement) and if the condition is satisfied does the first iteration, then it finally increments i
, to start another iteration if the second condition is still satisfied. This loop is repeated until the second statement of your for
expression is not true any more.
Finally all numbers[i]
means is, since numbers
is an array, the [ ]
operator accesses the value at the specified index. So it is like knocking at the house with number i
to see who lives there. Feeding the value to System.out.println()
prints that value on screen.
Continue iterating through an array at certain step beyond its boundaries
You are talking about circular arrays, where where the index excedes the array size you start at the beginning again
int[] arr = new arr[3];
int i = 4;
if (i > 0)
int index = (i % arr.Length) ;
and in a step iteration format
int step = 2;
for(int i=0; i < ? ; i+= step)
{
int index = (i % arr.Length) ;
// do something with index
}
Loop through an array by set of 4 elements at a time in Javascript?
Use a for loop where i increases by 4.
Note: I was working on my answer when Jaromanda X commented the same thing.
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
for (var i = 0; i < arr.length; i += 4) { console.log("Working with: " + arr.slice(i, i + 4));}
Bash - how to iterate over array in steps of 2?
Here you have a simple way in Bash.
#!/bin/bash
data=("Banana" "Apple" "Onion" "Peach")
for ((i=0;i< ${#data[@]} ;i+=2));
do
echo ${data[i]}
done
Regards!
Javascript: How to loop through every array's item every second
You can use an interval.
Here is an example:
var arr = [0,1,2,3,4];var index = 0;setInterval(function(){ console.log(arr[index++ % arr.length]);}, 1000)
Related Topics
How to Set Default Ruby Version with Rvm
How to Require Active Record Working Outside of Rails
How to Simulate Java-Like Annotations in Ruby
Project Euler 1:Find the Sum of All the Multiples of 3 or 5 Below 1000
Why Doesn't Minitest::Spec Have a Wont_Raise Assertion
Shell Out from Ruby While Setting an Environment Variable
Ruby/Rails: Converting a Date to a Unix Timestamp
What's the Difference Between Design Patterns and Design Principles
Devise - How to Change Setting So That Email Addresses Don't Need to Be Unique
Activerecord - Querying Polymorphic Associations
Size, Length and Count in Rails
How to Uninstall Ruby on Rails on MAC Os X
How to Say Something Happened "X Minutes Ago" or "X Hours Ago" or "X Days Ago" in Ruby
Return Index of All Occurrences of a Character in a String in Ruby