Lesson 1
Basic For Loops - Iterating Over Collections
Introduction to Basic For Loops

Welcome to our first lesson on loop structures in Swift! As you may know, loops allow us to repeat a set of instructions efficiently. This unit will introduce you to the basic for loop, which is fundamental for iterating over collections like arrays or dictionaries. Imagine having a list of tasks and being able to automate their completion. That’s what loops help you achieve.

What You'll Learn

By the end of this lesson, you'll learn how to use for loops to iterate over collections in Swift. We’ll start with a simple example of looping through an array of planets and printing out messages for each one.

Here's a sneak peek:

Swift
1// List of planets in the mission scope 2let planets = ["Mercury", "Venus", "Earth", "Mars"] 3 4// Using a for loop to print all the planets 5for planet in planets { 6 print("Mission includes a visit to \(planet).") 7}

We'll cover each part of this code in detail.

Why It Matters

Understanding how to use for loops is essential because it allows you to automate repetitive tasks. If you can write a loop to do something repeatedly, you can save time and reduce errors in your programs. This skill is extremely useful not just in space exploration simulations but in many other real-world applications, like data processing, game development, and more.

Feeling ready to dive in? Let's get started on writing some loops in the practice section and see these concepts in action!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.