What you have written is something correct but it should be if instead of while.