Skip to content

Commit 81c0805

Browse files
committed
Added for each loops
1 parent 57679e3 commit 81c0805

1 file changed

Lines changed: 51 additions & 2 deletions

File tree

src/Java-Fundamentals/course/Loops.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ while (condition) { // the condition that is evaluated to true or false
1414

1515
// Last line of the loop body is the one right before the }
1616
}
17+
//after condition is evaluated to false the code jumps down to here and continues
1718
```
1819
Let's go over how this loop will be executed:
1920
1. The condition is evaluated. If it evaluates to `false`, the code is skipped.
@@ -42,11 +43,11 @@ The line containing the `for` statement itself contains several statements, let'
4243
1. ```java
4344
int i = 0;
4445
```
45-
- This is run before the first iteration of the loop and sets the variable that the rest of the loop uses. This variable is usually named after a single letter such as `i` or `j`.
46+
- This is run before we start the loop and sets the variable that the rest of the loop uses. This variable is sometimes named after a single letter such as `i` or `j`. This is named after [index](./Arrays.md)
4647
2. ```java
4748
i < 10;
4849
```
49-
- A condition utilizing [equality operators](./Boolean-And-Equality-Operators.md#equality-operators). If this condition is true, the loop runs again, otherwise the program moves on. This is identical to the while loop's condition that we saw above.
50+
- The loop's condition that it checks after each run-through. This uses [equality operators](./Boolean-And-Equality-Operators.md#equality-operators). If this condition is true, the loop runs again, otherwise the program moves on. This is identical to the while loop's condition that we saw above.
5051
3. ```java
5152
i++;
5253
```
@@ -70,6 +71,54 @@ while (i < 10) { // the condition that is evaluated
7071
```
7172
______________________________________________________________________
7273

74+
## For each loops
75+
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) or String.
77+
78+
This may be confusing but the format is:
79+
```java
80+
for (theDataType name: nameOfArrayOrSomething) {
81+
[...]
82+
}
83+
```
84+
85+
This splits your array into individual data entries that can be referred to by a name in the loop.
86+
87+
```java
88+
String[] appleTypes = new String[] {"Pink Lady", "Red Delicious", "Granny Smith", "umm I'm out of apple names"};
89+
for (String theTypeOfApple: appleTypes) {
90+
[...]
91+
System.out.println(theTypeOfApple);
92+
} //prints out every item in the array
93+
```
94+
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+
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+
}
102+
```java
103+
int[] numbers = new int[] {3, 5, -7, 1}
104+
int total = 0
105+
for (int number: numbers) {
106+
total += number
107+
}
108+
```
109+
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.
110+
111+
______________________________________________________________________
112+
113+
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:
114+
```java
115+
for (int i = 0; i < arrayName.length; i++) {
116+
System.out.println("This is the index: " + i);
117+
System.out.println("And this is the value: " + arrayName[i]);
118+
}
119+
```
120+
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.
121+
73122
## Resources
74123

75124
[W3Schools While Loop](https://www.w3schools.com/java/java_while_loop.asp)\

0 commit comments

Comments
 (0)