Help Needed with Nesting Loops in Swift Playground

Hi,


I an a beginner in coding so I'm starting to learn with Swift Playground. I have a question with the Nesting Loops exercise in Swift Playground. I don't understand what's wrong with my answer for the exercise and I don't understand the answer given for the exercise.


This was my answer to the Nesting Loops exercise:


While !isBlocked {

While isOnGem {

turnLeft()

collectGem()

}

moveForward()

}


The result I got with this response is the character stopping on the 2nd gem, facing forward and not collecting gem. It turns left and collects the gem at the 1st spot. Why doesn't it do the same thing with the later gems after that? I know While Loops are supposed to keep going but the character is stopping before finishing collecting all the gems.


Maybe there is something wrong with my thinking? I don't know. I wrote this code so that the character keeps on the outer loop until it encounters the inner loop (gem). Once the inner loop is completed, doesn't it go back to following the outer loop again?


Also, the supposed correct answer doesn't make sense to me:


While !isBlocked {

While !isOnGem {

moveForward()

}

collectGem()

turnLeft()

}


Since the way is not blocked for several tiles in the first row, is Byte (the character) supposed to be collecting gems and turning left at every single tile??? I'm baffled by this.

Replies

I did not dig into exercise, but it depends what is the logic you want to move the robot (I assume it is a robot).


But first a comment: that is not the real code !

While will not compile, it must be while.

So when you post code, please post the exact code.


Would be useful to see what happens in isOnGem computed property, collectGem and move or turn funcs

=> Best would be to post the complete playground.


In the answer:

while !isBlocked {
           while !isOnGem {
                     moveForward()
                     }
           collectGem()
           turnLeft()
}


01. As long as robot is not blocked,

search for gem:

02. As long as not on a gem,

03. just moves forward

04. not found: return to 02

Note: That may be infinite walk, unless there is necessarily a gem at borderNote:

Once found gem, (we exit 02. while)

05. collect it and

06. turn left.

If not blocked there, start again 01. to search for gem.


Your code:

while !isBlocked {
          while isOnGem {
                    turnLeft()
                    collectGem()
          }
          moveForward()
}


As long as robot is not blocked,

if you are not on gem, move forward and return to 01. // OK

as long you are on gem

turnLeft

collectGem // Why collect after turning ?

it does not move, so is it always onGem (even though it collected) => That depends on what collect does (does it remove the gem ?)


Once the inner loop is completed, doesn't it go back to following the outer loop again?

It will first execute 06.

You can also try the discussion of these Swift Playground Learn to Code activities at: buildingrainbows.com