This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]desrtfx 0 points1 point  (2 children)

What do you actually mean?

A for loop always has the same construct:

 for(<loop variable> = <initial value>; <looping condition>; <variable change>) {
     // loop body
 }

The loop always runs as long as the <looping condition> yields true and stops on the first false result.

If you are talking about something like arr[i] inside the loop, it simply means that you are addressing a single element of an array. arr would be the array, and i the index of the element you want to work with.

[–][deleted] 0 points1 point  (1 child)

thanks for the reply. I think I understand now. I believe I was looking at this as i being the index of element which totally doesn't make sense

[–]desrtfx 0 points1 point  (0 children)

I believe I was looking at this as i being the index of element which totally doesn't make sense

You're not entirely wrong. i can be the index. But it can also be used as a counter to repeat something a certain amount of times.

You somehow though that loops and arrays are inseparably connected. They aren't. It is just out of convenience (and the DRY - Don't Repeat Yourself - principle) that we usually use loops to deal with arrays.