LOOPS
A loop is a structure that allows repeated execution of a block of statements. Java has three basic loops:
You use the while loop to execute a body of statements continuously while some conditions continue to be true.
Syntax
while (condition)
{
loop body;
}
Within a while loop structure, a boolean expression is evaluated. If it is true, then a block statements, called the loop body, executes, then the boolean expression is evaluated again. As long as the expression is true, the statements in the loop body continue to execute. When the boolean expression is false, the loop ends. If the boolean expression is never false, an infinite loop will occur.
| Example | Output | Comments |
| count=1; while(count < 3) { System.out.println("Hello"); count=count+1; } |
Hello Hello |
count is called a loop control variable. The loop control variable can be increment or decrement. |
The do ..... while loop is a loop that checks the bottom of the loop after one repetition has occurred. If you want to perform a task at least one time, the do ... while loop is a solution.
Syntax do
{ loop body;
}while(boolean expression);
Within the do ... while loop, the loop body is executed first, then the boolean expression is evaluated. If it is true, then a block statements, called the loop body, executes, then the boolean expression is evaluated again. As long as the expression is true, the statements in the loop body continue to execute. When the boolean expression is false, the loop ends. If the boolean expression is never false, an infinite loop will occur.
| Example | Output | Comments |
| count=1; do { System.out.println("Hello"); count=count+1; }while(count< 4); |
Hello Hello Hello |
count is called a loop control variable. The loop control variable can be increment or decrement. |
You can use a while loop when you need to perform a task some predetemined number of times. A loop that executes a specific number of times is a definite loop or counted loop. for loop is a definite loop.
Syntax for
(expression1;expression2;expression3)
{
loop body;
}
The following steps show how the for loop is executed.
| Example | Output |
| for (int i=0 ; i<3 ; i++) { System.out.println("hello"); } |
hello hello hello |
You can write a for loop that do not include any or all of the three expressions, but you always must include the semicolons.
| Example | Output |
| int i=0; for ( ; i<3 ; i++) { System.out.println("hello"); } |
hello hello hello |
| Example | Output |
| for ( int i=0 ; ; i++) { System.out.println("hello"); } |
hello hello hello hello . .
|
| Example | Output |
| for (int i=0 ; i<3 ; ) { System.out.println("hello"); i++; } |
hello hello hello |
| Example | Output |
| for ( ; ; ) { System.out.println("hello"); } |
hello hello hello . . |
You use a break statement in a Java program to exit from a while loop, a do while loop, or a for loop. The break statement causes an exit from only the innermost loop; it will not exit multiple loops. Before the break statement, it is often a condition statement.
| Example | Output |
| import javax.swing.*; public class Test { public static void main(String[ ] args) { int num, test|; String s; do { s = JOptionPane.showInputDialog("Enter a number"); num = Integer.parseInt(s); test=num % 2; if ( test = = 0) break; System.out.println(num +" is odd."); }while( 1 ); System.out.println(num + " is even"); } } |
number 1 1 is odd. number 5 5 is odd. number 6 6 is even
|
Note: while ( 1 ) means that the condition of loop is always true. Remember in Java any integer except 0 is a true value.
Counter-controlled loop
Write a while loop that displays Hello 100 times.
int i = 0;
while ( i < 100)
{ System.out.println(i);
i = i + 1;
}
Sentinel-value controlled loop
Accumulate all the integer numbers entered from the user until the
input is 0. The number 0 is the sentinel-value.
int num, sum = 0;
String s;
s = JOptionPane.showInputDialog("Enter a number");
num = Integer.parseInt(s);
while (num != 0)
{ sum = sum + num;
s = JOptionPane.showInputDialog("Enter a number");
num = Integer.parseInt(s);
}
System.out.println(sum);
Flag-controlled loop
Input a number
Accumulate the numbers
If the
accumulated value is greater than 30, then exit the loop and display the
accumulated value;
otherwise, input another number
int num , sum =0;
String s;
boolean flag = true;
while( flag)
{
s=JOptionPane.showInputDialog(“Enter a number”);
num= Integer.parseInt(s);
sum= sum + num ;
if( sum>30)
flag=false ;
} // Close the loop
System.out.println(sum);
Example
Display Hello 10 times
Use a while loop
int i= 1;
while (i<10)
{System.out.println(“hello”);
i=i+1;
}
Use a for loop
for (int i= 1; i<=10;i= i+1 )
System.out.println( “Hello”);
Example write a for loop to display even numbers starting from 4 to 60
for( int i = 4 ; i < = 60 ; i = i +2 )
System.out.println(i);