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

Robert Vazquez
Robert Vazquez
1,782 Points

How to check for logical checks in an IF statement

Hi I am trying to make the IF statement but I don't know how to check for logical Checks in an IF Statement?

2 Answers

Adam Moore
Adam Moore
21,956 Points

Here is an example, where the operator "%" is used to determine the remainder (even numbers, when divided by 2, will have no remainder, and, therefore, the result of, say, "4 % 2" will be "0"):

for x in 1...10 {
   if x % 2 == 0 {
         print("\(x) is even.")
      } else {
         print("\(x) is odd.")
      }
}

The code that you have linked to in this also incorporates "&&" which allows you to add two conditionals in one place:

if x > 5 && x < 10{

}

I don't want to give away the answer to the challenge TOO much, so let me know if this helps! :)

Adam Moore
Adam Moore
21,956 Points

I know someone else had already received best answer, but it bothered me that the rest of my answer wasn't included my response, so I modified it so that it would.

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

The challenge wants you to check for odd number && mutiple of 7.

Hint: To check for an odd number you can either use the remainder operator and 3, or use the not operator to check for "not even"

The hint says that you may use the not operator != to check for inequality.

One way to write the if statement is as the following

if n % 2 != 0 && n % 7 == 0 {
   // append number to the Array
}