Nesting of for loops in java
https://www.computersprofessor.com/2016/06/nesting-of-for-loops-in-java.html
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.
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.
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
|