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

Why does the word print output in swift?

var buttonUp=true
var buttonDown=false

if ((buttonUp && buttonDown)==false){
    println("apple")
}

When I try the code, in the console "apple" is printed even though I put in the if statement if the buttonUp AND buttonDown are false, then print "apple"?

Never mind, the AND operator is ||. The following website has this wrong for future reference: http://nshipster.com/swift-operators/.

3 Answers

if ((buttonUp && buttonDown)==false){
    println("apple")
}

This statement says "if buttonUp and buttonDown statement is equal to false; print apple". So if either of the variables is false in any way the statement (buttonUp && buttonDown) returns false and the total if-statement becomes true.

A correct solution to this problem is:

if (!buttonUp && !buttonDown) {
   println("apple")
}

or

if (buttonUp == false && buttonDown == false) {
   println("apple")
}

Where "if buttonUp is false AND buttonDown is false; print apple".

So there is no confusion: && is AND, || is OR, ! is reverse value

Thanks

Thanks

Hi Steve,

To back up what Patrick is saying the OR operator is || whilst the AND operator is in fact && as you've correctly used above. From your code it looks as if you want to say (In pseudo) "If button up and button down are both false then do this".

This makes logical sense and the tidiest way to do this would be to check both variables for their Boolean value separately but within the same if statement, so in your example you've given us you would want:

if (buttonUp == false && buttonDown == false){
    println("apple")
} 

This is the same as what Jonas has posted previously and what Swift will do with the above code is first check the buttonUp variable to see if it is false and will return true or false then will do the same for buttonDown. Swift can then take the two Boolean values that have been returned and check them, if they both return True then the code below runs, if not then the code will not run.

The reason for the above nature is down to how Truth tables work for Swift (And I imagine most languages too) which are as follows:

// AND
true && true // true
true && false // false
false && true // false
false && false // false

// OR
true || true // true
true || false // true
false || true // true
false || false // false

As you can see from the above using the && operator allows that only when both Boolean values are true that the results will be true, anything else will simply return false. There is nothing wrong with your original code but for the result you are after you need a slightly different approach as when you are doing:

if ((buttonUp && buttonDown)==false){
}

You are checking to see if the result of buttonUp AND buttonDown is false once you have amalgamated the two, this is the same as: true && false // false

Which as per the truth table rings true (no pun intended!)

Operator form can certainly be a bug bear but I hope the above does at least provide some clarity. If however it still doesn't make sense or you'd like me to go through it in more detail or with more clarity please do just let me know and I'll give it another crack for you.

Thank you so much. Could you explain what you mean by "amalgamated "?

I'm glad I could help! Oh and yes of course, that's just mean using unnecessary wording! However, amalgamation is simply another word for combining or mixing an object, both in real world and programming terms. Simply put, you take two objects, put them together and you then get a new object which is the result of mixing the two together. To relate this to your problem an example would be:

//Original objects
var buttonUp=true
var buttonDown=false

//Amalgamated object
var buttonAmalgamation = buttonUp && buttonDown

It's good to note that Amalgamation, Mixing, Combining, etc are different to "Concatenation" which you might hear a bit. The key difference is that concatenation combines two objects "end-to-end" such as when you concatenate two strings such as:

var firstString = "My favourite colour is"
firstString += " blue"
//The result of this would simply be My favourite colour is blue

In summary and to apply to real world terms as much as possible the two meanings you would "amalgamate" two liquids like whiskey and coke, but if you were adding a a few rails to the end of a train track for example you would "concatenate" the track with the new rails.

Hope that all makes sense :)

I could be wrong as I haven't used Swift a ton yet but I don't think what you're doing is valid Swift. In most languages you can't say if this && this == true you have to say if this == true && this == true.

Yeah, I tried yours, and unfortunately like I said && is the OR operator and || is the AND operator. This seems to work

(buttonUp||buttonDown)==false

No, || is OR and && is AND. This is common across pretty much any language. And it is never the reverse even if they don't use these symbols (I have yet to come across a language that doesn't). I did look it up and it looks like you can do compounds like you did in the first post but since you want to check if it's false you want to ditch the first set of perens and use the ! (NOT) operator.

var buttonUp = true
var buttonDown = false

if !(buttonUp && buttonDown) {
    println("apple")
}

This way you're saying if buttonUp AND buttonDown are NOT true print apple.

I just tried what you suggested in the playground, and "apple" is printed out, so that seems to point to the fact that && is the OR operator. Like you said, I am aware that in every other language it is different, thats why I tried && in the first place. Can you try your code above, and see what happens. Thanks again.

If you follow this link and scroll down to "Logical Operators" you will see that || is OR and && is AND. I'm at work on an old version of OS X so I can't download the new Xcode unfortunately. I'll give my code a try when I have a playground available to me. I feel it's important to repeat, || is OR and && is AND. This is per the Apple documentation.