You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The for each loop runs code for every part of a collection. It will run a loop that is the number of indexes in an [Array](./Arrays.md) or String.
76
+
The for each loop runs code for every part of a collection. It will run a loop that is the number of indexes in an [Array](./Arrays.md).
77
77
78
78
This may be confusing but the format is:
79
79
```java
@@ -86,31 +86,28 @@ This splits your array into individual data entries that can be referred to by a
86
86
87
87
```java
88
88
String[] appleTypes =newString[] {"Pink Lady", "Red Delicious", "Granny Smith", "umm I'm out of apple names"};
89
+
89
90
for (String theTypeOfApple: appleTypes) {
90
91
[...]
91
92
System.out.println(theTypeOfApple);
92
93
} //prints out every item in the array
93
94
```
94
95
Here we have an array of apple types. The code in for loop runs "for each" type of apple. In this code the type of apple we are currently on is referred to as `theTypeOfApple`. Each time this runs we are on a different entry in the array and the code is done on all of them. Each of these entries is of type `String` and we are calling them `theTypeOfApple` for the for loop. The array we are looping through is called `appleTypes`. That is why we have `String theTypeOfApple: appleTypes` because this runs through the everything in array with each individual String.
95
96
96
-
Here are some examples:
97
-
```java
98
-
String name ="Connor";
99
-
for (char letter: name) {
100
-
//here there can be code where the code runs each character that makes up the String "name". Every character that it runs through will be referred to as "letter" every time the loop goes around.
101
-
}
97
+
Here is an example:
102
98
```java
103
-
int[] numbers =newint[] {3, 5, -7, 1}
104
-
int total =0
99
+
int[] numbers =newint[] {3, 5, -7, 1};
100
+
int total =0;
101
+
105
102
for (int number: numbers) {
106
-
total += number
103
+
total += number;
107
104
}
108
105
```
109
106
This last example adds every integer in numbers together. Also since arrays are often a named a plural noun, using it's singular to refer to each entry makes sense. Here we are referring to each number that is a part of numbers.
The regular loop can also be used to go through an Array or String but still allowing us to use the index in the code:
110
+
The regular for loop can also be used to go through an Array but still allowing us to use the index in the code:
114
111
```java
115
112
for (int i =0; i < arrayName.length; i++) {
116
113
System.out.println("This is the index: "+ i);
@@ -119,6 +116,8 @@ for (int i = 0; i < arrayName.length; i++) {
119
116
```
120
117
This lets us use both the index and the value (through `arrayName[index]`) in each iteration of the loop. This too will go through every entry into the array.
0 commit comments