Table of Contents
When a forloop exists inside the body of another forloop, it is known as nested forloop in java
for (initialization; boolean_expresion; increment or decrement){
// statement(s) inside the body
for (initialization; boolean_expresion; increment or dicrement){
// statement(s) inside the body
}
}
Here we can see, a for loop is inside the body another for loop
Example programs
Program 1
This program displays square number pattern using nested whileloop
class forloop
{
public static void main(String args[])
{
for(int x=1;x<=10; x++) //Outer for loop
{
System.out.println();
for(int y=1;y<=10; y++)
{
System.out.print(y);
}
}
}
}
When you execute the above code, it produces the following result
Program 2
This program displays square star pattern using nested whileloop
When you execute above code , it produces the folloing result
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
Program 3
This program displays Floyd’s triangle number pattern using nested whileloop
When you execute above code , it produces the following result
*
**
***
****
*****
******
*******
********
*********
**********
Similar post
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.