NESTED LOOPS
A for Loop can contain another for loop
inside it. This is called nested for loop.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
import [Link].*;
class m
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++) //OFL {row}
{
for(j=1;j<=i;j++) //IFL {Column}
{
[Link](j + " ");
}
[Link]();
}
}
}
Dry Run
i=1 1<=5 True OFL
J=1 1<=1 True IFL print 1
J=2 2<=1 False
out of J Loop ie IFL
Line Change
i=2 2<=5 True OFL
J=1 1<=2 True IFL print 1
J=2 2<=2 TRUE IFL print 2
J=3 3<=2 False
out of J Loop ie IFL
Line Change
i=3 3<=5 True OFL
J=1 1<=3 True IFL print 1
J=2 2<=3 TRUE IFL print 2
J=3 3<=3 True IFL print 3
J=4 4<=3 False
out of J Loop ie IFL
Line Change
i=4 4<=5 True OFL
J=1 1<=4 True IFL print 1
J=2 2<=4 TRUE IFL print 2
J=3 3<=4 True IFL print 3
J=4 4<=4 True IFL print 4
J=5 5<=4 False IFL
out of J Loop ie IFL
Line Change
i=5 5<=5 True OFL
J=1 1<=5 True IFL print 1
J=2 2<=5 TRUE IFL print 2
J=3 3<=5 True IFL print 3
J=4 4<=5 True IFL print 4
J=5 5<=5 True IFl print 5
J=6 6<=5 False IFL
out of J Loop ie IFL
Line Change
i=6 6<=5 False OFL
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
import [Link].*;
class m
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link](i+ " ");
}
[Link]();
}
}
}
Dry Run
i=1 1<=5 True OFL
J=1 1<=1 True IFL print 1
J=2 2<=1 False
out of J Loop ie IFL
Line Change
i=2 2<=5 True OFL
J=1 1<=2 True IFL print 2
J=2 2<=2 TRUE IFL print 2
J=3 3<=2 False
out of J Loop ie IFL
Line Change
i=3 3<=5 True OFL
J=1 1<=3 True IFL print 3
J=2 2<=3 TRUE IFL print 3
J=3 3<=3 True IFL print 3
J=4 4<=3 False
out of J Loop ie IFL
Line Change
i=4 4<=5 True OFL
J=1 1<=4 True IFL print 4
J=2 2<=4 TRUE IFL print 4
J=3 3<=4 True IFL print 4
J=4 4<=4 True IFL print 4
J=5 5<=4 False IFL
out of J Loop ie IFL
Line Change
i=5 5<=5 True OFL
J=1 1<=5 True IFL print 5
J=2 2<=5 TRUE IFL print 5
J=3 3<=5 True IFL print 5
J=4 4<=5 True IFL print 5
J=5 5<=5 True IFl print 5
J=6 6<=5 False IFL
out of J Loop ie IFL
Line Change
i=6 6<=5 False OFL
*
**
***
****
*****
import [Link].*;
class m
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link]("*" + " ");
}
[Link]();
}
}
}
A
AB
ABC
ABCD
AbC D E
import [Link].*;
class m
{
public static void main(String args[])
{
int i,j;
for(i='A';i<='E';i++) // OFL row
{
for(j='A';j<=i;j++) //IFL column
{
[Link]((char)j + " ");
}
[Link]();
}
}
}
(OR)
import [Link].*;
class m
{
public static void main(String args[])
{
int i,j;
for(i=65;i<=69;i++) // OFL row
{
for(j=65;j<=i;j++) //IFL column
{
[Link]((char)j + " ");
}
[Link]();
}
}
}
A
BB
CCC
DDDD
EEEEE
import [Link].*;
class m
{
public static void main(String args[])
{
int i,j;
for(i='A';i<='E';i++) // OFL row
{
for(j='A';j<=i;j++) //IFL column
{
[Link]((char)i + " ");
}
[Link]();
}
}
}
OR
import [Link].*;
class m
{
public static void main(String args[])
{
int i,j;
for(i=65;i<=69;i++) // OFL row
{
for(j=65;j<=i;j++) //IFL column
{
[Link]((char)i + " ");
}
[Link]();
}
}
}
ASCII VALUES
American Standard Code for Information
Interchange
A -65 a=97
B- 66 b=98
: :
: :
Z=90 z=122
Logic:
In nested for loop outer for loop any value inner for loop executes
number of times until the condition in inner for loop become false.
After that increment or decrement in the value of outer for loop
takes place and then again this new value of outer for loop inner
for loop executes again number of times.