Docsity

Java Loops: For, While, Do-While, Study notes of Computer Science

Toggle sidebar
bg1
CS121/IS223
Using loops
When we have a number of things to do that are similar, we use a loop. Java has three types, a for
loop, a while loop and a do-while loop. The for loop is the simplest, but the while loop is best for
reading from files, and we will do that the most. We will discuss them in the order given above.
The for loop is used when we know how many items are to be processed. The form for this is
for (int count = 0; count < 10; count ++)
{
System.out.println ("Count: " + count);
}
This loop will count from 0 to 9, since the counter started with zero and stopped at the number one
less than 10.
If we want to add up all the numbers from 1 to 10, we will need a variable to hold the sum. The
following loop will do this. (This is sometimes called a running sum.)
int sum = 0;
for (int count = 1; count <= 10; count ++)
sum = sum + count;
System.out.println ("Sum: " + sum);
It is often helpful to trace the execution of a loop. This is a way to look at each variable as it changes
throughout execution.
count 1 2 3 4 5 6 7 8 9 10
sum 1 3 6 10 15 21 28 36 45 55
A for loop is called a count controlled loop, since we know before it starts how many times it will
execute. The limits may be read in before the loop begins.
int noNumbers, sum = 0;
System.out.println ("Enter the number of numbers: ");
noNumbers = scan.nextInt ();
for (int count = 0; count < noNumbers; count ++)
sum = sum + count;
System.out.println ("Sum: " + sum);
In many cases, we do not know how many times the loop will be executed. We need a while or do-
while loop when this happens. The while loop executes as long as a condition is true. When the
condition changes to false, the loop is terminated. If more that one statement is done inside the loop,
curly braces are needed to indicate that several statements are to be performed.
int sum = 0; count = 1;
while (count < 5)
{
sum = sum + count;
count = count + 1;
}
System.out.println ("Sum: " + sum);
The trace above is the same for this loop as for the for loop above. Actually the for loop is
implemented by the compiler with the while loop shown.
pf3

Partial preview of the text

Download Java Loops: For, While, Do-While and more Study notes Computer Science in PDF only on Docsity!

CS121/IS

Using loops When we have a number of things to do that are similar, we use a loop. Java has three types, a for loop, a while loop and a do-while loop. The for loop is the simplest, but the while loop is best for reading from files, and we will do that the most. We will discuss them in the order given above. The for loop is used when we know how many items are to be processed. The form for this is for (int count = 0; count < 10; count ++) { System.out.println ("Count: " + count); } This loop will count from 0 to 9, since the counter started with zero and stopped at the number one less than 10. If we want to add up all the numbers from 1 to 10, we will need a variable to hold the sum. The following loop will do this. (This is sometimes called a running sum .) int sum = 0; for (int count = 1; count <= 10; count ++) sum = sum + count; System.out.println ("Sum: " + sum); It is often helpful to trace the execution of a loop. This is a way to look at each variable as it changes throughout execution. count 1 2 3 4 5 6 7 8 9 10 sum 1 3 6 10 15 21 28 36 45 55 A for loop is called a count controlled loop, since we know before it starts how many times it will execute. The limits may be read in before the loop begins. int noNumbers, sum = 0; System.out.println ("Enter the number of numbers: "); noNumbers = scan.nextInt (); for (int count = 0; count < noNumbers; count ++) sum = sum + count; System.out.println ("Sum: " + sum); In many cases, we do not know how many times the loop will be executed. We need a while or do- while loop when this happens. The while loop executes as long as a condition is true. When the condition changes to false, the loop is terminated. If more that one statement is done inside the loop, curly braces are needed to indicate that several statements are to be performed. int sum = 0; count = 1; while (count < 5) { sum = sum + count; count = count + 1; } System.out.println ("Sum: " + sum); The trace above is the same for this loop as for the for loop above. Actually the for loop is implemented by the compiler with the while loop shown.

int sum = 0; int count = 1; while (count < 5) { false sum = sum + count; count < 5 count = count + 1; } true Another example reads prices from the keyboard and adds them up. int total = 0; double price; System.out.println ("Enter the first price: "); price = scan.nextDouble (); while (price > 0) { total = total + price; System.out.println ("Enter the next price: "); price = scan.nextDouble (); } This loop reads prices until a zero or negative price is entered. This is called reading to a sentinel. The loop stops when the sentinel (here a zero price) is entered. In order to get the first price, we have to read it before entering the loop. Then we must read again at the end of the loop. This way the zero will not be added into the total. Adding a zero won’t make a difference in the total, but if the sentinel is some non-zero value, it will. An example might be averaging temperatures. The sentinel might be a negative number, such as -30. If the temperature is measured in Celsius, a zero temperature might be a possible value. But - Celsius is much below any expected value. int total = 0, noTemps = 0; double temp; System.out.println ("Enter the first temperature: "); temp = scan.nextDouble (); while (temp > -30) { int sum = 0;

int count = 1;

sum = sum + count; count = count + 1;