Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,865 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
}