Lists and Iteration
Lists (also called arrays in some programming languages) store multiple values in order. Each item in a list is called an element, and each has a position number called an index:
myList = ["apple", "banana", "cherry"]
Here, "apple" is at index 0, "banana" at index 1, and "cherry" at index 2.
📋 Remember that most programming languages start counting list positions from 0, not 1!
Iteration means repeating code, often to process each item in a list. A common way to iterate is with a loop:
for (let i = 0; i < myList.length; i++) {
console.log(myList[i]);
}
This code will print each fruit in the list. This approach of going through each item one by one is called traversal.
When developing programs, it's best to use incremental development - building your program in small, testable chunks rather than all at once. This makes finding and fixing problems much easier.
Be careful to avoid infinite loops (loops that never end) and logic errors (mistakes in your code's reasoning that cause incorrect results but don't produce error messages). These bugs can be tricky to spot but are common in programming.