- Loop Essentials
A loop is a repetition control structure.
It causes a single statement or block to be executed multiple times
Like conditional statements, loops are controlled by boolean expressions
Java has three kinds of repetition statements:
- the while loop
- the do loop(also called do-while loop)
- the for loop
The programmer should choose the right kind of loop for the situation.
- The do-while Statement
General form:
do
{
statement1; // loop body
statement2;
. . . .
}
while(condition);
The block of statements is executed once initially, and then the condition is evaluated.
The block of statements is executed repeatedly until the condition becomes false
NOTE: Loop body can be a single statement,
an empty statement, or a block of statements.
When the expression is tested and found to be false,
the loop is exited and control is passed to the statement which follows the loop.
An example of a do-while loop
int count = 0;
do
{
count++;
System.out.println (count);
} while (count < 5);
The body of a do-while loop executes at least once
- The while Statement
A while statement has the following general form:
while ( condition )
{
statement1;
statement2;
. . .
}
If the condition is true, the block of statements is executed.
Then the condition is evaluated again, and if it is still true,
the statements are executed again.
The statements in the body of the while loop are executed repeatedly
until the condition becomes false
An example of a while statement:
int count = 1;
while (count <= 5)
{
System.out.println (count);
count++;
}
If the condition of a while loop is false initially, the body is never executed.
Therefore, the body of a while loop will execute zero or more times
- Comparing while and do-while
Difference between while and do-while:
do-while runs and then tests the condition
while first tests the condition and then if it is true - runs.
- While loop: The condition is tested, and only if it is true the statements
in the while body are executed.
This is repeated as long as the condition stays true.
- Do-while loop: the body is run once, then the condition is tested.
If true, the process is repeated.
- When the condition becomes false, the statements in the body are skipped,
and the execution continues from the next statement outside the body
of the while loop.
- Loop variable
The variable that determines the truth value of the 'while' expression is called loop variable
Three basic actions:
- Loop variable initialization necessary for while loop, optional for do-while
- Loop variable must be checked in the condition
- Loop variable must be updated - in the body of the loop, so that
eventually the condition becomes false
Failure to ensure these actions results in an infinite loop,
a loop that never stops unless the program is terminated.
- Two Types of Loops
- count controlled loops: repeat the block of
statements in the loop body a specified number of times.
The loop variable is used to count the number of loop repetitions
- event-controlled loops: some condition within the
loop body changes and this causes the repetition to stop.
The event may be represented by a sentinel value -
of the same type as the processed data, however not within the range
of the processed items.
Example of an event-control loop: the do-while loop in Lab05.
The sentinel value was 4 – same type as the user choice,
but not within the range of the valid choices (between 1 and 3)
Example of count controlled loop:
//Version 1:
System.out.println(“How many rounds?”);
rounds = scan.nextInt(); // initialization of the loop variable
do
{
// body: game statements
rounds--; // decrement the loop variable
}
while(rounds > 0); // check the loop variable
//Version 2:
System.out.println(“How many rounds?”);
rounds = scan.nextInt(); // initialization of the loop variable
while(rounds > 0) // check the loop variable
{
// body: game statements
rounds--; // decrement the loop variable
}