Click to See Complete Forum and Search --> : "Repeat exercise 1, but this time use TWO FOR LOOPS, one nested within the other."


s3a
September 24th, 2009, 07:46 PM
On my programming assignment, here is the code I used to answer question 1:

public class Q1
{
public static void main (String [] args)
{

for(int number = 1; number <= 100; number += 1)
{
System.out.printf("%2d ", number);
if((number%10) == 0)
System.out.println();
}


}
}

"Repeat exercise 1, but this time use two for loops, one nested within the other."

Can someone please give me an example of how to do this?

Any help would be greatly appreciated!
Thanks in advance!

Deliverance
September 24th, 2009, 08:41 PM
here's an example:


public class Q1 {
public static void main (String [] args) {

for(int i = 0; i < 4; i++) {
for(int k = 0; k < 12; k++) {
System.out.print("I am a nested loop ");
}
System.out.println("Outer loop ");
}
}
}


Do you get what is happening on paper?

For each outer loop that is going to execute 4 times the following will happen:
For the next 12 times, print out that line <-- (now one outer loop finished)

so in this case, you would get 48 print statements. You were after 100. Figure it out :)