Nesting of for loops in java

Nesting of loops, i.e., one for statement with in another for statement is allowed in Java. For ex:–two loops can be nested as follows.

nested for loops


The nesting may continue up to any desired levels. The loops should be properly indented so as to enable the reader to easily determine which statements are contained with n each for statement.


Display right angle triangle of @ using nested for loop :

  class nested loop.
    {
      public static void main (String a [ ] )
       {
            int p, q ;
            System.out.println (“The right angle triangle of @ is shown below : \n”)
            for (p = 1 ; p < 5 ; p + + )
            {
             for (q = 1 ; q < = p ; q + +)
             {
             System.out.println (“@”) ;
             }
            System.out.println ( “ “ ) ;
            }
         }
      }

O/P : The right angle triangle of @ is shown below.

            @
            @ @
            @ @ @
            @ @ @ @
            @ @ @ @ @

Related

Java 8290257822762023370

Post a Comment

emo-but-icon

item