COSC1046 - Chapter 5
COSC1046 - Chapter 5
Chapter 5 – Loops
Loops are an incredibly important structure when programming, not just in Java. They are used to tell
your program to execute statements repeatedly. Suppose you wanted to print the same String over and
over again, how would you do that?
In this case, and any other case where we need repeated steps, we’ll be using a loop.
There are three major types of loops covered in the course: The while loop, for loop, and do while loop.
While loop
We’ll go back to our original problem, printing the same String 100 times. This is easily solved with a
while loop. We’ll make a count variable to keep track of what number we’re on, and our continuation
condition will be if we haven’t gone over 100 times.
The loop continuation condition checks that count is less than 100 each time, if this is true it executes
what’s inside the loop. Once the loop is completed, it goes back up to the continuation condition and
checks it again. If the loop continuation condition is false, it will not execute what is inside the loop, and
continue on to the code below the loop. Here is the structure of our while loop:
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6
Do while loop
A do while loop is exactly the same as a while loop, except it executes the body of the loop before the
continuation condition. This allows you to guarantee that the block will execute at least once.
Note the semicolon after the loop continuation condition, this is necessary.
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6
For loop
The for loop is the most complex loop that we cover in the course. It contains an initial action,
continuation condition, and action after each iteration within the loop’s definition:
In most cases, the initial-action is defining a variable, like int count = 0;. Here’s our example written with
a for loop:
In this example, we define i outside of the loop definition, but it is possible to define it inside the loop as
well.
This way is more common. Here is the structure of this for loop:
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6
It is easy to see that the while and do while loop are the easier loops to work with, but with practice the
for loop gets easier to use, and is more versatile and compact. However, it is important to note that all
of these loops perform the exact same thing, and thus can be used as the solution for any problem that
requires a loop. In general, a for loop is used when you know the number of iterations that you’ll be
making, whereas the while loop and do while loop are used when it is not know. It is possible though to
use any three for any problem.
Nested loops
Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated
the inner loops are re-entered and started anew.
After the above code has executed, what do you think the value for count would be?
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6
import java.util.Scanner;
names[i] = input.next();
scores[i] = input.nextInt();
}
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6
secondLowestScore = lowestScore;
secondLowestName = lowestName;
lowestScore = scores[i];
lowestName = names[i];
secondLowestScore = scores[i];
secondLowestName = names[i];
System.out.println("The student with the lowest score is " + lowestName + " with a score of " +
lowestScore);
System.out.println("The student with the second-lowest score is " + secondLowestName + " with a
score of " + secondLowestScore);
int count = 0;
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6
if (i % 3 == 0 && i % 4 == 0) {
count++;
if (count % 10 == 0) {
System.out.println();