Section 10f

The Java while Loop


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The Original Basic Loop

It is important for you to have learned the concept of repetition before you learn the coding of it in the Java programming language. Coding is nothing more than translation of the solution that you create, so you want to make sure the solution is correct before you spend any time working on the code. At this point, you will be introduced to the format or syntax of how to perform program repetition in Java. As mentioned earlier, you will not be surprised at what it looks like because it is coded in much the same way as you have seen it written in pseudo code. To wit . . .

Using the while Loop

Once you have written your loop in pseudo code, the process to turn it into program code is quite easy. As mentioned above, there are two ways to implement the while loop. The first -- shown in this topic -- is to simply use the while keyword, as shown below.

// in global constant area of program
const char SPACE = ' ';

// in method where loop is used (local)
int numberOfSpaces = 5;

// start loop until number of spaces is zero
while( numberOfSpaces > 0 )
{
// print a space
// method: printChar
conIO.printChar();

// decrement the number of spaces to be printed
numberOfSpaces--;
}
// end loop

Evaluate the loop algorithm:

For the change making loop, the translation from pseudo code to program code is shown. The video on this showed the action with integers; this one shows it as floating point values. Note that there can be a little problem with double precision here. The change making code may hav a changeToBeReturned that is ever so close to zero but positive, in which another cent too many is being returned. As will be discussed later, you may have to bracket the test value just above and just below zero.

// in method where loop is used (local)
double changeToBeReturned = .04;

// start loop while there is still change to be returned
while( changeToBeReturned > 0.00 )
{
// give out one penny
// method: printString
conIO.printString( "One penny returned" );

// decrement penny from change
changeToBeReturned -= 0.01;
}
// end loop

Evaluate the loop algorithm:

Notice that the while test is an inequality test (i.e., changeToBeReturned > 0.00 ). Since, as you have seen before, double values can be imprecise in computer memory, it is almost always a bad idea to try testing for equality ("==") or not-equality ("!="). Thanks to the imprecision, two double values will rarely have exactly the same value in them. For example, two variables assigned 5.00, one might actually hold 4.9998976, and the other might hold 5.0001035. As mentioned in a previous chapter, this is due to the way the double values are held in memory and contributes to their imprecision. Always use inequality tests for double values.

If you are testing greater than, use a number slightly below the test value, and if you are using less than, use a number slightly above the test value.

Examples.

while( testValue > 4.999 ) // greater than

or

while( testValue < 5.001 ) // less than

For the sentence letter counting loop, consider the following code. Note that the Console_IO_Class method getChar is used, so it will capture every character in the data stream or until the period is found.

// in class global constant area
public static final char PERIOD = '.';

// initialize method/variables

// initialize character count
int numChars = 0;

// declare input character
int inChar;

// prompt user for input
// method: printString
conIO.printString( "Enter a sentence ending with a period: " );

// get first character (as an integer), prime loop
// method: getChar
inChar = conIO.getChar();

// loop while input character is not a period
while( (char)inChar != PERIOD )
{
// increment the number of characters
numChars++;

// get next character (as an integer) - reprime
// method: getChar
inChar = conIO.getChar();
}
end loop

// display number of characters
// method: printString, printEndline
conIO.printEndline();
conIO.printString( "The number of characters is: " + numChars );
conIO.printEndline();

Evaluate the loop algorithm:

The Process of Repetition, Continued

In this topic, you have seen the same thing you saw in the previous topic, only with the translation to programming code. The while loop is a simple process, and made simpler if you conduct the three part loop evaluation process. As long as you conduct this process, along with your "What is the action?" and "What is updated?" analysis questions, you will be in charge of your loops, instead of the other way around. Watch this video to see the last loop implemented, with some extra description.