Loops in Java with Example

  • by

There may be a situation when you need to execute a block of code several number of times. To solve this type of situation in Java, there are three basic loops: for loop, while and do while loop in Java. Loop statement allows us to execute a statement or group of statements in multiple times. In this article we are going to discuss about loops viz.
1] for loop
2] while
3] do while

1] for loop -:
It executes a block of statements repeatedly until the specified condition returns false. The for statement provides a compact way to iterate over a range of values. The general form of the for loop statement can be expressed as follows:
Syntax -:
for (Initialization ; Condition ; Increment / Decrement)
{
statement 1 ;
}
Initialization -: Executes only once during the beginning of loop
Condition -: Executes the block of statement iterate / repeatedly until this condition returns false.
Increment / Decrement -: It executes after each iteration of loop.

Exapmle -:
public class forloop {

public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1; i<=10; i++)
{
System.out.print( i );
}
}

}
Otuput -:
12345678910
If we use ‘System.out.println( i )’; instead of ‘System.out.print( i )’ , then print each number (1 to 10) in new line because of ‘println’

1.1] Nested for loop -:
Placing of one for loop inside the body of another for loop is called nesting. When you “nest” two loops, the outer loop takes control of the number of complete repetitions of the inner loop. While all types of loops may be nested, the most commonly nested loops are for loops. When working with nested loops, the outer loop changes only after the inner loop is completely finished
Syntax -;
for (Initialization; Condition; Increment / Decrement)
{
for (Initialization; Condition; Increment / Decrement)
{
statement 1;
}
}

Example -:
package basic;

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
for(int k = 3;k>1; k–)
{
System.out.println();
for(int i =0; i<=5; i++)
{
for(int j=0; j<i; j++)
{
System.out.print(“*”);
}
System.out.println(“”);
}
}
}
}
Output -:
*
**
***
****
*****

3] While -:
The while statement continually executes a block of statements while a particular condition is true. Like for loop, it also executes a block of statements repeatedly until the condition(Boolean expression) returns false. Syntax can be expressed as –;
while (condition)
{
statement ;
}

Example -:
package basic;
public class whileloop {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
while(i<=5)
{
System.out.println(i);
i = i+2;
}
}
}
Output -:
0
2
4

4] do while -;
Syntax -;
do
{
statement 1;
} while (condition);

Example -:
package basic;
public class whileloop {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i =1;
do
{
System.out.println(i);
i++;
}while (i<5);
}
}
Output -:
1
2
3
4

Note -; The difference between while and do while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once,

Leave a Reply