Results 1 to 6 of 6
Thread: Need help analysising for loop
- 05-09-2011, 11:56 AM #1
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Need help analysising for loop
im having a tough time understanding the for loop in this code and what the limits of the loop are. I need help breaking the code down and understanding a for loop.
[CODE]class Diamond {
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
}
/*
*
***
*****
*******
*********
*******
*****
***
*
*/[CODE/]
- 05-09-2011, 02:25 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
What is your question? exactly what is it you are stuck on or don't understand?
- 05-09-2011, 03:01 PM #3
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
I don’t understand the code. You see what i posted for the code was given to us in class. i was told to learn how the code works and how to change it so you get different shapes. Like to how to change it from a diamond to just a triangle. But i don’t understand how the for loop works and what its limits are. Because i figured out that to get a triangle all i need is a for loop with different limits.
- 05-09-2011, 08:55 PM #4
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
Add System.out.println for both of the loop variables ( 'i' and 'j' ). Seeing how they change will help you understand the loops and see exactly how each of the loops exits.
- 05-09-2011, 09:16 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Read the following closely: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
- 05-09-2011, 10:43 PM #6
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
Convert do while loop to for loop
By sandeeptheviper in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:37 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks