0% found this document useful (0 votes)
7 views9 pages

Lesson-2 3

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

Lesson-2 3

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

Java Loops

Many Java programs require repetition of certain code blocks.

The idea of repeating code is called iteration or looping.

In Java, one way of looping is with the while loop:

while (expression)

{
[Java code block to repeat while expression is true]
}
In this structure, all code between the curly braces is repeated
while the given logical expression is true.

Note there is no semicolon at the end of the while line.

In the if structure, the code block is processed just once if


expression is true. In the while structure, the code block is
continually processed as long as expression remains true.

A while loop structure will not execute even once if expression is


false the first time through.

If we do enter the loop (expression is true), it is assumed at some


point expression will become false to allow exiting.

Once this happens, code execution continues at the statement


following the closing right brace.
Very important point about loops – if you get in one, make sure you
get out at some point.

In the while loop, if expression is always true, you will loop forever
– something called an infinite loop.

A loop that can be used in a rocket countdown:

counter = 10;
while (counter > 0)
{
counter = counter - 1;
}
Example 2 (assuming we have a Random object named my
Random):

rolls = 0;
counter = 0;
while (counter < 10)
{
roll = roll + 1;
if ((myRandom.nextInt(6) + 1) == 6)
{
counter = counter + 1;
}
}
This loop repeats while the counter variable remains less than 10.
The counter variable is incremented (increased by one) each time a
simulated dice rolls a 6. The roll variable tells you how many rolls
of the dice were needed to roll 10 sixes.
If the logical expression used by a while loop is false the first time
the loop is encountered, the code block in the while loop will not
be executed.
Another looping structure in Java that will always be executed at
least once.

This loop is a do/while structure:

do
{
[Java code block to process]
}
while (expression);
Unlike the while loop, there is a semicolon at the end of the while
statement.

The code block in the braces repeats ‘as long as’ the boolean-
valued expression is true.

The loop is always executed at least once.

Somewhere in the loop, expression should be changed to false to


allow exiting.

Example: Keep adding three to a sum until the value exceeds 50.

sum = 0;
do
{
sum = sum + 3;
}
while (sum <= 50);
Another dice example:
sum = 0;
roll = 0;
do
{
sum = sum + myRandom.nextInt(6) + 1;
roll = roll + 1;
}
while (sum <= 30);

This loop rolls a simulated dice while the sum of the rolls does not
exceed 30. It also keeps track of the number of rolls (roll) needed
to achieve this sum.

You need to decide which of the loop structures (while, do/while)


fits your project.
The major difference is that a do/while loop is always executed at
least once; a while loop may never be executed.

In both looping structures, this means that, at some point, the


checking logical expression must become false to allow exiting the
loop.

When you exit a while loop, processing continues at the next Java
statement after the closing brace.

In a do/while loop, processing continues at the Java statement


after the while statement.

If, at some point in the code block of a loop, you decide you need
to immediately leave the loop, this can be done using a Java break
statement.
When a break statement is encountered, processing is
immediately transferred to the Java statement following the loop
structure.
sum = 0;
roll = 0;
do
{
// Roll a simulated die
die = myRandom.nextInt(6) + 1;
if (die == 5)
{
break;
}
sum = sum + die;
roll = roll + 1;
}
while (sum <= 30);

You might also like