Oct-11-2018, 12:37 PM
The example has a couple extra steps that I'll explain at the end.
At the beginning of the loop, we declare a variable:
In other languages, you can only perform a for loop with numbers (e.g. in Java: for i := 0; i < 10; i ++ {...} would loop 10 times) and then use the variable "i" as an index (hence "i" often being used) to an array object. So, to iterate over a sequence in Java (or another language), you have to do something like this (though written in Python):
At the beginning of the loop, we declare a variable:
my_list = ["ketchup", "chicken", "potatoes", "veggies"] for i in my_list:Within the body of the loop, we can then reference the variable we declared:
my_list = ["ketchup", "chicken", "potatoes", "veggies"] for i in my_list: print(i)The variable does not have to be "i"; we can just as easily call it "x", "n", "bobsYourUncle", or "each". In any case, the variable is assigned to the value of the next item in the sequence. For the first iteration over "my_list", "i" is assigned to "ketchup"; when that iteration is done, "i" is reassigned to "chicken" and so forth.
In other languages, you can only perform a for loop with numbers (e.g. in Java: for i := 0; i < 10; i ++ {...} would loop 10 times) and then use the variable "i" as an index (hence "i" often being used) to an array object. So, to iterate over a sequence in Java (or another language), you have to do something like this (though written in Python):
for i in range(len(eww_Java)): value = eww_Java[i] print(value)Python simplifies this by iterating over the sequence itself (the list, tuple, etc.) and assigning the variable to the next value in the sequence.