0% found this document useful (0 votes)
3 views7 pages

COSC1046 - Chapter 5

The document discusses different types of loops in programming including while, do-while, and for loops. It provides examples of using each loop type to print a string multiple times and explanations of when each loop type is generally used.

Uploaded by

shekhar bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

COSC1046 - Chapter 5

The document discusses different types of loops in programming including while, do-while, and for loops. It provides examples of using each loop type to print a string multiple times and explanations of when each loop type is generally used.

Uploaded by

shekhar bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6

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

A while loop is structured as following:

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

Here is the structure of our do while loop

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

When to use which loop?

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

Assignment Portion – hand in once completed

Answer for 5.9

import java.util.Scanner;

public class FindTwoLowestScores {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of students: ");

int numberOfStudents = input.nextInt();

String[] names = new String[numberOfStudents];

int[] scores = new int[numberOfStudents];

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

System.out.print("Enter the name for student " + (i + 1) + ": ");

names[i] = input.next();

System.out.print("Enter the score for student " + (i + 1) + ": ");

scores[i] = input.nextInt();

}
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6

int lowestScore = Integer.MAX_VALUE;

int secondLowestScore = Integer.MAX_VALUE;

String lowestName = "";

String secondLowestName = "";

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

if (scores[i] < lowestScore) {

secondLowestScore = lowestScore;

secondLowestName = lowestName;

lowestScore = scores[i];

lowestName = names[i];

} else if (scores[i] < secondLowestScore) {

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

Answer for 5.10

public class DivisibleByThreeAndFour {

public static void main(String[] args) {

int count = 0;
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 6

for (int i = 100; i <= 1000; i++) {

if (i % 3 == 0 && i % 4 == 0) {

System.out.print(i + " ");

count++;

if (count % 10 == 0) {

System.out.println();

You might also like