In programming, a FOR destination (more commonly called the iterable or iteration source) defines the collection of values that a loop variable will sequentially assume during each iteration of a FOR loop.
for identifier in destination:
# loop body
commands
Where:
numbers = [1, 2, 3, 4]
for number in numbers:
print(number)
# Output: 1, 2, 3, 4 (each on new line)
for number in range(5):
print(number)
# Output: 0, 1, 2, 3, 4
word = "Hello"
for letter in word:
print(letter)
# Output: H, e, l, l, o
with open("myfile.txt", "r") as file:
for line in file:
print(line.strip())
ages = {"Alice": 30, "Bob": 25}
for name, age in ages.items():
print(f"{name} is {age} years old")
unique_numbers = {1, 2, 3, 2, 1}
for num in unique_numbers:
print(num)
# Output: 1, 2, 3 (order not guaranteed)
__iter__() or __getitem__() can be a FOR destinationfor x, y in points:for-else executes else block if loop completes without breakWhile syntax differs, the concept exists across languages:
for (const item of iterable)for (Type item : collection)for (auto& item : container)foreach (var item in collection)