Example
int i = 0: declare a counter variable and assign an initial value to it;
i < 100: define the condition to continue with the for loop; as long as this condition is true the loop will run, when it is not true, we stop and exit from the for loop;
i++: increment the counter value; i++ is the same thing as i = i + 1.
So, how many times will the “<do something>” be repeated in the code above?
Lab Work
- 1.
Use the for loop to output “Hello!” 10 times.
- 2.
Use the for loop to print out all integers from 1 to 25, inclusive.
- 3.
Print out all integers from 1 to 25.
- 4.
Output all even numbers from 3 to 99.
The for Loop Formula
firstTerm + difference × (n – 1)
Finding the “for Loop” Formula for an Arithmetic Sequence
-4, 5, 14, 23, 32, 41, 50, 59, 68, 77, 86.
It follows an arithmetic sequence.
firstItem = -4
lastItem = 86
difference = 5 – (-4) = 9
Translate this to a for loop:
It will iterate through every single number in the list.
Math: Counting Strategically
You may finger count, but that will not work when you have an extremely large amount of numbers in the series. The right approach is to prepare these numbers by reorganizing them. The purpose is to find a good pattern so that we can count systematically.
Finding a pattern here is to figure out a basic formula representing every number in the series.
100 – 3 + 1 = 98.
A common method is to convert the number series to something more straightforward. If we subtract 2 from every number in the series, we get 1, 2, 3, 4, ......, 98. We now know the count is 98. And, the formula representing every number will be x(i) = i + 2, (i=1, 2, ......,98).
What about 5, 8, 11, 14, ......, 101? How do you use the “for loop” to print it out?
- 1.
Subtract 5 from every number; it becomes 0, 3, 6, 9, ......, 96
- 2.
Divided by 3, it then becomes 0, 1, 2, 3, ......, 32
- 3.
It is not hard to count from 0, one by one up to 32. The total count is 33.
- 4.
The general term for the i-th number in the series will be x(i) = 3 * i + 5, (i=0, 1, 2, ......, 32).
Lab Work
Write a for loop to produce the following list of numbers:
1 4 9 16 25 36 49 64 81 100
(Hint: watch for a common pattern.)
Example
What is the output of the following sequence of loops?
The external for loop (marked as “1”) has two iterations, so the output will have two lines, by println().
The middle for loop (marked as “2”) has three iterations, so it will print out 2 x 3 = 6 “!” in total by print().
The internal for loop (marked as “3”) has four iterations, so it will print out 2 x 3 x 4 = 24 “*” in total by print().
Lab Work
Write a method exp() to compute an exponential result, given the input of a base number and a power (a.k.a. an exponent number). For example, exp(3, 4) returns 81. The restriction is that the base and exponent numbers are non-negative.
Problems
- 1.What is the output of the following sequence of loops?for (int i = 1; i <= 2; i++) {for (int j = 1; j <= 3; j++) {System.out.print(i + ""*"" + j + ""= "" + i * j + ""; ");}System.out.println();}
- 2.
Write a for loop to produce the following list of numbers:
5 10 17 26 37 50
- 3.
Write a for loop to produce the following list of numbers:
1 8 27 64 125
- 4.
Write a for loop to produce the following list of numbers:
-1 0 7 26 63 124
- 5.Use for loops to produce the following output:********************
- 6.
Write for loop code to output the following:
1
22
333
4444