Nested while loop in java
Placing one while loop with in the body of another while is called Nested while loop in java programm.
CODE
DESCRIPTION
Here outer loop is executed for values going from 1 till less than 3 i.e. 1 and 2. Similarly inner loop is executed from 5 till less than 8 i.e. 5, 6 and 7. So when outer is 1, inner will become 5, 6, and 7 and when outer is 2, then also inner will become 5, 6, and 7.
CODE
public class NestedWhileExample
{
public static void main(String arg[])
{
int outer = 1;
while(outer < 3)
{
int inner = 5;
while(inner < 8)
{
System.out.println(outer + " " + inner);
inner++;
}
outer++;
}
}
}
Output :
1 5
1 6
1 7
2 5
2 6
2 7
DESCRIPTION
Here outer loop is executed for values going from 1 till less than 3 i.e. 1 and 2. Similarly inner loop is executed from 5 till less than 8 i.e. 5, 6 and 7. So when outer is 1, inner will become 5, 6, and 7 and when outer is 2, then also inner will become 5, 6, and 7.
Comments
Post a Comment