Professional Documents
Culture Documents
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
1. Initializing a counter variable.
2. Defining the looping condition.
3. Incrementing the counter.
1. i = 0: i is initialized to 0.
2. i < 5: the loop is given a condition that relies on the value of i.
3. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
4. i = 0: i is initialized to 0.
5. i < 5: the loop is given a condition that relies on the value of i.
6. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
7. i = 0: i is initialized to 0.
8. i < 5: the loop is given a condition that relies on the value of i.
9. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
10. i = 0: i is initialized to 0.
11. i < 5: the loop is given a condition that relies on the value of i.
12. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
13. i = 0: i is initialized to 0.
14. i < 5: the loop is given a condition that relies on the value of i.
15. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
16. i = 0: i is initialized to 0.
17. i < 5: the loop is given a condition that relies on the value of i.
18. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
19. i = 0: i is initialized to 0.
20. i < 5: the loop is given a condition that relies on the value of i.
21. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
22. i = 0: i is initialized to 0.
23. i < 5: the loop is given a condition that relies on the value of i.
24. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
Writing the same code over and over is time-consuming.
Having less code means having less to debug.
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
25. i = 0: i is initialized to 0.
26. i < 5: the loop is given a condition that relies on the value of i.
27. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
}
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
LEARN JAVA: LOOPS
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
28. i = 0: i is initialized to 0.
29. i < 5: the loop is given a condition that relies on the value of i.
30. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
// is condition still true?
// if so, repeat code block
}
// exit when condition is not true
while loops are extremely useful when you want to run some code until a specific
change happens. However, if you aren’t certain that change will occur, beware the
infinite loop!
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
31. i = 0: i is initialized to 0.
32. i < 5: the loop is given a condition that relies on the value of i.
33. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
34. i = 0: i is initialized to 0.
35. i < 5: the loop is given a condition that relies on the value of i.
36. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
37. i = 0: i is initialized to 0.
38. i < 5: the loop is given a condition that relies on the value of i.
39. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
40. i = 0: i is initialized to 0.
41. i < 5: the loop is given a condition that relies on the value of i.
42. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
43. i = 0: i is initialized to 0.
44. i < 5: the loop is given a condition that relies on the value of i.
45. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
46. i = 0: i is initialized to 0.
47. i < 5: the loop is given a condition that relies on the value of i.
48. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
49. i = 0: i is initialized to 0.
50. i < 5: the loop is given a condition that relies on the value of i.
51. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
52. i = 0: i is initialized to 0.
53. i < 5: the loop is given a condition that relies on the value of i.
54. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
55. i = 0: i is initialized to 0.
56. i < 5: the loop is given a condition that relies on the value of i.
57. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
58. i = 0: i is initialized to 0.
59. i < 5: the loop is given a condition that relies on the value of i.
60. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
61. i = 0: i is initialized to 0.
62. i < 5: the loop is given a condition that relies on the value of i.
63. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
LEARN JAVA: LOOPS
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
64. i = 0: i is initialized to 0.
65. i < 5: the loop is given a condition that relies on the value of i.
66. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
67. i = 0: i is initialized to 0.
68. i < 5: the loop is given a condition that relies on the value of i.
69. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
70. i = 0: i is initialized to 0.
71. i < 5: the loop is given a condition that relies on the value of i.
72. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
73. i = 0: i is initialized to 0.
74. i < 5: the loop is given a condition that relies on the value of i.
75. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
76. i = 0: i is initialized to 0.
77. i < 5: the loop is given a condition that relies on the value of i.
78. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
79. i = 0: i is initialized to 0.
80. i < 5: the loop is given a condition that relies on the value of i.
81. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
82. i = 0: i is initialized to 0.
83. i < 5: the loop is given a condition that relies on the value of i.
84. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
85. i = 0: i is initialized to 0.
86. i < 5: the loop is given a condition that relies on the value of i.
87. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
88. i = 0: i is initialized to 0.
89. i < 5: the loop is given a condition that relies on the value of i.
90. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
91. i = 0: i is initialized to 0.
92. i < 5: the loop is given a condition that relies on the value of i.
93. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
94. i = 0: i is initialized to 0.
95. i < 5: the loop is given a condition that relies on the value of i.
96. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
97. i = 0: i is initialized to 0.
98. i < 5: the loop is given a condition that relies on the value of i.
99. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
100. i = 0: i is initialized to 0.
101. i < 5: the loop is given a condition that relies on the value of i.
102. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
103. i = 0: i is initialized to 0.
104. i < 5: the loop is given a condition that relies on the value of i.
105. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
LEARN JAVA: LOOPS
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
106. i = 0: i is initialized to 0.
107. i < 5: the loop is given a condition that relies on the value of i.
108. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
109. i = 0: i is initialized to 0.
110. i < 5: the loop is given a condition that relies on the value of i.
111. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
112. i = 0: i is initialized to 0.
113. i < 5: the loop is given a condition that relies on the value of i.
114. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
115. i = 0: i is initialized to 0.
116. i < 5: the loop is given a condition that relies on the value of i.
117. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
118. i = 0: i is initialized to 0.
119. i < 5: the loop is given a condition that relies on the value of i.
120. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
121. i = 0: i is initialized to 0.
122. i < 5: the loop is given a condition that relies on the value of i.
123. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
124. i = 0: i is initialized to 0.
125. i < 5: the loop is given a condition that relies on the value of i.
126. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
We’ll go over three types of loops that you’ll see everywhere:
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
127. i = 0: i is initialized to 0.
128. i < 5: the loop is given a condition that relies on the value of i.
129. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
130. Initializing a counter variable.
131. Defining the looping condition.
132. Incrementing the counter.
130. i = 0: i is initialized to 0.
131. i < 5: the loop is given a condition that relies on the value of i.
132. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
133. i = 0: i is initialized to 0.
134. i < 5: the loop is given a condition that relies on the value of i.
135. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
136. i = 0: i is initialized to 0.
137. i < 5: the loop is given a condition that relies on the value of i.
138. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
139. i = 0: i is initialized to 0.
140. i < 5: the loop is given a condition that relies on the value of i.
141. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
142. i = 0: i is initialized to 0.
143. i < 5: the loop is given a condition that relies on the value of i.
144. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
145. i = 0: i is initialized to 0.
146. i < 5: the loop is given a condition that relies on the value of i.
147. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
148. i = 0: i is initialized to 0.
149. i < 5: the loop is given a condition that relies on the value of i.
150. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
}
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
LEARN JAVA: LOOPS
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
151. i = 0: i is initialized to 0.
152. i < 5: the loop is given a condition that relies on the value of i.
153. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
// is condition still true?
// if so, repeat code block
}
// exit when condition is not true
while loops are extremely useful when you want to run some code until a specific
change happens. However, if you aren’t certain that change will occur, beware the
infinite loop!
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
154. i = 0: i is initialized to 0.
155. i < 5: the loop is given a condition that relies on the value of i.
156. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
157. i = 0: i is initialized to 0.
158. i < 5: the loop is given a condition that relies on the value of i.
159. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
160. i = 0: i is initialized to 0.
161. i < 5: the loop is given a condition that relies on the value of i.
162. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
163. i = 0: i is initialized to 0.
164. i < 5: the loop is given a condition that relies on the value of i.
165. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
166. i = 0: i is initialized to 0.
167. i < 5: the loop is given a condition that relies on the value of i.
168. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
LEARN JAVA: LOOPS
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
169. i = 0: i is initialized to 0.
170. i < 5: the loop is given a condition that relies on the value of i.
171. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
172. i = 0: i is initialized to 0.
173. i < 5: the loop is given a condition that relies on the value of i.
174. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
175. i = 0: i is initialized to 0.
176. i < 5: the loop is given a condition that relies on the value of i.
177. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
178. i = 0: i is initialized to 0.
179. i < 5: the loop is given a condition that relies on the value of i.
180. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
181. i = 0: i is initialized to 0.
182. i < 5: the loop is given a condition that relies on the value of i.
183. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
184. i = 0: i is initialized to 0.
185. i < 5: the loop is given a condition that relies on the value of i.
186. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
187. i = 0: i is initialized to 0.
188. i < 5: the loop is given a condition that relies on the value of i.
189. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
190. i = 0: i is initialized to 0.
191. i < 5: the loop is given a condition that relies on the value of i.
192. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
193. i = 0: i is initialized to 0.
194. i < 5: the loop is given a condition that relies on the value of i.
195. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
196. i = 0: i is initialized to 0.
197. i < 5: the loop is given a condition that relies on the value of i.
198. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
199. i = 0: i is initialized to 0.
200. i < 5: the loop is given a condition that relies on the value of i.
201. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
202. i = 0: i is initialized to 0.
203. i < 5: the loop is given a condition that relies on the value of i.
204. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
205. i = 0: i is initialized to 0.
206. i < 5: the loop is given a condition that relies on the value of i.
207. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
208. i = 0: i is initialized to 0.
209. i < 5: the loop is given a condition that relies on the value of i.
210. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
LEARN JAVA: LOOPS
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
211. i = 0: i is initialized to 0.
212. i < 5: the loop is given a condition that relies on the value of i.
213. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
214. i = 0: i is initialized to 0.
215. i < 5: the loop is given a condition that relies on the value of i.
216. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
217. i = 0: i is initialized to 0.
218. i < 5: the loop is given a condition that relies on the value of i.
219. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
220. i = 0: i is initialized to 0.
221. i < 5: the loop is given a condition that relies on the value of i.
222. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
223. i = 0: i is initialized to 0.
224. i < 5: the loop is given a condition that relies on the value of i.
225. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
226. i = 0: i is initialized to 0.
227. i < 5: the loop is given a condition that relies on the value of i.
228. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
229. i = 0: i is initialized to 0.
230. i < 5: the loop is given a condition that relies on the value of i.
231. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
232. i = 0: i is initialized to 0.
233. i < 5: the loop is given a condition that relies on the value of i.
234. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
235. i = 0: i is initialized to 0.
236. i < 5: the loop is given a condition that relies on the value of i.
237. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
238. i = 0: i is initialized to 0.
239. i < 5: the loop is given a condition that relies on the value of i.
240. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
241. i = 0: i is initialized to 0.
242. i < 5: the loop is given a condition that relies on the value of i.
243. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
244. i = 0: i is initialized to 0.
245. i < 5: the loop is given a condition that relies on the value of i.
246. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
247. i = 0: i is initialized to 0.
248. i < 5: the loop is given a condition that relies on the value of i.
249. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
250. i = 0: i is initialized to 0.
251. i < 5: the loop is given a condition that relies on the value of i.
252. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
253. i = 0: i is initialized to 0.
254. i < 5: the loop is given a condition that relies on the value of i.
255. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
Writing the same code over and over is time-consuming.
Having less code means having less to debug.
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
256. i = 0: i is initialized to 0.
257. i < 5: the loop is given a condition that relies on the value of i.
258. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
}
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
LEARN JAVA: LOOPS
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
259. i = 0: i is initialized to 0.
260. i < 5: the loop is given a condition that relies on the value of i.
261. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
// is condition still true?
// if so, repeat code block
}
// exit when condition is not true
while loops are extremely useful when you want to run some code until a specific
change happens. However, if you aren’t certain that change will occur, beware the
infinite loop!
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
262. i = 0: i is initialized to 0.
263. i < 5: the loop is given a condition that relies on the value of i.
264. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
265. i = 0: i is initialized to 0.
266. i < 5: the loop is given a condition that relies on the value of i.
267. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
268. i = 0: i is initialized to 0.
269. i < 5: the loop is given a condition that relies on the value of i.
270. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
271. i = 0: i is initialized to 0.
272. i < 5: the loop is given a condition that relies on the value of i.
273. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
We’ll go over three types of loops that you’ll see everywhere:
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
274. i = 0: i is initialized to 0.
275. i < 5: the loop is given a condition that relies on the value of i.
276. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
277. Initializing a counter variable.
278. Defining the looping condition.
279. Incrementing the counter.
277. i = 0: i is initialized to 0.
278. i < 5: the loop is given a condition that relies on the value of i.
279. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
280. i = 0: i is initialized to 0.
281. i < 5: the loop is given a condition that relies on the value of i.
282. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
283. i = 0: i is initialized to 0.
284. i < 5: the loop is given a condition that relies on the value of i.
285. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
286. i = 0: i is initialized to 0.
287. i < 5: the loop is given a condition that relies on the value of i.
288. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
289. i = 0: i is initialized to 0.
290. i < 5: the loop is given a condition that relies on the value of i.
291. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
292. i = 0: i is initialized to 0.
293. i < 5: the loop is given a condition that relies on the value of i.
294. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
295. Initializing a counter variable.
296. Defining the looping condition.
297. Incrementing the counter.
295. i = 0: i is initialized to 0.
296. i < 5: the loop is given a condition that relies on the value of i.
297. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
298. i = 0: i is initialized to 0.
299. i < 5: the loop is given a condition that relies on the value of i.
300. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
301. i = 0: i is initialized to 0.
302. i < 5: the loop is given a condition that relies on the value of i.
303. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
304. i = 0: i is initialized to 0.
305. i < 5: the loop is given a condition that relies on the value of i.
306. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
307. i = 0: i is initialized to 0.
308. i < 5: the loop is given a condition that relies on the value of i.
309. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
310. i = 0: i is initialized to 0.
311. i < 5: the loop is given a condition that relies on the value of i.
312. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
313. i = 0: i is initialized to 0.
314. i < 5: the loop is given a condition that relies on the value of i.
315. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
316. i = 0: i is initialized to 0.
317. i < 5: the loop is given a condition that relies on the value of i.
318. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
319. i = 0: i is initialized to 0.
320. i < 5: the loop is given a condition that relies on the value of i.
321. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
322. i = 0: i is initialized to 0.
323. i < 5: the loop is given a condition that relies on the value of i.
324. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
325. i = 0: i is initialized to 0.
326. i < 5: the loop is given a condition that relies on the value of i.
327. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
328. i = 0: i is initialized to 0.
329. i < 5: the loop is given a condition that relies on the value of i.
330. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
331. i = 0: i is initialized to 0.
332. i < 5: the loop is given a condition that relies on the value of i.
333. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
334. i = 0: i is initialized to 0.
335. i < 5: the loop is given a condition that relies on the value of i.
336. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
337. Initializing a counter variable.
338. Defining the looping condition.
339. Incrementing the counter.
337. i = 0: i is initialized to 0.
338. i < 5: the loop is given a condition that relies on the value of i.
339. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
340. i = 0: i is initialized to 0.
341. i < 5: the loop is given a condition that relies on the value of i.
342. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
343. i = 0: i is initialized to 0.
344. i < 5: the loop is given a condition that relies on the value of i.
345. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
346. i = 0: i is initialized to 0.
347. i < 5: the loop is given a condition that relies on the value of i.
348. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
349. i = 0: i is initialized to 0.
350. i < 5: the loop is given a condition that relies on the value of i.
351. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
352. i = 0: i is initialized to 0.
353. i < 5: the loop is given a condition that relies on the value of i.
354. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
355. i = 0: i is initialized to 0.
356. i < 5: the loop is given a condition that relies on the value of i.
357. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
358. i = 0: i is initialized to 0.
359. i < 5: the loop is given a condition that relies on the value of i.
360. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
361. i = 0: i is initialized to 0.
362. i < 5: the loop is given a condition that relies on the value of i.
363. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
364. i = 0: i is initialized to 0.
365. i < 5: the loop is given a condition that relies on the value of i.
366. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
367. i = 0: i is initialized to 0.
368. i < 5: the loop is given a condition that relies on the value of i.
369. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
370. i = 0: i is initialized to 0.
371. i < 5: the loop is given a condition that relies on the value of i.
372. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
373. i = 0: i is initialized to 0.
374. i < 5: the loop is given a condition that relies on the value of i.
375. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
376. i = 0: i is initialized to 0.
377. i < 5: the loop is given a condition that relies on the value of i.
378. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
379. i = 0: i is initialized to 0.
380. i < 5: the loop is given a condition that relies on the value of i.
381. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
382. i = 0: i is initialized to 0.
383. i < 5: the loop is given a condition that relies on the value of i.
384. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
385. i = 0: i is initialized to 0.
386. i < 5: the loop is given a condition that relies on the value of i.
387. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
388. i = 0: i is initialized to 0.
389. i < 5: the loop is given a condition that relies on the value of i.
390. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
391. i = 0: i is initialized to 0.
392. i < 5: the loop is given a condition that relies on the value of i.
393. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
394. i = 0: i is initialized to 0.
395. i < 5: the loop is given a condition that relies on the value of i.
396. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
397. i = 0: i is initialized to 0.
398. i < 5: the loop is given a condition that relies on the value of i.
399. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
We’ll go over three types of loops that you’ll see everywhere:
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
400. i = 0: i is initialized to 0.
401. i < 5: the loop is given a condition that relies on the value of i.
402. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
403. Initializing a counter variable.
404. Defining the looping condition.
405. Incrementing the counter.
403. i = 0: i is initialized to 0.
404. i < 5: the loop is given a condition that relies on the value of i.
405. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
406. i = 0: i is initialized to 0.
407. i < 5: the loop is given a condition that relies on the value of i.
408. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
409. i = 0: i is initialized to 0.
410. i < 5: the loop is given a condition that relies on the value of i.
411. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
412. i = 0: i is initialized to 0.
413. i < 5: the loop is given a condition that relies on the value of i.
414. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
415. i = 0: i is initialized to 0.
416. i < 5: the loop is given a condition that relies on the value of i.
417. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
418. i = 0: i is initialized to 0.
419. i < 5: the loop is given a condition that relies on the value of i.
420. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
421. Initializing a counter variable.
422. Defining the looping condition.
423. Incrementing the counter.
421. i = 0: i is initialized to 0.
422. i < 5: the loop is given a condition that relies on the value of i.
423. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
424. i = 0: i is initialized to 0.
425. i < 5: the loop is given a condition that relies on the value of i.
426. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
427. i = 0: i is initialized to 0.
428. i < 5: the loop is given a condition that relies on the value of i.
429. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
430. i = 0: i is initialized to 0.
431. i < 5: the loop is given a condition that relies on the value of i.
432. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
433. i = 0: i is initialized to 0.
434. i < 5: the loop is given a condition that relies on the value of i.
435. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
436. i = 0: i is initialized to 0.
437. i < 5: the loop is given a condition that relies on the value of i.
438. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
439. i = 0: i is initialized to 0.
440. i < 5: the loop is given a condition that relies on the value of i.
441. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
442. i = 0: i is initialized to 0.
443. i < 5: the loop is given a condition that relies on the value of i.
444. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
445. i = 0: i is initialized to 0.
446. i < 5: the loop is given a condition that relies on the value of i.
447. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
448. i = 0: i is initialized to 0.
449. i < 5: the loop is given a condition that relies on the value of i.
450. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
451. i = 0: i is initialized to 0.
452. i < 5: the loop is given a condition that relies on the value of i.
453. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
454. i = 0: i is initialized to 0.
455. i < 5: the loop is given a condition that relies on the value of i.
456. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
457. i = 0: i is initialized to 0.
458. i < 5: the loop is given a condition that relies on the value of i.
459. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
460. i = 0: i is initialized to 0.
461. i < 5: the loop is given a condition that relies on the value of i.
462. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
463. Initializing a counter variable.
464. Defining the looping condition.
465. Incrementing the counter.
463. i = 0: i is initialized to 0.
464. i < 5: the loop is given a condition that relies on the value of i.
465. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
466. i = 0: i is initialized to 0.
467. i < 5: the loop is given a condition that relies on the value of i.
468. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
469. i = 0: i is initialized to 0.
470. i < 5: the loop is given a condition that relies on the value of i.
471. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
472. i = 0: i is initialized to 0.
473. i < 5: the loop is given a condition that relies on the value of i.
474. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
475. i = 0: i is initialized to 0.
476. i < 5: the loop is given a condition that relies on the value of i.
477. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
478. i = 0: i is initialized to 0.
479. i < 5: the loop is given a condition that relies on the value of i.
480. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
481. i = 0: i is initialized to 0.
482. i < 5: the loop is given a condition that relies on the value of i.
483. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
484. i = 0: i is initialized to 0.
485. i < 5: the loop is given a condition that relies on the value of i.
486. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
487. i = 0: i is initialized to 0.
488. i < 5: the loop is given a condition that relies on the value of i.
489. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
490. i = 0: i is initialized to 0.
491. i < 5: the loop is given a condition that relies on the value of i.
492. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
493. i = 0: i is initialized to 0.
494. i < 5: the loop is given a condition that relies on the value of i.
495. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
496. i = 0: i is initialized to 0.
497. i < 5: the loop is given a condition that relies on the value of i.
498. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
499. i = 0: i is initialized to 0.
500. i < 5: the loop is given a condition that relies on the value of i.
501. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
502. i = 0: i is initialized to 0.
503. i < 5: the loop is given a condition that relies on the value of i.
504. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
505. i = 0: i is initialized to 0.
506. i < 5: the loop is given a condition that relies on the value of i.
507. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
508. i = 0: i is initialized to 0.
509. i < 5: the loop is given a condition that relies on the value of i.
510. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
511. i = 0: i is initialized to 0.
512. i < 5: the loop is given a condition that relies on the value of i.
513. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
514. i = 0: i is initialized to 0.
515. i < 5: the loop is given a condition that relies on the value of i.
516. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
517. i = 0: i is initialized to 0.
518. i < 5: the loop is given a condition that relies on the value of i.
519. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
}
520. i = 0: i is initialized to 0.
521. i < 5: the loop is given a condition that relies on the value of i.
522. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
for (String inventoryItem : inventoryItems) {
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
523. i = 0: i is initialized to 0.
524. i < 5: the loop is given a condition that relies on the value of i.
525. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
We’ll go over three types of loops that you’ll see everywhere:
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
526. i = 0: i is initialized to 0.
527. i < 5: the loop is given a condition that relies on the value of i.
528. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
529. Initializing a counter variable.
530. Defining the looping condition.
531. Incrementing the counter.
529. i = 0: i is initialized to 0.
530. i < 5: the loop is given a condition that relies on the value of i.
531. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
532. i = 0: i is initialized to 0.
533. i < 5: the loop is given a condition that relies on the value of i.
534. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
535. i = 0: i is initialized to 0.
536. i < 5: the loop is given a condition that relies on the value of i.
537. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
538. i = 0: i is initialized to 0.
539. i < 5: the loop is given a condition that relies on the value of i.
540. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
541. i = 0: i is initialized to 0.
542. i < 5: the loop is given a condition that relies on the value of i.
543. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
544. i = 0: i is initialized to 0.
545. i < 5: the loop is given a condition that relies on the value of i.
546. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
547. Initializing a counter variable.
548. Defining the looping condition.
549. Incrementing the counter.
547. i = 0: i is initialized to 0.
548. i < 5: the loop is given a condition that relies on the value of i.
549. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
550. i = 0: i is initialized to 0.
551. i < 5: the loop is given a condition that relies on the value of i.
552. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
553. i = 0: i is initialized to 0.
554. i < 5: the loop is given a condition that relies on the value of i.
555. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
556. i = 0: i is initialized to 0.
557. i < 5: the loop is given a condition that relies on the value of i.
558. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
559. i = 0: i is initialized to 0.
560. i < 5: the loop is given a condition that relies on the value of i.
561. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
562. i = 0: i is initialized to 0.
563. i < 5: the loop is given a condition that relies on the value of i.
564. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
565. i = 0: i is initialized to 0.
566. i < 5: the loop is given a condition that relies on the value of i.
567. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
568. i = 0: i is initialized to 0.
569. i < 5: the loop is given a condition that relies on the value of i.
570. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
571. i = 0: i is initialized to 0.
572. i < 5: the loop is given a condition that relies on the value of i.
573. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
574. i = 0: i is initialized to 0.
575. i < 5: the loop is given a condition that relies on the value of i.
576. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
577. i = 0: i is initialized to 0.
578. i < 5: the loop is given a condition that relies on the value of i.
579. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
580. i = 0: i is initialized to 0.
581. i < 5: the loop is given a condition that relies on the value of i.
582. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
583. i = 0: i is initialized to 0.
584. i < 5: the loop is given a condition that relies on the value of i.
585. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
586. i = 0: i is initialized to 0.
587. i < 5: the loop is given a condition that relies on the value of i.
588. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
}
LEARN JAVA: LOOPS
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
589. Initializing a counter variable.
590. Defining the looping condition.
591. Incrementing the counter.
589. i = 0: i is initialized to 0.
590. i < 5: the loop is given a condition that relies on the value of i.
591. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
LEARN JAVA: LOOPS
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
592. i = 0: i is initialized to 0.
593. i < 5: the loop is given a condition that relies on the value of i.
594. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
595. i = 0: i is initialized to 0.
596. i < 5: the loop is given a condition that relies on the value of i.
597. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
598. i = 0: i is initialized to 0.
599. i < 5: the loop is given a condition that relies on the value of i.
600. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
601. i = 0: i is initialized to 0.
602. i < 5: the loop is given a condition that relies on the value of i.
603. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
604. i = 0: i is initialized to 0.
605. i < 5: the loop is given a condition that relies on the value of i.
606. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
607. i = 0: i is initialized to 0.
608. i < 5: the loop is given a condition that relies on the value of i.
609. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
610. i = 0: i is initialized to 0.
611. i < 5: the loop is given a condition that relies on the value of i.
612. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
613. i = 0: i is initialized to 0.
614. i < 5: the loop is given a condition that relies on the value of i.
615. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
616. i = 0: i is initialized to 0.
617. i < 5: the loop is given a condition that relies on the value of i.
618. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
619. i = 0: i is initialized to 0.
620. i < 5: the loop is given a condition that relies on the value of i.
621. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
622. i = 0: i is initialized to 0.
623. i < 5: the loop is given a condition that relies on the value of i.
624. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
// conditional logic uses counter
while (wishes < 3) {
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
625. i = 0: i is initialized to 0.
626. i < 5: the loop is given a condition that relies on the value of i.
627. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
}
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example:
Introduction to Loops
In the programming world, we hate repeating ourselves. There are two reasons for this:
But we often need to do the same task more than once. Fortunately, computers are
really good (and fast) at doing repetitive tasks. And in Java, we can use loops.
A loop is a programming tool that allows developers to repeat the same block of code
until some condition is met. We employ loops to easily scale programs, saving time and
minimizing mistakes.
while loops
for loops
for-each loops
// code to run
}
Like an if statement, the code inside a while loop will only run if the condition is true.
However, a while loop will continue running the code over and over until the condition
evaluates to false. So the code block will repeat until silliness is less than or equal to
10.
// set attempts to 0
int attempts = 0;
System.out.println("Try again.");
passcode = getNewPasscode();
attempts += 1;
Infinite loops occur when the condition will never evaluate to false. This can cause your
entire program to crash.
int hedgehogs = 5;
}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
// counter is initialized
int wishes = 0;
System.out.println("Wish granted.");
// counter is incremented
wishes++;
}
In the above example, the counter wishes gets initialized before the loop with a value
of 0, then the program will keep printing "Wish granted." and adding 1 to wishes as long
as wishes has a value of less than 3. Once wishes reaches a value of 3 or more, the
program will exit the loop.
Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;
doPushup();
pushupsToDo--;
}
In the code above, the counter, pushupsToDo, starts at 10, and increments down one at a
time. When it hits 0, the loop exits.
For Loops
Incrementing with loops is actually so common in programming that Java (like many
other programming languages) includes syntax specifically to address this
pattern: for loops. A for loop brings together the following steps in a single line,
separated by semicolons:
628. i = 0: i is initialized to 0.
629. i < 5: the loop is given a condition that relies on the value of i.
630. i++: i will increment at the end of each loop.
So the code will run through the loop a total of five times.
You’ll also hear the term “iteration” in reference to loops. When you iterate, it just means
that you are repeating the same block of code.
LEARN JAVA: LOOPS
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.
forloops begin with a counter variable. We can use that counter to track the
index of the current element as we iterate over the list of data.
Because the first index in an array or ArrayList is 0, the counter would begin
with a value of 0 and increment until the the end of the list. So we can
increment through the array or ArrayList using its indices.
secretCode[i] += 1;
}
Notice that our condition in this example is i < secretCode.length. Because array
indices start at 0, the length of secretCode is 1 larger than its final index.
Therefore, the loop should stop when it is less than BUT NOT equal to the
length value.
For-Each Loops
Sometimes we need access to the elements’ indices or we only want to iterate through a
portion of a list. If that’s the case, a regular for loop is a great choice. But sometimes we
couldn’t care less about the indices; we only care about the element itself. At times like
these, for-each loops come in handy.
For-each loops allow you to directly loop through each item in a list of items (like an
array or ArrayList) and perform some action with each item. The syntax looks like this:
System.out.println(inventoryItem);
}
We can read the : as “in” like this: for each inventoryItem (which should be a String)
in inventoryItems, print inventoryItem.
Note that we can name the individual item whatever we want; using the singular of a
plural is just a convention. You may also encounter conventions like String word :
sentence.
LEARN JAVA: LOOPS
Review
Nice work! Let’s iterate over what you’ve just learned about loops:
while loops: These are useful to repeat a code block an unknown number of
times until some condition is met. For example:
int wishes = 0;
forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:
For-each loops: These make it simple to do something with each item in a list. For
example: