Loops Coding Practice (2.7-2.12)
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods16 exercises30 graded tests
Files
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-looppractice1folder and editTest1.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-looppractice2folder and editTest1.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-looppractice3folder and editTest1.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-looppractice4folder and editTest1.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-looppractice5folder and editTest1.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-looppractice6folder and editTest1.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-looppractice7folder and editTest1.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-looppractice8folder and editTest1.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-looppractice9folder and editTest1.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-looppractice10folder and editTest1.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-looppractice11folder and editTest1.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-looppractice12folder and editTest1.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-looppractice13folder and editTest1.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-looppractice14folder and editTest1.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-looppractice15folder and editTest1.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-looppractice16folder and editTest1.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
- Practice for topics 2.7–2.12: 2.7 While Loops, 2.8 For Loops, 2.9 Implementing Selection and Iteration Algorithms, 2.10 Implementing String Algorithms, 2.11 Nested Iteration, 2.12 Informal Runtime Analysis of Loops.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/loops-practice-coding.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–16, each in its own folder, pressing Run to try their code.
- 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
| Folder | File | Grading | Tests |
|---|---|---|---|
01-looppractice1 | Test1.java | Autograded | 2 |
02-looppractice2 | Test1.java | Autograded | 2 |
03-looppractice3 | Test1.java | Autograded | 2 |
04-looppractice4 | Test1.java | Autograded | 2 |
05-looppractice5 | Test1.java | Autograded | 2 |
06-looppractice6 | Test1.java | Autograded | 1 |
07-looppractice7 | Test1.java | Autograded | 1 |
08-looppractice8 | Test1.java | Autograded | 2 |
09-looppractice9 | Test1.java | Autograded | 2 |
10-looppractice10 | Test1.java | Autograded | 2 |
11-looppractice11 | Test1.java | Autograded | 2 |
12-looppractice12 | Test1.java | Autograded | 2 |
13-looppractice13 | Test1.java | Autograded | 2 |
14-looppractice14 | Test1.java | Autograded | 2 |
15-looppractice15 | Test1.java | Autograded | 2 |
16-looppractice16 | Test1.java | Autograded | 2 |
Teachers can download a reference solution after signing in.