Section2.20Unit 2 Part 2 Summary on Loops (2.7-2.12)
In the second half of this unit, lessons 2.7-2.12, you learned about loops. Loops are used to repeat a statement or block of statements inside a pair of curly braces.
Body of a Loop - The single statement or a block of statements that can be repeated (a loop may not execute at all if the condition is false to start with). In Java the body of the loop is either the first statement following a while or for loop is the body of the loop or a block of statements enclosed in { and }.
For Loop - A loop that has a header with 3 optional parts: initialization, condition, and change. It does the initialization one time before the body of the loop executes, executes the body of the loop if the condition is true, and executes the change after the body of the loop executes before checking the condition again.
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.
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 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?