Loops Coding Practice (2.7-2.12)

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java2 class periods16 exercises30 graded tests

Files

README.mdDownload

Loops Coding Practice (2.7-2.12)

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

Exercise 1

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

Open the 01-looppractice1 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 2

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

Open the 02-looppractice2 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 3

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

Open the 03-looppractice3 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 4

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

Open the 04-looppractice4 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 5

The 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 press Ctrl+C in the terminal to stop the loop.

Open the 05-looppractice5 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 6

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

Open the 06-looppractice6 folder and edit Test1.java. Press Run to try it; Run Tests checks it (1 test).

Exercise 7

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

Open the 07-looppractice7 folder and edit Test1.java. Press Run to try it; Run Tests checks it (1 test).

Exercise 8

Finish the code to print a countdown from 100 to 0 by 10’s using a for or while loop.

Open the 08-looppractice8 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 9

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.

Open the 09-looppractice9 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 10

Finish the code to print the values for 10 * x where x changes from 0 to 10 using a loop.

Open the 10-looppractice10 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 11

Finish the 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.

Open the 11-looppractice11 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 12

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.

Open the 12-looppractice12 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 13

Write a loop below to print the number of x’s in the string message. Use the substring method.

Open the 13-looppractice13 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 14

Write the code to print a rectangle of stars (*) with 5 rows of stars and 3 stars per row. Hint: use nested for loops.

Open the 14-looppractice14 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 15

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

Open the 15-looppractice15 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

Exercise 16

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

Open the 16-looppractice16 folder and edit Test1.java. Press Run to try it; Run Tests checks it (2 tests).

For teachers

Loops Coding Practice (2.7-2.12)

Students read CSAwesome2's “Loops Coding Practice (2.7-2.12)” in CS Room and work through its 16 Java coding exercises.

  • Time: 2 class periods
  • Tests: 30 in 16 graded exercises, set up for you when you import the lesson

Goals

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–16, each in its own folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-looppractice1Test1.javaAutograded2
02-looppractice2Test1.javaAutograded2
03-looppractice3Test1.javaAutograded2
04-looppractice4Test1.javaAutograded2
05-looppractice5Test1.javaAutograded2
06-looppractice6Test1.javaAutograded1
07-looppractice7Test1.javaAutograded1
08-looppractice8Test1.javaAutograded2
09-looppractice9Test1.javaAutograded2
10-looppractice10Test1.javaAutograded2
11-looppractice11Test1.javaAutograded2
12-looppractice12Test1.javaAutograded2
13-looppractice13Test1.javaAutograded2
14-looppractice14Test1.javaAutograded2
15-looppractice15Test1.javaAutograded2
16-looppractice16Test1.javaAutograded2

Teachers can download a reference solution after signing in.