Hi,
Here is a collection of few examples for NESTED FOR LOOP.
Simple codes in how to use nested loops,
There is only the code, no explanation (not good at explaining anything:D)
So, just try to understand the program
Hope this will help as much people as possible
Let's Stop talking and just begin
OK
Here we go!
1.Get the following output :
a.
1
12
123
1234
12345
123456
1234567
12345678
123456789
//Numbers Ladder
//Nested Loops
//By ALPHA
//On 22 Oct 2008
//www.CodeCall.net
//----------------------
public class testFor
{
public static void main(String [] args)
{
for (int i=1; i<=9; i++)
{
System.out.println();
for (int j=1; j<=i; j++)
{
System.out.print(j);
}
}
System.out.println();
}
}
b.
*
**
***
****
*****
******
*******
********
*********
(just bring it to the LEFT side just like it is)
Star Pattern using nested for loop
//Nested Loops
//star ladder
//www.CodeCall.net
//By ALPHA
//22 Oct 2008
//--------------
public class starForTest
{
public static void main(String [] args)
{
for (int i=1; i<=9; i++)
{
System.out.println();
for (int j=9; j>=i; j--)
{
System.out.print(" ");
}
for (int k=1; k<=i; k++)
{
System.out.print("*");
}
}
System.out.println();
}
}
2.A program for sorting an array elements (ascending sort)
//Nested For Loop
//Sorting elements of an Array
//By ALPHA
//23 Oct 2008
//www.CodeCall.net
//------------------------
public class arrSort
{
public static void main(String args [])
{
int num[]=new int[] {20,50,10,70,40,80,30,90,60};
int t=0;
for (int i=0; i<num.length; i++)
{
for (int j=i+1; j<num.length; j++)
if (num[i]>num[j])
{
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
System.out.println("Sorting elements in Ascending Order :\n");
for (int i=0; i<num.length; i++)
System.out.println(num[i]);
}
}
3.Program to create a Two-Dimensional Array
//Two Dimensional Array
//Nested Loops
//By ALPHA
//On 23 Oct 2008
//www.CodeCall.net
//-------------------------------
public class twoDimArray
{
public static void main(String [] args)
{
String city[][]={ {"NewYork","Muscat","London"} , {"Cairo","Beijing","CapeTown"} };
/* Note : hope u already know why
* i used Double Qoutes( " ) when
* initializing the Array elements
*/
for (int i=0; i<city.length; i++)
{
for (int j=0; j<city[i].length; j++)
//Note:we could use 2 and 3 instead of "city.length" and "city[i].length
//'Cause we already know that we have 2 Rows and 3 Columns ^_^
{
System.out.print(city[i][j]+"\t");
}
System.out.println();
}
}
}
Java - Take For Loop Variables And Put Into 2 Separate Array
4.Program to create multiplication table
//Multiplication Table
//Using Nested Loops
//By ALPHA
//23 October 2008
//www[dot]CodeCall[dot]net
//-----------------------------------
/*Important Note : this program includes Parts that
* are ONLY for the shape of the OUTPUT
* it will NOT be an ERROR if you
* missed those Parts but, as said it's only for
* the shape of the output
*/
//------------------------------------
public class multiplicationTable
{
public static void main(String args[])
{
System.out.print(" ");
for (int i=1; i<=9; i++)
System.out.print(" "+i);
System.out.println();
System.out.println("-------------------------------------------------------");
for (int i=1; i<=9; i++)
{
System.out.print(i+"|");
for (int j=1; j<=9; j++)
{
if (i*j<10)
System.out.print(" "+i*j);
else
System.out.print(" "+i*j);
}
System.out.println();
}
}
}
That's all I have for nested loops
If anyone has any other ideas, PLEASE post it !
c u ![]()
ALPHA
Looking for more tutorials on loops?
Java - Take For Loop Variables And Put Into 2 Separate Array
Edited by Roger, 25 February 2013 - 05:06 PM.

















