This book is now obsolete Please use CSAwesome instead.

7.5. Common Mistakes

  • Forgetting to change the thing you are testing in a while loop and ending up with an infinite loop.

  • Getting the start and end conditions wrong on the for loop. This will often result in you getting an out of bounds error. An out of bounds error occurs when you try to access past the end of a string.

  • Jumping out of a loop too early by using one or more return statements inside of the loop.

Here is an example of a while loop that doesn’t ever change the value in the loop so it never ends. If you run it refresh the page to stop it. Fix it.

Here is an example of going past the bounds of a string. This code should double all but the first and last letter in message. Fix the code so that it doesn’t cause an out of bounds an error.

Here is an example of jumping out of a loop too early. The code below is intended to test if all of the letters in a string are in ascending order from left to right. But, it doesn’t work correctly. Can you fix it?

You have attempted of activities on this page