Try
func collectToggle() {
if isOnGem {
collectGem()
} else if isOnClosedSwitch {
toggleSwitch()
}
}
while !isOnOpenSwitch {
collectToggle()
if isBlocked && isBlockedLeft {
turnRight()
} else if isBlocked && isBlockedRight {
turnLeft()
} else if isOnOpenSwitch && isBlocked {
turnLeft()
}
moveForward()
}
it's less confusing in my opinion