Chapter 10
When on loop is placed inside another loop it is called nested loop
for(int i=1;i<5;i++)
{
for(int j=1; j<7; j++)
{
}
}
The control comes out of the inner loop
The control comes out of the second and innermost loop
It takes the control to the update statement in case of for loop and to the condition part of
while and do while loop
for(int i=1; i<10; i++)
{
if(i %2==0) continue;
System.out.print(i + “ “);
}
Output: 1 3 5 7 9
public class MyClass {
public static void main( ) {
for(int i=3; i>=1; i--)
{
for(int j=1; j<=i; j++)
{
System.out.print("$");
}
System.out.println();
}
}
}
class b
{
static void a()
{
for(int i=1; i<=4; i++)
{
for(int j=1; j<=i; j++)
{
System.out.print("& ");
}
System.out.println();
}
}
}
class b
{
static void a()
{
for(int i=1; i<=4; i++)
{
for(int j=1; j<=5; j++)
{
System.out.print("@ ");
}
System.out.println();
}
}
}
class b
{
static void a()
{
for(int i=1; i<=5; i++)
{
for(int j=1; j<=i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
class a
{
static void f()
{
for(int i=5; i>=1; i--)
{
for(int j=1; j<=i; j++)
{
System.out.print(j + “ “);
}
System.out.println();
}
}
}
class d
{
static void f()
{
for(int i=1; i<=5; i++)
{
for(int j=1; j<=i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}
class e
{
static void f()
{
for(int i=1; i<=5; i++)
{
for(int j=5; j>=i; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
class f
{
static void f()
{
for(int i=5; i>=1; i--)
{
for(int j=1; j<=i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}
class g
{
static void f()
{
int k=1;
for(int i=1; i<=4; i++)
{
for(int j=1; j<=i; j++)
{
System.out.print(k++ + " ");
}
System.out.println();
}
}
}
class h
{
static void f()
{
for(int i=10; i<=90; i++)
{
int count = 0;
for(int j=1; j<=i; j++)
{
if(i % j == 0)
count++;
}
if(count == 2)
System.out.println(i);
}
}
}
class i
{
static void f(int m, int n)
{
for(int i=m; i<=n; i++)
{
int count = 0;
for(int j=1; j<=i; j++)
{
if(i % j == 0)
count++;
}
if(count == 2)
System.out.println(i);
}
}
}
class j
{
static void f(int m, int n)
{
for(int i=m; i<=n; i++)
{
int sum = 0;
for(int j=1; j<i; j++)
{
if(i % j == 0)
sum += j;
}
if(sum == i)
System.out.println(i);
}
}
}