1. sanaranjo's Avatar
    This is my first time every playing in Swift Playground. Total coding newbie. As I started playing in the playground, I just couldn't stop. These puzzles are really fun. Although this one command in particular is kicking my butt. Here is the command:

    Code:
    while !isBlocked && !isOnClosedSwitch || !isOnOpenSwitch {
        moveForward()
    }
    And here is a screenshot.

    For me, this says "as long as the path is not blocked AND is not sitting on an open or closed switch", then the statement would be true. However, the character just keeps walking through this command. Here is a screencast:

    https://www.screencast.com/t/5QJXBl71

    Right at 30 seconds, the little character continues to walk forward. But why? Since the character is sitting on a closed switch, shouldn't the character stop?

    Doesn't "!isOnClosedSwitch" = is not on a closed switch?

    Please help.
    06-24-2020 08:34 PM
  2. Just_Me_D's Avatar
    Hopefully someone will be along to assist you soon.
    06-25-2020 07:53 PM
  3. sinime's Avatar
    A little late and you probably found the solution, but I'm going to take a stab at it....

    because of the || (or) statement, since it's not on open switch, it will ignore if it is blocked/closed and it will return true, move forward.

    && means both items have to return true to execute the if statement's code
    || means only the left or right side has to be true to execute the if statement's code.

    if you want to stop it "as long as the path is not blocked AND is not sitting on an open or closed switch" try changing it to...

    "as long as the path is not blocked AND is not sitting on an open switch AND not sitting on a closed switch"
    Code:
    while !isBlocked && !isOnClosedSwitch && !isOnOpenSwitch {
        moveForward()
    }
    FWIW and I don't think it would help in this situation, but whenever I use && and || in the same if statement, I usually try to add () around certain blocks for clarity in my statement groupings.for example...

    if a && b || c
    is the same as
    if (a && b) || c
    but not the same as
    if a && (b || c)
    Last edited by sinime; 10-08-2020 at 10:52 PM.
    HelloNNNewman likes this.
    10-08-2020 10:37 PM

Similar Threads

  1. Apple Privacy in 2020: Why you'll want these feature right now
    By iMore.com in forum iMore.com News Discussion & Contests
    Replies: 0
    Last Post: 06-24-2020, 04:12 PM
  2. Apple is closing its retail stores in Texas again as COVID-19 cases rise
    By iMore.com in forum iMore.com News Discussion & Contests
    Replies: 0
    Last Post: 06-24-2020, 02:51 PM
  3. Become a data master with this in-depth Excel training
    By iMore.com in forum iMore.com News Discussion & Contests
    Replies: 0
    Last Post: 06-24-2020, 01:12 PM
  4. Rumored 20W iPhone 12 charger spotted in certifications
    By iMore.com in forum iMore.com News Discussion & Contests
    Replies: 0
    Last Post: 06-24-2020, 11:12 AM
  5. DOJ 'taking the first steps towards' Apple antitrust probe in the U.S.
    By iMore.com in forum iMore.com News Discussion & Contests
    Replies: 0
    Last Post: 06-24-2020, 10:30 AM
LINK TO POST COPIED TO CLIPBOARD