Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Collections and Control Flow Control Flow With Conditional Statements Working with Logical Operators

For loops make no sense to me

Not sure how to perform this task.

logicalOperators.swift
var results: [Int] = []

for n in 1...100 {

    // End code 
}

2 Answers

Here you go:

for n in 1...100 {
    // Enter your code below
    if n % 2 != 0 && n % 7 == 0 {
      results.append(n)
    }
    // End code 
}

First you check to see if each n is odd (n % 2 != 0) and evenly divisible by 7 (n % 7 == 0). If so you append n to the results array.

I was forgetting {} after the "if". I was also trying to do n % 3 instead of n % 2. I'm not giving up or just using answers to coast along. I guess I just get flustered trying to accomplish the tasks. I'm fine when I'm building things on my own for ideas I have. I just have trouble trying to interpret what is being asked in the tasks. I've been this far in 2 other languages (Ruby and Objective-C). This one is the easiest for me to write. I'm really trying to get a good comprehension on programming and how to solve these tasks. After breaking your answers down they do really help and put the pieces together for me. I'm very thankful everyone takes their time to help out. you are greatly appreciated Jcorum!

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Carl Smith,

jcorum has already kindly helped you with the context of the challenge, now let me see if I can help you understand for loops better.

A loop is just a way of repeating some sort of specified functionality a given number of times. There are a few different types of loops in Swift: for-in loops, while loops, and repeat while loops. Let's talk about the for-in loop:

A for in loop is just a way of performing a bit of code for each item in a sequence. This could be items in an array or a range of numbers. This challenge is using a for-in loop with a range of numbers.

Let's take a look at the code:

for n in 1...100 {

}

So what's happening here? This loop will run 100 times. The first time it runs, the value of n will be 1. The loop will then execute the code inside of its curly braces (known as the body of the loop). Once all the code inside of the curly braces is finished executing, the value of n will change to the next value in the range, in this case 2. Each complete cycle is known as an iteration. So this process will continue until n is equal to 100. Then the loop will stop executing.

Hope this clears some things up, good luck!

Thank you Steven! It does help, I understand while loops easier than the for. This definitely cleared some stuff up. Greatly appreciated!