# Multiple nested loops in a single code

Page 1 of 1

## 6 Replies - 1189 Views - Last Post: 20 October 2012 - 09:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=296417&amp;s=f5126d0abcf28f4e739404458a44bdd1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 vhodges22

Reputation: 0
• Posts: 6
• Joined: 20-October 12

# Multiple nested loops in a single code

Posted 20 October 2012 - 09:20 AM

The program is asking to print two asterisk pyramids (upside-down). I assumed this would mean two nested loops. However, when I add the second nested loop in for the second pyramid, it messes up my output for my first pyramid. Is there some way that I can split the two up so that both loops will run properly? Here is my code: The first pyramid starts off with 7 asterisks and the second starts off with 4.
[
public class Program165a
{
public static void main (String [] args)
{

System.out.println("Enter the starting size: 7");

//This is for the first shape.
//x represents the *
for (int x = 7; x >= 0; x--) //Use - so it will count backwards and be upside-down.
{
for (int y = 0; y < x; y++)
{
System.out.print("*");
}
System.out.println();
System.out.println("Enter the starting size: 4");

for (x = 4; x >= 0; x--) //Use - so it will count backwards and be upside-down.
{
for (int b = 0; b < x; b++)
{
System.out.print("*");
}
System.out.println();
}
}
}
}
]

Is This A Good Question/Topic? 0

## Replies To: Multiple nested loops in a single code

### #2 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:24 AM

What's the desired shape? Build it inside code tags so the spacing will be preserved.

Oh, and post your code inside code tags.

### #3 vhodges22

Reputation: 0
• Posts: 6
• Joined: 20-October 12

## Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:27 AM

[Enter the staring size: 7
*******
******
*****
****
***
**
*

Enter the starting size: 4
****
***
**
*]
This is what the output should look like.

### #4 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:38 AM

Still not sure you're showing us correctly, but I'll work with what you've posted.

Notice there are the same number of rows as the starting size, AND the same number of stars in the first row as the starting size. Each subsequent row has one less star.

To program, you need one loop that controls the rows and another loop that controls the number of stars in each row:
```int startingSize = 7;

//This is for the first shape.
for (int rows = startingSize; rows > 0; rows--) //Use - so it will count backwards and be upside-down.
{
// rows decreases each time through the loop
for ( int y = rows ; y > 0 ; y-- )
{
System.out.print("*");
}
System.out.println();
}
```

Then to vary the output, just change the starting size.

### #5 vhodges22

Reputation: 0
• Posts: 6
• Joined: 20-October 12

## Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:38 AM

The first nested loop will compile with the correct shape, but when I add the second one it messes up the first. Im not sure how to fix this, and I'm new to programming so could you please tell me a method that would not be too complicated?

### #6 vhodges22

Reputation: 0
• Posts: 6
• Joined: 20-October 12

## Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:43 AM

Ok thank you for your help!

### #7 GregBrannon

• D.I.C Lover

Reputation: 2215
• Posts: 5,240
• Joined: 10-September 10

## Re: Multiple nested loops in a single code

Posted 20 October 2012 - 09:46 AM

You mentioned method, and putting the loops into a method that prints the pyramids is a good idea, if you meant method in Java terms. Then you could print the desired pyramids by calling the method:

printPyramid( 7 );

and

printPyramid( 4 );

where 7 and 4 are the starting sizes.