Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for this problem set:
for
loops for repeating lines of codefor
loops
A for
loop repeats a group of statements a given number of times.
for (initialization; test; update) { statement(s) to repeat; }
Example:
for (int i = 1; i <= 10; i++) { System.out.println("We're number one!"); }
for
loop that produces the song Bottles of Beer on the Wall:
10 bottles of beer on the wall, 10 bottles of beer Take one down, pass it around, 9 bottles of beer on the wall 9 bottles of beer on the wall, 9 bottles of beer Take one down, pass it around, 8 bottles of beer on the wall ... (output continues in the same pattern) ... 1 bottles of beer on the wall, 1 bottles of beer Take one down, pass it around, 0 bottles of beer on the wall
for
loop table practice
Create a table of the number of stars on each line:
******* ***** *** * |
|
multiplier: How much does the # of stars change between lines? -2 shift: Given your multiplier, what must be added to get that many stars on line 1? 9 |
Test your loop expression in Practice-It! using the checkmark icon above. Use the form:
for (int stars = 1; stars <= multiplier * line + shift; stars++) {
for
loop
public class Count2 { public static void main(String[] args) { for ( fill me in! ) { System.out.println( fill me in! ); } } }
2 times 1 = 2 2 times 2 = 4 2 times 3 = 6 2 times 4 = 8
Our Practice-It! system lets you solve Java problems online.
for
loop:
8 11 14 17 20 23
******** *********** ************** ***************** ******************** ***********************
What output is produced by the following Java program? Write the output in the box on the right side.
public class OddStuff { public static void main(String[] args) { int number = 32; for (int count = 1; count <= number; count++) { System.out.println(number); number = number / 2; } } } |
Output: 32 16 8 4 |
for
loops to produce the
following output:
000111222333444555666777888999 000111222333444555666777888999 000111222333444555666777888999
99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000 99999888887777766666555554444433333222221111100000
999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221 999999999888888887777777666666555554444333221
public static void stars() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int j = 1; j <= 20 - 2 * i; j++) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } }
* * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* ******** ******** ********* ********* ********************
public static void mystery() { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.print(i * j + "\t"); } System.out.println(); } }
\t
represents a tab character.
Answer:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Write a program to produce the following output using nested for
loops. Use a table to help you figure out the patterns of characters on each line.
-----1----- ----333---- ---55555--- --7777777-- -999999999- |
|
|
Test your loop expressions in Practice-It! using the checkmark icon above. Use your expressions in the loop tests of the inner loops of your code.
Write a Java program in a class named SlashFigure
to produce the following output with nested for
loops. Use a loop table if necessary to figure out the expressions.
!!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!////// \\\\\\\\!!!!!!//////// \\\\\\\\\\!!////////// |
|
Test your code in Practice-It! or the Output Comparison Tool.
A class constant is a global value that cannot be changed.
public static final type name = expression;
Example:
public static final int DAYS_PER_WEEK = 7; public static final double TAX_RATE = 0.10;
for
loop expressions w/ constant
When adding a class constant to a loop expression, it affects the constant that must be added in the expression.
Suppose we have the two loop expressions below for figure sizes of 5 and 9. The third line of the table shows the general formula that would be used if we turned our figure's size into a constant named SIZE
.
size | expression | relationship |
---|---|---|
5 |
8 * line + 16 |
16 = 3 * 5 + 1 |
9 |
8 * line + 28 |
28 = 3 * 9 + 1 |
SIZE |
8 * line + (3 * SIZE + 1) |
continued on the next slide ...
for
loop table w/ constant
You already found loop expressions for the slash figure at size 6. Now make a table at size 4 and use the two to generalize the loop expression in terms of a constant for the figure size.
!!!!!!!!!!!!!! \\!!!!!!!!!!// \\\\!!!!!!//// \\\\\\!!////// |
|
|
Add a class constant to your slash figure program so that it can be resized from its default of 6:
size 4 | size 7 |
---|---|
!!!!!!!!!!!!!! \\!!!!!!!!!!// \\\\!!!!!!//// \\\\\\!!////// |
!!!!!!!!!!!!!!!!!!!!!!!!!! \\!!!!!!!!!!!!!!!!!!!!!!// \\\\!!!!!!!!!!!!!!!!!!//// \\\\\\!!!!!!!!!!!!!!////// \\\\\\\\!!!!!!!!!!//////// \\\\\\\\\\!!!!!!////////// \\\\\\\\\\\\!!//////////// |
Test your code in the Output Comparison Tool.
1 2 3 4 5 6 7 8 9 10 |
public class Numbers { public static void main(String[] args) { int number = 42; for (int i = 1; i <= 1000; i++) { number = number * 37 % 103; } int number2 = number * number; System.out.println("result = " + number2); } } |
for
loop (line 5). Do this by moving your cursor to the beginning of that line until you see a
stop-sign icon
and then clicking.
continued on the next slide...
i
and number
.
number
has when i
has the given value.
Keep in mind that you are figuring out what value number
has just before it executes this line of code.
i = 1, number = |
42 |
|
i = 2, number = |
9 |
|
i = 3, number = |
24 |
|
i = 4, number = |
64 |
continued on the next slide...
number
after the loop is done executing.
number
?
69
A variable's scope is the part of a program in which it exists. In Java, the scope of a variable starts when it is declared and ends when the closing curly brace for the block that contains it is reached. A variable is said to be in scope where it is accessible.
public class Example { public static void main(String[] args) { performTest(); } public static void performTest() { int count = 12; for (int i = 1; i <= 12; i++) { runSample(); System.out.print(count); } } public static void runSample() { System.out.print("sample"); } } |
In which of these two blocks is the variable count in scope?
|