Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] Array : loop 0 to Length ou 0 to Length-1

Author
Message
Hubert BAYRE
6
Years of Service
User Offline
Joined: 21st Oct 2017
Location: France
Posted: 13th Jun 2019 11:05
i've some doubt with my code today... with the For loop.

To have all the item inside the array ... 0 to Length or 0 to Length-1 ?



Many thanks.

The author of this post has marked a post as an answer.

Go to answer

blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 13th Jun 2019 12:18
This post has been marked by the post author as the answer.
Yes it should be .length not .length - 1. So Arrays start from zero and array.length always points to the last element

Sometime people will get into trouble by querying if the length is a certain value and then referencing the element like so



Because basic evaluates ALL the elements of a condition this will still fail because if index is greater than myArray.length it will still try to resolve myArray[index]



One thing i do that reduces the array referencing is to place the array element into a type at the top of the loop like this;



You just need to remember to set your array to the temporary variable at the end of processing the element
Hubert BAYRE
6
Years of Service
User Offline
Joined: 21st Oct 2017
Location: France
Posted: 13th Jun 2019 18:24
Many thanks blink0k

Login to post a reply

Server time is: 2024-10-01 08:14:03
Your offset time is: 2024-10-01 08:14:03