Nested loop in java
A nested loop is a loop within another loop. We can nested for loops is a
loop within another for loop or vice versa.
Nested for loop
Syntax
for Nested For loop:
// this is outer loop
for ( initial-value; test-condition;
increment / decrement )
{
// this is the inner loop
for ( initial-value; test-condition;
increment / decrement )
{
// statement within the inside loop
}
} //statement of outer
loop or end of scope of outer loop
Syntax for Nested While loop:
// this is outer loop
while(test-condition)
{
// this is the inner loop
while(test-condition)
{
// statement within the inside loop
}
// statement of outer loop or end of scope
of outer loop
}
Syntax for Nested Do-While
loop:
// this is outer loop
do{
// this is the inner loop
do{
// statement within the inside loop
}while(test-condition);
// statement of outer loop or end of scope
of outer loop
}while(test-condition);
The execution of
statement of nested loop will perform its task from inner most loop to outer
most loop. In nested loop, the loop inside a loop will finished first then the
outer most loop.
Nested For loop in java
Example of nested for loop in java
public class
NestedForLoop1
{
public static void main(String args[])
{
int i, j;
for(i=1;i<=3;i++)
{
System.out.println("Outer
Loop: "+ i);
for(j=1;j<=2;j++)
{
System.out.print(" Inner
Loop :"+ j);
}
System.out.println();
}
}
}
Sample output
Outer Loop:1
Inner Loop:1 Inner Loop:2
Outer Loop:2
Inner Loop:1 Inner Loop:2
Outer Loop:3
Inner Loop:1 Inner Loop:2
Here outer most loop
will execute three times and inner loop will execute six times as it run three
times from outer most loop.
Print prime number between 1 to 100
public class
PrintPrimeOneTo100
{
public static void main(String atgs[])
{
int j, f=1,i;
for (i=2; i<=100; i++)
{
f=1;
for (j=2; j<i; j++)
{
if(i % j==0)
// if number is divided other
then 1 and by itself the flag variable become f=0
{
f=0;
break;
}
}
// print prime number if flag variable is f=1
If (f==1)
{
System.out.print (I + "
");
}
}
}
}
In the above program all
the prime number will print between 1 to 100. And this is done with the help of
nested for loop. Here in each iteration value I of first loop will check for
prime number and if it so it will print the prime number.
Print the series s = 1!
+ 2! + 3! + 4! + 5!
public class
FactorialofSum
{
public static void main(String args[])
{
int i, j, f, s=0;
for (i=1; i<=5; i++)
{
f=1;
// factorial of ith digit
for(j=1; j<=i; j++)
{
f=f*j;
}
// print the factorial f
System.out.println ("Factorial="+f);
// summation of factorial
s=s + f;
}
System.out.println ("Sum="+s);
}
}
Sample output
Factorial= 1
Factorial= 2
Factorial= 6
Factorial= 24
Factorial= 120
Sum = 153
Star pattern with nested for loop
*
* *
* * *
* * * *
* * * * *
public class StarPattern
{
public static void main(String args[])
{
int i, j;
for
(i=1 ;i<=5; i++)
{
For (j=1; j<=i; j++)
{
System.out.print ("*
");
}
System.out.println (); // dummy
println
}
}
}
Nested while loop in java
Nested while is a loop
within another while loop. The inside loop will end first the outside loop or we
can say the function of the inner loop continues till the condition of the inner
loop is satisfied or become false.
Syntax
while(test-expression)
{
Statement
1;
Statement 2;
while(test-expression)
{
Statement
1;
Statement 2;
Statement 31;
}
}
Example of the nested while loop in java
class NestedWhileLoop
{
public static void main(String args[])
{
// Local variable initialized i=1, j=1
int i=1,j=1;
while(i<=3)
{
System.out.println("\n"+i+" "+"outer loop
executed\n");
while(j<=2)
{
System.out.println(j+"
"+"inner loop executed");
j++;
} // end of inner loop
i++; // iterate i++
j=1; // initialize j again with 1
}// end of outer loop
}
}
Star pattern in java with nested while loop
public class
StarPatternWhileLoop
{
public static void main(String args[])
{
int i=1, j;
while(i<=5) // outer while loop
{
j=1;
while(j<=i) // inner while loop
{
System.out.print("*
");
j++; // inner while loop iteration
}
System.out.println();
i++; // outer while loop iteration
}
}
}
Sample Output
*
* *
* * *
* * * *
* * * * *
Nested do while loop in java
Syntax
Statements 1;
Statements 2;
do {
Statements
1;
Statements 2;
Statements 3;
Do {
Statements 1;
Statements 2;
Statements 3;
} while(test-condition);
} while (test-condition);
Example of nested do while loop
class NestedDoWhileLoop
{
public static void main(String args[])
{
// Local variable initialized i=1, j=1
int i=1,j=1;
do{
System.out.println("\n"+i+" "+"outer loop
executed\n");
do{
System.out.println(j+"
"+"inner loop executed");
j++;
}while(j<=2); // end of inner
loop
i++; // iterate i++
j=1; // initialize j again with 1
} while(i<=3);// end of outer loop
}
}
Star pattern in java with do while loop
public class
StarPatternDoWhileLoop
{
public static void main(String args[])
{
int i=1, j;
do// outer while loop
{
j=1;
do // inner while loop
{
System.out.print ("*
");
j++; // inner while loop iteration
} while (j<=i);
System.out.println ();
i++; // outer while loop iteration
} while (i<=5);
}
}
Loops in Java - Java loop explained with examples