You are on page 1of 1365

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
1. Initializing a counter variable.
2. Defining the looping condition.
3. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;


while (pushupsToDo > 0) {

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:

4. Initializing a counter variable.


5. Defining the looping condition.
6. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

7. Initializing a counter variable.


8. Defining the looping condition.
9. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

10. Initializing a counter variable.


11. Defining the looping condition.
12. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

13. Initializing a counter variable.


14. Defining the looping condition.
15. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

16. Initializing a counter variable.


17. Defining the looping condition.
18. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

19. Initializing a counter variable.


20. Defining the looping condition.
21. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");


}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

22. Initializing a counter variable.


23. Defining the looping condition.
24. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS


Using For Loops
for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

25. Initializing a counter variable.


26. Defining the looping condition.
27. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {


// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?
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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

}
 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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. Initializing a counter variable.


29. Defining the looping condition.
30. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


forloops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:


for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

31. Initializing a counter variable.


32. Defining the looping condition.
33. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run


}

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

34. Initializing a counter variable.


35. Defining the looping condition.
36. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

37. Initializing a counter variable.


38. Defining the looping condition.
39. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

40. Initializing a counter variable.


41. Defining the looping condition.
42. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

43. Initializing a counter variable.


44. Defining the looping condition.
45. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {


// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

46. Initializing a counter variable.


47. Defining the looping condition.
48. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

49. Initializing a counter variable.


50. Defining the looping condition.
51. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

52. Initializing a counter variable.


53. Defining the looping condition.
54. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

55. Initializing a counter variable.


56. Defining the looping condition.
57. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

58. Initializing a counter variable.


59. Defining the looping condition.
60. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

61. Initializing a counter variable.


62. Defining the looping condition.
63. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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. Initializing a counter variable.


65. Defining the looping condition.
66. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


forloops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:


for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

67. Initializing a counter variable.


68. Defining the looping condition.
69. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

70. Initializing a counter variable.


71. Defining the looping condition.
72. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

73. Initializing a counter variable.


74. Defining the looping condition.
75. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

76. Initializing a counter variable.


77. Defining the looping condition.
78. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

79. Initializing a counter variable.


80. Defining the looping condition.
81. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

82. Initializing a counter variable.


83. Defining the looping condition.
84. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

85. Initializing a counter variable.


86. Defining the looping condition.
87. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {


// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

88. Initializing a counter variable.


89. Defining the looping condition.
90. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

91. Initializing a counter variable.


92. Defining the looping condition.
93. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

94. Initializing a counter variable.


95. Defining the looping condition.
96. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

97. Initializing a counter variable.


98. Defining the looping condition.
99. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

100. Initializing a counter variable.


101. Defining the looping condition.
102. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

103. Initializing a counter variable.


104. Defining the looping condition.
105. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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. Initializing a counter variable.


107. Defining the looping condition.
108. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


forloops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:


for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

109. Initializing a counter variable.


110. Defining the looping condition.
111. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

112. Initializing a counter variable.


113. Defining the looping condition.
114. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

115. Initializing a counter variable.


116. Defining the looping condition.
117. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

118. Initializing a counter variable.


119. Defining the looping condition.
120. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

121. Initializing a counter variable.


122. Defining the looping condition.
123. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

124. Initializing a counter variable.


125. Defining the looping condition.
126. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.
We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

127. Initializing a counter variable.


128. Defining the looping condition.
129. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {


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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
130. Initializing a counter variable.
131. Defining the looping condition.
132. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

133. Initializing a counter variable.


134. Defining the looping condition.
135. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

136. Initializing a counter variable.


137. Defining the looping condition.
138. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

139. Initializing a counter variable.


140. Defining the looping condition.
141. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

142. Initializing a counter variable.


143. Defining the looping condition.
144. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

145. Initializing a counter variable.


146. Defining the looping condition.
147. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {
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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

148. Initializing a counter variable.


149. Defining the looping condition.
150. Incrementing the counter.

The opening line might look like this:


for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?
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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

}
 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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. Initializing a counter variable.


152. Defining the looping condition.
153. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


forloops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:


for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

154. Initializing a counter variable.


155. Defining the looping condition.
156. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run


}

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

157. Initializing a counter variable.


158. Defining the looping condition.
159. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

160. Initializing a counter variable.


161. Defining the looping condition.
162. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

163. Initializing a counter variable.


164. Defining the looping condition.
165. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

166. Initializing a counter variable.


167. Defining the looping condition.
168. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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. Initializing a counter variable.


170. Defining the looping condition.
171. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


forloops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:


for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

172. Initializing a counter variable.


173. Defining the looping condition.
174. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

175. Initializing a counter variable.


176. Defining the looping condition.
177. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

178. Initializing a counter variable.


179. Defining the looping condition.
180. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

181. Initializing a counter variable.


182. Defining the looping condition.
183. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

184. Initializing a counter variable.


185. Defining the looping condition.
186. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

187. Initializing a counter variable.


188. Defining the looping condition.
189. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

190. Initializing a counter variable.


191. Defining the looping condition.
192. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {


// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

193. Initializing a counter variable.


194. Defining the looping condition.
195. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

196. Initializing a counter variable.


197. Defining the looping condition.
198. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

199. Initializing a counter variable.


200. Defining the looping condition.
201. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

202. Initializing a counter variable.


203. Defining the looping condition.
204. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

205. Initializing a counter variable.


206. Defining the looping condition.
207. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

208. Initializing a counter variable.


209. Defining the looping condition.
210. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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. Initializing a counter variable.


212. Defining the looping condition.
213. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


forloops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:


for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

214. Initializing a counter variable.


215. Defining the looping condition.
216. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

217. Initializing a counter variable.


218. Defining the looping condition.
219. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

220. Initializing a counter variable.


221. Defining the looping condition.
222. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

223. Initializing a counter variable.


224. Defining the looping condition.
225. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

226. Initializing a counter variable.


227. Defining the looping condition.
228. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

229. Initializing a counter variable.


230. Defining the looping condition.
231. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

232. Initializing a counter variable.


233. Defining the looping condition.
234. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {


// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

235. Initializing a counter variable.


236. Defining the looping condition.
237. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

238. Initializing a counter variable.


239. Defining the looping condition.
240. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

241. Initializing a counter variable.


242. Defining the looping condition.
243. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

244. Initializing a counter variable.


245. Defining the looping condition.
246. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

247. Initializing a counter variable.


248. Defining the looping condition.
249. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

250. Initializing a counter variable.


251. Defining the looping condition.
252. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");


}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

253. Initializing a counter variable.


254. Defining the looping condition.
255. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS


Using For Loops
for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

256. Initializing a counter variable.


257. Defining the looping condition.
258. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {


// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?
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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

}
 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.
LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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. Initializing a counter variable.


260. Defining the looping condition.
261. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


forloops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:


for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

262. Initializing a counter variable.


263. Defining the looping condition.
264. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run


}

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

265. Initializing a counter variable.


266. Defining the looping condition.
267. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

268. Initializing a counter variable.


269. Defining the looping condition.
270. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

271. Initializing a counter variable.


272. Defining the looping condition.
273. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.
We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

274. Initializing a counter variable.


275. Defining the looping condition.
276. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {


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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
277. Initializing a counter variable.
278. Defining the looping condition.
279. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

280. Initializing a counter variable.


281. Defining the looping condition.
282. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

283. Initializing a counter variable.


284. Defining the looping condition.
285. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

286. Initializing a counter variable.


287. Defining the looping condition.
288. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

289. Initializing a counter variable.


290. Defining the looping condition.
291. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

292. Initializing a counter variable.


293. Defining the looping condition.
294. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
295. Initializing a counter variable.
296. Defining the looping condition.
297. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;


while (pushupsToDo > 0) {

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:

298. Initializing a counter variable.


299. Defining the looping condition.
300. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

301. Initializing a counter variable.


302. Defining the looping condition.
303. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

304. Initializing a counter variable.


305. Defining the looping condition.
306. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

307. Initializing a counter variable.


308. Defining the looping condition.
309. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

310. Initializing a counter variable.


311. Defining the looping condition.
312. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

313. Initializing a counter variable.


314. Defining the looping condition.
315. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

316. Initializing a counter variable.


317. Defining the looping condition.
318. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

319. Initializing a counter variable.


320. Defining the looping condition.
321. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

322. Initializing a counter variable.


323. Defining the looping condition.
324. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

325. Initializing a counter variable.


326. Defining the looping condition.
327. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

328. Initializing a counter variable.


329. Defining the looping condition.
330. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

331. Initializing a counter variable.


332. Defining the looping condition.
333. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

334. Initializing a counter variable.


335. Defining the looping condition.
336. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
337. Initializing a counter variable.
338. Defining the looping condition.
339. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;


while (pushupsToDo > 0) {

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:

340. Initializing a counter variable.


341. Defining the looping condition.
342. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

343. Initializing a counter variable.


344. Defining the looping condition.
345. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

346. Initializing a counter variable.


347. Defining the looping condition.
348. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

349. Initializing a counter variable.


350. Defining the looping condition.
351. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

352. Initializing a counter variable.


353. Defining the looping condition.
354. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

355. Initializing a counter variable.


356. Defining the looping condition.
357. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

358. Initializing a counter variable.


359. Defining the looping condition.
360. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

361. Initializing a counter variable.


362. Defining the looping condition.
363. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

364. Initializing a counter variable.


365. Defining the looping condition.
366. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

367. Initializing a counter variable.


368. Defining the looping condition.
369. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

370. Initializing a counter variable.


371. Defining the looping condition.
372. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

373. Initializing a counter variable.


374. Defining the looping condition.
375. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

376. Initializing a counter variable.


377. Defining the looping condition.
378. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

379. Initializing a counter variable.


380. Defining the looping condition.
381. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

382. Initializing a counter variable.


383. Defining the looping condition.
384. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

385. Initializing a counter variable.


386. Defining the looping condition.
387. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

388. Initializing a counter variable.


389. Defining the looping condition.
390. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

391. Initializing a counter variable.


392. Defining the looping condition.
393. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

394. Initializing a counter variable.


395. Defining the looping condition.
396. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

397. Initializing a counter variable.


398. Defining the looping condition.
399. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.
We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

400. Initializing a counter variable.


401. Defining the looping condition.
402. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {


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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
403. Initializing a counter variable.
404. Defining the looping condition.
405. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

406. Initializing a counter variable.


407. Defining the looping condition.
408. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

409. Initializing a counter variable.


410. Defining the looping condition.
411. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

412. Initializing a counter variable.


413. Defining the looping condition.
414. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

415. Initializing a counter variable.


416. Defining the looping condition.
417. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

418. Initializing a counter variable.


419. Defining the looping condition.
420. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
421. Initializing a counter variable.
422. Defining the looping condition.
423. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;


while (pushupsToDo > 0) {

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:

424. Initializing a counter variable.


425. Defining the looping condition.
426. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

427. Initializing a counter variable.


428. Defining the looping condition.
429. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

430. Initializing a counter variable.


431. Defining the looping condition.
432. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

433. Initializing a counter variable.


434. Defining the looping condition.
435. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

436. Initializing a counter variable.


437. Defining the looping condition.
438. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

439. Initializing a counter variable.


440. Defining the looping condition.
441. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

442. Initializing a counter variable.


443. Defining the looping condition.
444. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

445. Initializing a counter variable.


446. Defining the looping condition.
447. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

448. Initializing a counter variable.


449. Defining the looping condition.
450. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

451. Initializing a counter variable.


452. Defining the looping condition.
453. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

454. Initializing a counter variable.


455. Defining the looping condition.
456. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

457. Initializing a counter variable.


458. Defining the looping condition.
459. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

460. Initializing a counter variable.


461. Defining the looping condition.
462. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
463. Initializing a counter variable.
464. Defining the looping condition.
465. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;


while (pushupsToDo > 0) {

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:

466. Initializing a counter variable.


467. Defining the looping condition.
468. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

469. Initializing a counter variable.


470. Defining the looping condition.
471. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

472. Initializing a counter variable.


473. Defining the looping condition.
474. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

475. Initializing a counter variable.


476. Defining the looping condition.
477. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

478. Initializing a counter variable.


479. Defining the looping condition.
480. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

481. Initializing a counter variable.


482. Defining the looping condition.
483. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

484. Initializing a counter variable.


485. Defining the looping condition.
486. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

487. Initializing a counter variable.


488. Defining the looping condition.
489. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

490. Initializing a counter variable.


491. Defining the looping condition.
492. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

493. Initializing a counter variable.


494. Defining the looping condition.
495. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

496. Initializing a counter variable.


497. Defining the looping condition.
498. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

499. Initializing a counter variable.


500. Defining the looping condition.
501. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

502. Initializing a counter variable.


503. Defining the looping condition.
504. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

505. Initializing a counter variable.


506. Defining the looping condition.
507. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

508. Initializing a counter variable.


509. Defining the looping condition.
510. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

511. Initializing a counter variable.


512. Defining the looping condition.
513. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

514. Initializing a counter variable.


515. Defining the looping condition.
516. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

517. Initializing a counter variable.


518. Defining the looping condition.
519. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

520. Initializing a counter variable.


521. Defining the looping condition.
522. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

}
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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:
for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:
while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS


Incrementing While Loops
When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

523. Initializing a counter variable.


524. Defining the looping condition.
525. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!
Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.
We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

526. Initializing a counter variable.


527. Defining the looping condition.
528. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {


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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.

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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
529. Initializing a counter variable.
530. Defining the looping condition.
531. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

532. Initializing a counter variable.


533. Defining the looping condition.
534. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

535. Initializing a counter variable.


536. Defining the looping condition.
537. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

538. Initializing a counter variable.


539. Defining the looping condition.
540. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

541. Initializing a counter variable.


542. Defining the looping condition.
543. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

544. Initializing a counter variable.


545. Defining the looping condition.
546. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
547. Initializing a counter variable.
548. Defining the looping condition.
549. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;


while (pushupsToDo > 0) {

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:

550. Initializing a counter variable.


551. Defining the looping condition.
552. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

553. Initializing a counter variable.


554. Defining the looping condition.
555. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

556. Initializing a counter variable.


557. Defining the looping condition.
558. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

559. Initializing a counter variable.


560. Defining the looping condition.
561. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

562. Initializing a counter variable.


563. Defining the looping condition.
564. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

565. Initializing a counter variable.


566. Defining the looping condition.
567. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

568. Initializing a counter variable.


569. Defining the looping condition.
570. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

571. Initializing a counter variable.


572. Defining the looping condition.
573. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

574. Initializing a counter variable.


575. Defining the looping condition.
576. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

577. Initializing a counter variable.


578. Defining the looping condition.
579. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

580. Initializing a counter variable.


581. Defining the looping condition.
582. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

583. Initializing a counter variable.


584. Defining the looping condition.
585. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

586. Initializing a counter variable.


587. Defining the looping condition.
588. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

}
LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:
589. Initializing a counter variable.
590. Defining the looping condition.
591. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops
LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;


while (pushupsToDo > 0) {

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:

592. Initializing a counter variable.


593. Defining the looping condition.
594. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

595. Initializing a counter variable.


596. Defining the looping condition.
597. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

598. Initializing a counter variable.


599. Defining the looping condition.
600. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

601. Initializing a counter variable.


602. Defining the looping condition.
603. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

604. Initializing a counter variable.


605. Defining the looping condition.
606. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

607. Initializing a counter variable.


608. Defining the looping condition.
609. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

610. Initializing a counter variable.


611. Defining the looping condition.
612. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

613. Initializing a counter variable.


614. Defining the looping condition.
615. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

616. Initializing a counter variable.


617. Defining the looping condition.
618. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

619. Initializing a counter variable.


620. Defining the looping condition.
621. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

622. Initializing a counter variable.


623. Defining the looping condition.
624. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS


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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:

int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

625. Initializing a counter variable.


626. Defining the looping condition.
627. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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.

LEARN JAVA: LOOPS

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS


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.

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;

while (wishes < 3) {

// code that will run


wishes++;

}
 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

LEARN JAVA: LOOPS

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.

We’ll go over three types of loops that you’ll see everywhere:

 while loops
 for loops
 for-each loops

LEARN JAVA: LOOPS

While We're Here


A while loop looks a bit like an if statement:

while (silliness > 10) {

// 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;

// enter loop if condition is true


while (passcode != 0524 && attempts < 4) {

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;

// This will cause an infinite loop:


while (hedgehogs < 6) {

System.out.println("Not enough hedgehogs!");

}
In the example above, hedgehogs remains equal to 5, which is less than 6. So we would
get an infinite loop.

LEARN JAVA: LOOPS

Incrementing While Loops


When looping through code, it’s common to use a counter variable. A counter (also
known as an iterator) is a variable used in the conditional logic of the loop and (usually)
incremented in value during each iteration through the code. For example:

// 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.

So the output would look like:

Wish granted.
Wish granted.
Wish granted.
We can also decrement counters like this:
int pushupsToDo = 10;

while (pushupsToDo > 0) {

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:

628. Initializing a counter variable.


629. Defining the looping condition.
630. Incrementing the counter.

The opening line might look like this:

for (int i = 0; i < 5; i++) {

// code that will run

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

Using For Loops


for loops aren’t just a nicer syntax; they also help us remember to increment
our counter — something that is easy to forget when we increment with
a while loop. Leaving out that line of code would cause an infinite loop —
yikes!

Fortunately, equipped with your new understanding of for loops, you can help
prevent infinite loops in your own code.

LEARN JAVA: LOOPS

Iterating Over Arrays and ArrayLists


One common pattern you’ll encounter as a programmer is looping through a
list of data and doing something with each item. In Java, that list would be an
array or ArrayList and the loop could be a for loop. But wait, how does this
work?

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.

For example, if we wanted to add 1 to every int item in an array secretCode, we


could do this:

for (int i = 0; i < secretCode.length; i++) {

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.

In the case of an ArrayList, this code would look like:

for (int i = 0; i < secretCode.size(); i++) {

int num = secretCode.get(i);


secretCode.set(i, num + 1);

LEARN JAVA: LOOPS

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.
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;

while (wishes < 3) {

// code that will run


wishes++;

 forloops: These are ideal for when you are incrementing or decrementing with a
counter variable. For example:

for (int i = 0; i < 5; i++) {

// code that will run

 For-each loops: These make it simple to do something with each item in a list. For
example:

for (String inventoryItem : inventoryItems) {

// do something with each inventoryItem

You might also like