Spaces:
Running
Running
Create 14a. for loops
Browse files
Week 1: Types, condition clauses and loops/14a. for loops
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
for (<initialization>; <condition>; <change>) {
|
| 2 |
+
|
| 3 |
+
<block>
|
| 4 |
+
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
==================================================
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
public class Example {
|
| 13 |
+
public static void main(String[] args) {
|
| 14 |
+
for (int i=1; i<=10; i++) {
|
| 15 |
+
System.out.println(i);
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
Output:
|
| 21 |
+
1
|
| 22 |
+
2
|
| 23 |
+
3
|
| 24 |
+
4
|
| 25 |
+
5
|
| 26 |
+
6
|
| 27 |
+
7
|
| 28 |
+
8
|
| 29 |
+
9
|
| 30 |
+
10
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
==================================================
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
Components of the loop are all optional - so you can omit the initialization, condition clause or modification (or even all of them).
|
| 39 |
+
However, semicolons are mandatory.
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
public static void main(String[] args) {
|
| 43 |
+
// variable initialized outside for-loop
|
| 44 |
+
int i = 1;
|
| 45 |
+
for (; i<=10; i++) {
|
| 46 |
+
System.out.println(i);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// making the variable modification inside the loop
|
| 50 |
+
for (int j = 5; j > 1;) {
|
| 51 |
+
System.out.println(j);
|
| 52 |
+
j--;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// eternal loop
|
| 56 |
+
for(;;) {
|
| 57 |
+
// this is printed until the user
|
| 58 |
+
// closes the program: in practice the same as while(true)
|
| 59 |
+
System.out.println("Hi!");
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
==================================================
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
A change in the value of a variable in a for statement is usually done with the ++ and -- operators.
|
| 73 |
+
These operators increase or decrease the value of a variable by one, for example:
|
| 74 |
+
|
| 75 |
+
public class Example {
|
| 76 |
+
public static void main(String[] args) {
|
| 77 |
+
int a = 3;
|
| 78 |
+
a++;
|
| 79 |
+
System.out.println(a);
|
| 80 |
+
|
| 81 |
+
a--;
|
| 82 |
+
a--;
|
| 83 |
+
System.out.println(a);
|
| 84 |
+
|
| 85 |
+
// This first prints the value and then
|
| 86 |
+
// after that increases it:
|
| 87 |
+
System.out.println(a++);
|
| 88 |
+
|
| 89 |
+
// one more print
|
| 90 |
+
System.out.println(a);
|
| 91 |
+
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
Output:
|
| 96 |
+
4
|
| 97 |
+
2
|
| 98 |
+
2
|
| 99 |
+
3
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|