Example:The while loop repeatedly executes the statements in the loop body when the loop-
continuation-condition evaluates to true.
loop-continuation-condition is count < 100 and the loop body contains the
following two statements:
In this example, you know exactly how many times the loop
body needs to be executed because the control variable
count is used to count the number of executions. This type
of loop is known as a counter-controlled loop
Here is another example to help understand how a
loop works.
int sum = 0, i = 1;
while (i < 10) {
sum = sum + i;
i++;
}
[Link]("sum is " + sum); // sum is 45
What happens if the loop is mistakenly written as follows?
int sum = 0, i = 1;
while (i < 10) {
sum = sum + i;
}
This loop is infinite, because i is always 1 and i < 10 will
always be true
• Caution
• Programmers often make the mistake of executing a loop one more or
less time. This is commonly known as the off-by-one error. For example,
the following loop displays Welcome to Java 101 times rather than
100 times. The error lies in the condition, which should be count < 100
rather than count <= 100.
int count = 0;
while (count <= 100)
{ [Link]("Welcome to
Java!");
count++;
Write a program that prompts the user to enter an answer for a question on
addition of two single digits. Using a loop
import [Link];
public class AdditionQuiz {
public static void main(String[] args) {
int number1 = (int)([Link]()*10);
int number2 = (int)([Link]()*10);
//creare a Scanner
Scanner input = new Scanner([Link]);
[Link]("what is "+number1 +" + "+ number2+" ? ");
int answer = [Link]();
while(number1 + number2 != answer){
[Link]("Wrong answer. Try again. What is "
+ number1 + " + "+ number2+" ? ");
answer = [Link]();
}
[Link]("You got it!");
}}
The do – while Loop
• A do-while loop is the same as a while loop except
that it executes the loop body first and then checks
the loop continuation condition.
The do-while loop is a variation of the while loop.
Its syntax is:
do {
// Loop body;
Statement(s);
} while (loop-continuation-condition);
The do-while loop executes the loop body first, then checks the
loopcontinuation-condition to determine whether to continue
or terminate the loop.
Example
int i = 0;
do {
[Link](i);
i++;
} while(i < 5);
The for Loop
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
• Statement 1 is executed (one time) before the execution of
the code block.
• Statement 2 defines the condition for executing the code
block.
• Statement 3 is executed (every time) after the code block
has been executed.
• The example below will print the numbers 0 to 4:
Example
for (int i = 0; i < 5; i++) {
[Link](i);
}
This example will only print even values
between 0 and 10:
Example
for (int i = 0; i <= 10; i = i + 2) {
[Link](i);
}
Nested Loops
It is also possible to place a loop inside another loop. This is
called a nested loop.
The "inner loop" will be executed one time for each iteration of
the "outer loop":
Example
// Outer loop
Outer: 1
for (int i = 1; i <= 2; i++) {
Inner: 1
[Link]("Outer: " + i); // Executes 2 times
Inner: 2
Inner: 3
// Inner loop
Outer: 2
for (int j = 1; j <= 3; j++) {
Inner: 1
[Link](" Inner: " + j); // Executes 6 times (2 * 3)
Inner: 2
}
Inner: 3
}
Which Loop to Use?
• You can use a for loop, a while loop, or a do-while loop,
whichever is convenient.
The while loop and for loop are called pretest loops
because the continuation condition is checked before the
loop body is executed. The do-while loop is called a
posttest loop because the condition is checked after the loop
body is executed.
Keywords break and continue
• The break and continue keywords provide additional
controls in a loop .
• You have used the keyword break in a switch statement. You can
also use break in a loop to immediately terminate the loop.
Example
• This example stops the loop when i is equal to 4:
OutPut
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
0
} 1
[Link](i); 2
}
3
Keyword continue
• You can also use the continue keyword in a loop. When it is
encountered, it ends the current iteration and program
control goes to the end of the loop body. In other words,
continue breaks out of an iteration while the break
keyword breaks out of a loop
Example
• This example skips the value of 4:
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
[Link](i);
}
Break and Continue in While Loop
int i = 0;
int i = 0;
while (i < 10) { while (i < 10) {
[Link](i); if (i == 4) {
i++;
i++; continue;
if (i == 4) { }
[Link](i);
break; i++;
}} }