replaces while loop with if-else loop