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:
- initializing condition: the number of spaces variable is set to 5
- condition to continue: the loop will continue while the number of spaces is greater than zero
- updating action: the number of spaces variable is decremented with each iteration; this is commonly called a loop control variable or lcv
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:
- initializing condition: the change to be returned variable is set to 0.04
- condition to continue: the loop will continue while there is still change left to disburse
- updating action: the change to be returned variables is decremented each time a penny is given out; this variable is also considered a loop control variable
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:
- initializing condition: the letter counter is set to zero; the first character is acquired
- condition to continue: the loop will continue while a period is not found in the incoming characters
- updating action: a new character will be acquired from the input data stream to be tested and counted if it is not a period; this is an updating action of its own, so there is no loop control variable used here
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.