Pages

 Java Fundamentals part-5

Program to demonstrate while loop:

class Demo

{

          public static void main(String args[])

{

          int i=1;

          while(i<=10)

          {

                     System.out.println(i);

                     i++;

          }

}

}

Program to demonstrate do while loop:

class Demo

{

          public static void main(String args[])

{

          int i=1;

          do

          {

                     System.out.println(i);

                     i++;

          }while(i<=10);

}

}

Program to demonstrate for loop:

class Demo

{

          public static void main(String args[])

{

          for(int i=1;i<=10;i++)

          {

                     System.out.println(i);

          }

}

}

Program to demonstrate nested loops:

class Demo

{

          public static void main(String args[])

{

          for(int i=1;i<=3;i++)

          {

                     for(int j=1;j<=10;j++)

                     {

System.out.println(j);

                     }

          }

}

}

Break Statement:

It terminates the nearest enclosing loop or switch statement.

Example:

class Demo

{

          public static void main(String args[])

{

          for(int i=1;i<=10;i++)

          {

                     if(i==5)

                               break;

                     System.out.println(i);

          }

}

}

Break LABEL Statement:

It terminates the specified LABEL loop.

Example:

class Demo

{

          public static void main(String args[])

{

          FIRST:for(int i=1;i<=3;i++)

          {

                     SECOND:for(int j=1;j<=10;j++)

                     {

                               if(j==5)

                                         break FIRST;

                               System.out.println(j);

                     }        

          }

}

}

 

Continue Statement:

It passes the control to the next iteration of a loop.

Example:

class Demo

{

          public static void main(String args[])

{

          for(int i=1;i<=10;i++)

          {

                     if(i==5)

                               continue;

                     System.out.println(i);

          }

}

}

Continue LABEL Statement:

It passes the control to the next iteration of specified LABEL loop.

Example:

class Demo

{

          public static void main(String args[])

{

          FIRST:for(int i=1;i<=3;i++)

          {

                     SECOND:for(int j=1;j<=10;j++)

                     {

                               if(j==5)

                                         continue FIRST;

                               System.out.println(j);

                     }        

          }

}

}

 

 

By

        Mr. Venkatesh Mansani

No comments:

Post a Comment