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
Copy file name to clipboardExpand all lines: src/Java-Fundamentals/course/Loops.md
+51-2Lines changed: 51 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ while (condition) { // the condition that is evaluated to true or false
14
14
15
15
// Last line of the loop body is the one right before the }
16
16
}
17
+
//after condition is evaluated to false the code jumps down to here and continues
17
18
```
18
19
Let's go over how this loop will be executed:
19
20
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'
42
43
1.```java
43
44
int i =0;
44
45
```
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)
46
47
2.```java
47
48
i <10;
48
49
```
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.
50
51
3.```java
51
52
i++;
52
53
```
@@ -70,6 +71,54 @@ while (i < 10) { // the condition that is evaluated
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 =newString[] {"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 =newint[] {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.
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
+
73
122
## Resources
74
123
75
124
[W3Schools While Loop](https://www.w3schools.com/java/java_while_loop.asp)\
0 commit comments