5.10. Coding 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).
Answer: 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).
Answer: 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).
Answer: 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).
Answer: 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 in the browser to stop the loop and then click on Load History and move the bar above it to see your last changes.
Answer: 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.
Answer: 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.
Answer: 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 using a for or while loop.
Answer: You can 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 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.
Answer: 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 using a loop.
Answer: Use a for
loop with x
changing from 0 to 10 and print the value of x
and 10 * x
.
Finish the following code so that it prints a string message minus the last character each time through the loop until there are no more characters in message.
Answer: 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 loop printing the message each time through the loop and remove an x
from the message until all the x
’s are gone.
Answer: 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 a loop below to print the number of x
’s in the string message. Use the indexOf
and substring
methods.
Answer: 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
.
Write the code below to print a rectangle of stars (*
) with 5 rows of stars and 3 stars per row. Hint: use nested for loops.
Answer: 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.
Answer: 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 55555, 4444, 333, 22, 1 with each on a different line.
Answer: 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.