Nesting Loops Explanation Needed

I am brand new to coding and loving Swift Playground. When I got to the Nesting Loops level, I did not understand the organization of the correct code.

Here is a link to the correct solution.

My original code had the collectGem and turnLeft commands directly below the while !isOnGem with the !moveForward command at the bottom - it worked until the second gem. I don't understand how the nested loops commands are organized and the hints section did not help. Thanks for any further explanation for this as I'd like to understand nested loops before continuing. I could not find any online info that provided support for this, so if this post should be in another section, please advise.

Replies

YEs, the logic seems off. Would some one walk us through the logic. Thx.

I have the same question too.


Actually if you're going to use isOnGemwith turnLeft() and collectGem(), there is no exclamation mark (signifies "not").


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. 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???