# 7.12. Code Practice with Loops¶

Rewrite the following code so that it uses a `for` loop instead of a `while` loop to print out all the integers from 5 to 1 (inclusive).

In a `for` loop you declare and initialize the variable(s), specify the condition, and specify how the loop variable(s) change in the header of the `for` loop as shown below.

Rewrite the following code to use a `while` loop instead of a `for` loop to print out the numbers from 1 to 10 (inclusive).

You need to specify the declarations and initializations of the loop variables(s) before the Boolean condition. You need to do the change(s) at the end of the body of the loop.

Rewrite the following code so that it uses a `for` loop instead of a `while` loop to print out all the integers from 5 to 15 (inclusive).

In a `for` loop you declare and initialize the variable(s), specify the condition, and specify how the loop variable(s) change in the header of the `for` loop as shown below.

Rewrite the following code to use a `while` loop instead of a `for` loop to print out the numbers from 10 to 100 by 10’s (inclusive).

You need to specify the declarations and initializations of the loop variables(s) before the Boolean condition. You need to do the change(s) at the end of the body of the loop.

The following code should print the values from 1 to 10 (inclusive) but has errors. Fix the errors so that the code works as intended. If the code is in an infinite loop you can refresh the page to stop the loop.

On line 6 it should be `while (x <= 10)`. Add line 9 at the end of the loop body to increment `x` so that the loop ends (isn’t an infinite loop).

The following code should print the values from 10 to 5, but it has errors. Fix the errors so that the code works as intended.

Remove the `x--;` at the end of the body of the loop. The change area in the for loop decrements `x` by 1, so this line isn’t needed.

The following code should print the values from 10 to 1, but it has errors. Fix the errors so that the code works as intended.

Move the `x--;` to the end of the loop body (after the `System.out.println`. Change the `while` to `x > 0`.

Finish the code below to print a countdown from 100 to 0 by 10’s.

Use a `for` loop as shown below. Start `x` at 100, loop while it is greater or equal to 0, and subtract 10 each time after the body of the loop executes.

Finish the following code so that it prints a string minus the last character each time through the loop until there are no more characters in the string.

Add a `while` loop and loop while there is still at least one character in the string. At the end of the body of the loop reset the message to all characters except the last one.

Finish the code to print the value of `x` and `" is even"` if `x` is even and `" is odd"` if it is odd for all values from 10 to 1.

Use a `for` loop to loop from 10 to 1. Use a conditional to test if x is even (x % 2 == 0).

Finish the code below to print the values for `10 * x` where `x` changes from 0 to 10.

Use a `for` loop with `x` changing from 0 to 10 and print the value of `x` and `10 * x`. Use parentheses around `x * 10` to make sure it is evaluated before it is turned into a string.

Finish the code to loop printing the message each time through the loop and remove an `x` from the message until all the `x`’s are gone.

Use a `while` loop. Loop while `x` has been found in the message (using `indexOf`). Remove the `x` (using substring). Use indexOf again to get the position of the next `x` or -1 if there are none left in the message.

Write the code below to print 55555, 4444, 333, 22, with each on a different line.

Use nested `for` loops. The outer loop controls what is printed on each row and the number of rows. The inner loop controls the number of values printer per row.

Write the code below to print a rectangle of stars (`*`) with 5 rows of stars and 3 stars per row.

Use nested `for` loops. Use the outer loop to control the number of rows and the inner loop to control the number of stars per row.

Write the code below to print a rectangle of stars (`*`) with 3 rows of stars and 5 stars per row.
Use nested `for` loops. Use the outer loop to control the number of rows and the inner loop to control the number of stars per row.
Write the code below to print the number of `x`’s in the string message. Use the `indexOf` and `substring` methods.
Use indexOf to find the next `x`. Loop while pos is greater than or equal to 0. Use substring to reset message beyond the next `x`.