0

I want to know the name of the "temporary variable" that you access inside a for-loop. It's not really language-specific, but here's my code in Swift:

let array = [1, 2, 3]
for number in array {
    print(number) /// what is this?
}

What is number, the "temporary variable" that is different in each iteration, known as?

Here are my attempts at describing it.

  • The current iteration's element
  • The current element

Then, what if I was looping over the array's indices? How would I refer to number in this case?

let list = [1, 2, 3]
for i in 0..<list.count {
    let number = list[i] /// what is this?
    print(number)
}

My attempts:

  • The element of the list array at the current iteration's index
  • The element of the list array at the loop's current iteration's index
aheze
  • 24,434
  • 8
  • 68
  • 125
  • @ aheze are you trying to iterate both index and element of an array? –  May 15 '21 at 06:03
  • code one & two both will print `element`(value) of an array. but first one you directly call the value of an array & second will call value via array index. –  May 15 '21 at 06:11
  • In both cases `number` is a local variable that exists in the scope of the `for` loop so it is not different from any other variable in that it exists in the scope it was declared. – Joakim Danielson May 15 '21 at 06:52
  • 1
    It’s also known as *Index variable*. – vadian May 15 '21 at 09:33

1 Answers1

1

let's take one example.

let array = [1, 2, 3]
for number in array {
    print(number)
}

for the above case, the loop will print each number (element/Object) of the array(Not the index of the element).

It means if the array is [a,b,c,d], it will print abcd

For the second case, you are iterating loop over a range of array and then get the element based on an index

let list = [1, 2, 3]
for i in 0..<list.count {
    let number = list[i] /// what is this?
    print(number)
}

The above example, i is referred to the index of an array value and let number = list[i] code will give you an element at the i index.

In last, If you want both indexes with the element within a single loop. You can use .enumerated()

Here is one example

let array = [1, 2, 3]
for (index, number) in array.enumerated() {
    print("Array object/ Element Index :- ", index)
    print("Array object/ Element :- ", number)
}
Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
  • "element at the `i` index" I think this is at close at it gets, thanks! – aheze May 15 '21 at 06:26
  • The element at an index refers to a value in the array and not to the variable it is assigned to. – Joakim Danielson May 15 '21 at 06:54
  • Yes, I mean this. Just sentence word mistake. Thanks – Raja Kishan May 15 '21 at 07:02
  • 1
    @aheze be careful when using enumerated. It returns the offset not the index. If you use it with a slice of the collection it will probably crash your app. You should always use its indices. https://stackoverflow.com/a/54877538/2303865 – Leo Dabus May 20 '21 at 04:46
  • @LeoDabus thanks, gotcha! Actually I was only trying to find out the academic term for the iterator thing (which seems to be called the "**Index variable**"). RajaKishan just gave a (very appreciated) extended answer. – aheze May 20 '21 at 04:56