For Loops
1public class ForLoops {
2 public static void main(String[] args) {
3 // Loop from 0 to 9, incrementing by 1 each time
4 for(int i = 0; i < 10; i++) {
5 System.out.println(i);
6 }
7
8 // You can increment by more than 1
9 for(int i = 0; i < 10; i += 2) {
10 System.out.println(i);
11 }
12
13 // You can decrement too
14 for(int i = 10; i > 0; i--) {
15 System.out.println(i);
16 }
17
18 // You can decrement by more than 1
19 for(int i = 10; i > 0; i -= 2) {
20 System.out.println(i);
21 }
22
23 // Your increment can be a multiplier
24 for(int i = 1; i < 257; i *= 2) {
25 System.out.println(i);
26 }
27
28 // Or your increment can be a divisor
29 for(int i = 1024; i > 0; i /= 2) {
30 System.out.println(i);
31 }
32 }
33}
Explanation
In this exercise, we introduce
for
loops, structures used to repeat code a certain number of times.For
loops consist of several parts, in the form:1for(INITIALISATION; CONDITION; INCREMENT) { 2 BODY 3}
Loops in general are very important to programmers as they allow us to do complicated, long-winded calculations in a short amount of time and using fewer lines of code.
For
loops are mostly used for counting up or down in steps, usually in steps of 1. There are 3 parts to anyfor
loop: initialisation, condition and increment.Remember
++
means add1
, and--
means subtract1
. Similarly remember+= n
means addn
and-= n
means subtractn
. If we wanted to, we could writei = i + 1
rather thani++
, but that would be more tedious.The
BODY
is the code that is going to be repeated. TheINITIALISATION
where you declare the variable that will be increased or decreased; in most cases programmers usei
and it is declared as an integer. TheCONDITION
is what must remain true at the end of each iteration of theBODY
for thefor
loop to continue. TheINCREMENT
is what happens after each iteration of theBODY
of thefor
loop.So, for the first for loop,
i
is initialised to0
and the conditioni < 10
is checked. Since that is true, the body is run. Then the increment runs, which increasesi
by 1, and the condition is checked. 1 is still less than 10, so the body is run again. This continues untili
is incremented to10
, after which the loop stops. Therefore the loop goes from0
(inclusive) to9
(inclusive).The increment doesn't have to just be increasing
i
by 1, as is shown in the code above. In the thirdfor
loop,i
starts at 10 and then decreases until it is 0. In the fifthfor
loop,i
is multiplied by 2 every time the code runs, until it is greater than 256.
Exercises
Write a program,
Ex6B
, that uses afor
loop to produce the following sequence:1 -2 4 -8 16 -32
, stopping once it gets to 2000.Write a program,
Ex6C
, that uses afor
loop to produce the following sequence:1 3 6 10 15 21 28 36
. Hint: Use a counter to store the last number you've printed out, and think about what number to start at.Write a program,
Ex6D
, that prints out all the multiples of 9 between 0 and 108. Hint:x
is a multiple of 9 ifx % 9 == 0
.Write a program,
Ex6E
, that prints out the sum of all the multiples of 9 between 0 and 108. Hint: You will need to use+=
.