Last week you were introduced to statements in Java. A simple statement performs an action – a definite clear expression of something. Next, you learned about compound statements, which are combinations of simple statements surrounded by curly braces as shown:

{
<simple_statement>;
<simple_statement>;
<simple_statement>;
}

We rounded off the post with control statements – statements that play a role in how and when other statements are executed. You only discussed two types of control statements, if/if-else/if-else-if statements and switches. This week, we finish up with the last kind of control statements called loops. To begin, let’s understand when and why we might need a loop; you may be able to come up with a definition before I say one.

Look at this if statement:

int age = 7;
if(age < 18) {
System.out.println(“You are under age.”);
} else if (age == 18) {
System.out.println(“You are just the right age.”);
} else {
System.out.println(“You are old enough.”);
}

Imagine you had more than one age to check against 18, the minimum permitted age, like so:

int John = 7;
int Don = 45;
int Qori = 21;

You might be thinking of duplicating the if-else-if statement, but that’s not very efficient. Remember, the programming world has your back for the most part. This is a good example of when you might need loops. A loop allows us to repeat statements multiple times. A loop is a time saver. Three main loops we work with in Java are the for loop, while loop, and the do-while loop.

Before we can begin implementing our loops we need something to loop over. Sure, we have our three variables, John, Don, and Qori. But we need to put them in a container so we can reference the container and furthermore, its contents from the loop. We will use an array. An array is an ordered arrangement of items. It’s that simple. With that said, let’s get to the loops!

int John = 7;
int Don = 45;
int Qori = 21;

int[] ages = {John, Don, Qori};

for(int age : ages) {
if(age < 18) {
System.out.println(“You are under age.”);
} else if (age == 18) {
System.out.println(“You are just the right age.”);
} else {
System.out.println(“You are old enough.”);
}
}

After creating our array of integers with int[], we put our individual variables in the array so we can use them at once in the for loop. The for loop we’re using is called an enhanced for loop. You can read the line for(int age : ages) as “for each age in ages, do something with age. The for loop will repeat the age-checking if statements, replacing the value of age with the next element in the ages array after each run.

Here is the original for loop doing the same thing as above:

int John = 7;
int Don = 45;
int Qori = 21;

int[] ages = {John, Don, Qori};

for(int index = 1; index <= ages.length; index++) {
if(ages[index] < 18) {
System.out.println(“You are under age.”);
} else if (age[index] == 18) {
System.out.println(“You are just the right age.”);
} else {
System.out.println(“You are old enough.”);
}
}

The only difference between the enhanced for loop and the original for loop is the way we reference each element in an array. Here we use an index. Remember, an array is an arrangement of items. So every element has a position and therefore an index. Think of the index as a reference to the elements in the array. In the line, for(int i = 1; i <= ages.length; index++), we declare and define an integer to hold our indices (int index = 1), specify the condition that our index holder must adhere to (index <= ages.length – here we’re saying work with index as long as it is less than or equal to the length of the array), and increment our index holder so we can move on the next element (index++). We access the element at the current index with age[index]. This for loop will perform the if statement as long as the condition is kept then it will increment it.

Here is the same program written with a while loop:

int John = 7;
int Don = 45;
int Qori = 21;

int[] ages = {John, Don, Qori};

int index = 1;
while(index <= ages.length) {
if(ages[index] < 18) {
System.out.println(“You are under age.”);
} else if (age[index] == 18) {
System.out.println(“You are just the right age.”);
} else {
System.out.println(“You are old enough.”);
}
index++;
}

The while loop takes a condition and will perform the statements as long as the condition is true. Here, we tell it to perform the if statements while the index is less than or equal to the number of items in the array. Notice we increment the index at the bottom. This way, we ensure the while loop doesn’t run forever.

Finally here is a do-while loop:

int John = 7;
int Don = 45;
int Qori = 21;

int[] ages = {John, Don, Qori};

int index = 1;
do {
if(ages[index] < 18) {
System.out.println(“You are under age.”);
} else if (age[index] == 18) {
System.out.println(“You are just the right age.”);
} else {
System.out.println(“You are old enough.”);
}
index++;
}while(index <= ages.length);

The while loop and the do-while loop differ only in the way they initially run. The while loop will check if the condition is true first and then run. The do-while loop will run first and then any other subsequent statement execution will depend on condition. The do-while loop always runs at least once.

Here’s an exercise for you, create an array of arrays (a two-dimensional array like a checkers board) and print out the items in the array. Here’s a hint: you will need to use a loop inside another loop.

Have fun and see you next week!

[content_band inner_container=”true” padding_top=”10px” padding_bottom=”10px” border=”none” bg_pattern=”http://www.zipcodewilmington.com/wp-content/uploads/Triangles__1441830476_50.73.209.90-compressor.jpg”]

Want to learn more about Java? Check out more about our curriculum and apply today!

[/content_band]