Assignment #111: NestingLoops
Code
public class NestingLoops
{
public static void main( String[] args )
{
// this is #1 - I'll call it "CN"
for ( int n=1; n <= 3; n++)
{
for ( char c='A'; c <= 'E'; c++ )
{
System.out.println( c + " " + n );
}
}
// Variable n changes faster when on the inside. When variable c is on the inside, instead of moving to the next letter when a count from 1-3 finishes, the number increases by one when the letter reaches E.
System.out.println("\n");
// this is #2 - I'll call it "AB"
for ( int a=1; a <= 3; a++ )
{
for ( int b=1; b <= 3; b++ )
{
System.out.print( a + "-" + b + " " );
}
//When the code is changed to "println", each iteration prints on a separate line.
System.out.println("0");
//With this line of code, a 0 is printed whenever the first number increases by 1.
}
System.out.println("\n");
}
}
Picture of the output