Issue #550 changed for to while

fixes #550 changed for to while