Can you create an array like this

Xer0botXer0

Senpai
Hi guys, quick question

say I create an array like so:

for (i = 0; i < 10; i +=2)
{
arr_val = -1;
}

Will I have six entries (0,2,4,6,8,10) or 11 ?
 
Well, I hope what you wrote is pseudocode, because that will literally just make a variable called array_val equal to -1 five times in a row.
Code:
for (var i=0;i<10;i++) {
   array[i] = -1;
}
What this code will do is create an array and set it's first 5 values to -1. Why 5? On the first loop, i = 0; on the second loop, i = 2; on the third loop, i = 4; on the fourth loop, i = 6; on the fifth loop, i = 8; then finally the loop is cancelled because i = 10, which is because i >= 10, so the comparison in the for loop (i<10) is no longer valid and it exits the loop. So the answer is neither 11 nor 6, it is 5.
 
Z

Zep--

Guest
you will have 9 entries

Code:
for (i = 0; i < 10; i +=2)
{
arr_val[i] = i;
}

show_message(string(array_length_1d(arr_val    )));

show_message(string(arr_val[0]));
show_message(string(arr_val[1]));
show_message(string(arr_val[2]));
show_message(string(arr_val[3]));
show_message(string(arr_val[4]));
show_message(string(arr_val[5]));
show_message(string(arr_val[6]));

etc...
 
Z

Zep--

Guest
Just because you are trying to skip slots in the array, doesn't mean those empty slots aren't created in the array with some value.
 
Top