Swift 3 Logical AND OR operators
These are formatted so you can copy the code straight into your Swift Playground files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//set up a bunch of conditional variables to test in our if statement that contains AND plus OR operators. let hasDoorKey = false let knowsOverridePassword = true let enteredDoorCode = false let passedRetinaScan = true // && is an AND operators and checks if BOTH conditions are true // || is an OR operator, so only one OR condition needs to be true to proceed if ((enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword) { //the first condition was false because only one was true (passedRetinaScan) // the second condition (hasDoorKey) also false // the third condition knowsOverridePassword was true though print ("Welcome") } else { print ( "DENIED") } |
If you’re looking for places to learn after these Swift Playground examples, check out CartoonSmart.com or SpriteKitLessons.com