Key Concepts

Review core concepts you need to learn to master this subject


let zeroToThree = 0...3 // zeroToThree: 0, 1, 2, 3

Ranges created by the ... operator will include the numbers from the lower bound to (and includes) the upper bound.

stride() Function

let zeroToThree = 0...3 // zeroToThree: 0, 1, 2, 3

Calling stride() with the 3 necessary arguments creates a collection of numbers; the arguments decide the starting number to, the (excluded) ending number, and how to increment/decrement from the start to the end.

for-in Loop

let zeroToThree = 0...3 // zeroToThree: 0, 1, 2, 3

The for-in loop is used to iterate over collections, including strings and ranges.

continue Keyword

let zeroToThree = 0...3 // zeroToThree: 0, 1, 2, 3

The continue keyword will force the loop to move on to the next iteration.

break Keyword

let zeroToThree = 0...3 // zeroToThree: 0, 1, 2, 3

To terminate a loop before its completion, use the break keyword.

Using Underscore

let zeroToThree = 0...3 // zeroToThree: 0, 1, 2, 3

Use _ instead of a placeholder variable if the variable is not referenced in the for-in loop body.

while Loop

let zeroToThree = 0...3 // zeroToThree: 0, 1, 2, 3

A while loop accepts a condition and continually executes its body’s code for as long as the provided condition is true.

If the condition is never false then the loop continues to run and the program is stuck in an infinite loop.

Lesson 1 of 1
  1. 1
    Every day, we repeat a lot of things and we might not even think about it. For instance, when we cook, we might have to prepare our ingredients by chopping them up. We chop and chop and chop until …
  2. 2
    Before we get to writing our own loops, let’s explore what programming would be like if we couldn’t use loops. Let’s say we want to make a program that prints “Hip Hip Hooray!” 5 times: print(“…
  3. 3
    Now that we can appreciate what loops do for us, let’s start with the for-in loop. Below is the general syntax: for placeholderVariable in someSequence { for-in loop body } We use [for-in](ht…
  4. 4
    for-in loops also give us the flexibility to choose how we want to iterate over a sequence with the stride() function: stride(from: a, to: b, by: c) Notice, in order to use the stride() function…
  5. 5
    Aside from sequences of numbers, we can also loop over Strings! After all, a string is a collection of characters. Let’s say we wanted to look over a string and see if it contains “z”. In a progr…
  6. 6
    Remember our “Smelly code” chalkboard? Smelly code, or often referred to as having a code smell refers …
  7. 7
    When iterating through a sequence, we might not need to use every single value. In these cases, it’d be nice to just tell our loop to skip a value. That’s where continue comes in. If we wanted to …
  8. 8
    Let’s say we need to find out what “respect” means in the dictionary. We would iterate through the pages of the dictionary until we found the word “respect” and read the definition. Although we had…
  9. 9
    Another loop we can use is the while loop. This loop allows us to continue iterating for as long as a condition remains true. while condition { // Execute while condition remains true } while…
  10. 10
    for _ in 1…10 { print(“Great job!”) } print(“Now time to review loops!”) The above syntax should now look familiar! In this lesson, we went over: Manually repeating tasks. for-in loops a…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo