Swift Basics — Week 5

Loops

Ryan Cortez
5 min readSep 14, 2016

📅 Last week

We talked about Collection Types. This closely relates to our topic today, Loops. We learned that they are a constant/variable that holds constants/variables.

📚 Homework Review

Create a Phone Book

  1. Create a dictionary named “phoneNumbers”
  2. Inside of a function, add your family and friends names with their associated phone numbers attached
  3. Pull your mom’s number out from the dictionary
  4. Call her on the phone

This one possible solution:

phoneNumbers = ["Mom":"815-151-5915", "Dad":"951-195-9511"]

I created a dictionary with two sets of values, one is my mom’s with the key “Mom” and a value of “815–151–5915”. The other key is “Dad” with an associated value of “951–195–9511”.

phoneNumbers[“Mom”]

This means I’m going to search the dictionary “phoneNumbers” for the value associated with the string “Mom”

💭 Loops: (The Concept, without code)

Loop: — A section of code, that runs multiple times automatically and consecutively.

Typically you’ll use a loop to have the computer do repetitive work for you.

A loop runs multiple times in a row and is never used after that. We’ll take a closer look at the concepts of a For Loop and a While Loop.

For Loop: — A loop that runs a fixed number of times

There are two ways to make a For Loop:

  1. Perform your code a repeated, fixed number of times using a number
  2. Perform your code for every item in a collection (such as an Array or Dictionary)

We’ll take a look at some examples in the next section.

While Loop: — A loop that runs over and over, infinitely until a condition is met

I know the “condition” part may be a little ambiguous, but I promise it will make more sense with examples.

What do For Loops do for me?

They save you from having to type the same code, over and over again. You could take all the items (the constants and variables) in an array and do something to each item. Rather than writing many lines of code for each item in the array.

What do While Loops do for me?

Just like For Loops, they save you from having to type the same code, over and over again. While loops help keep a program running and updating. Games use them frequently to continue actions in the game. Such as, having a level scroll to the left as a character runs to the right. It would be a boring game if Mario was in a level and the level just stopped moving 😉.

💻 Loops: (The Code)

Example: — Creating a For Loop using a fixed number

Here I’m creating a loop that will go through the code inside of the brackets five times, I’ll just have the For Loop print the word “something”.

Notice the For Loop starts with the word “for”. It has a variable named “index” that contains the current loop number you’re on. I’ll demonstrate that below, by printing the value inside the index.

Notice that each time the code inside the brackets is run the index goes up once. It goes from 1 to 5. So we start the For Loop with “for” add any name we want for the “index”, and then add “in” in between the index name and the “1…5”. The “1…5” is just saying that I want my index to go from the number ‘1’ to the number ‘5’. Take a look at another example to make this concept more solid.

Example: — Creating a For Loop using an array

Another way to create a For Loop is to have the For Loop perform your code for every item in a collection, such as an array or dictionary. Here were creating an array named “directions”, to hold all four cardinal directions, then creating a loop that goes through every item in the “directions” array.

Example: — Appending string to all existing strings in an array

We’re going to do two things:

  1. We’re going to make a list of names (stored inside an array)
  2. Add words to the end of every name on the list

In this first example, I made the array of names. I made a for loop that prints each name.

Noticed I changed the name for the index to “name” it can be any name you want.

Below I changed the inside of the For Loop, I made a constant named “fullName” to store the edited name. I added a space by using a plus sign and quotes around a space. Then I used another plus sign to add another string titled “Smith”. The loop then prints the “fullName” with the space and “Smith” added.

Example: — Creating a While Loop

Another style of Loop is a While Loop, a While Loop performs your code over and over again infinitely until a condition you write is satisfied.

Here I made a variable named “index” and started it at zero. I used the keyword “while” then placed a condition after it. The condition in English says, “If the index is less than five, perform the code that’s in the loop below.”

Inside the loop I printed a string “The while loop went through once”. It was important for me to change the value of “index” inside the loop; I decided to add one to “index” each time the loop was run.

📒 Homework

  1. Create a loop that prints your name multiple times
  2. Create a loop that prints an array of different names
  3. Create a while loop that prints your name 10 times, no more and no less.

📅 Next Week: — Syntax Practice

Next week will be a little different. I’m going to make a quiz for you to practice your Swift. This will give you a chance to assess if you get the foundational concepts first. We’ve covered a lot: Constants and Variables, Data Types, Collections, Loops. We need to get the concepts down in a concrete way in order to move to the more complex material. See you next week!

--

--

Ryan Cortez

A mediocre Software Engineer based in San Jose, California