Day16: Nested Loop

 Nested Loop

In the nested loop, loop are going inside loop whose syntax are given below:

for(initializer; test_cond; increment or decrement)
{
    for(initializer; test_cond; increment or decrement)
        {
            // loop body
        }
   //loop body
}

Example : 

class Nestloop
{
public static void main(String [ ] args)
{
for(int i = 1; i<=3; i++)
{
for(int j = 10; j<=15; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}


Labelled Break;

class Nestloop
{
public static void main(String [ ] args)
{
bhopal:
for(int i = 1; i<=3; i++)
{
for(int j = 10; j<=15; j++)
{
System.out.print(j+" ");
if(j==14)
{
break bhopal;
}
}
System.out.println();
}
System.out.println("Thank You");
}
}


Exit:

class Nestloop
{
public static void main(String [ ] args)
{
bhopal:
for(int i = 1; i<=3; i++)
{
for(int j = 10; j<=15; j++)
{
System.out.print(j+" ");
if(j==14)
{
System.exit(0);
}
}
System.out.println();
}
System.out.println("Thank You");
}
}

In the case of Exit loop Exit when the required condition will met i.e., In the above code Thank you will not print.




Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?