Results 1 to 3 of 3
- 10-23-2012, 05:45 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 5
- Rep Power
- 0
Not sure what this particular for loop is doing
I do not understand lines 18-21 of the following code. How is j changing value in the for loop that involves k? The only way I could see that happening is if the k loop were nested in the j loop, which it clearly isn't.
Java Code:import java.text.*; import java.util.*; public class HelloWorld { public static void main(String[] args) { //loop iterator int i,j,k; //output the upper triangle, line by line, space first, and then "*" for (i=1;i<=3;i++) { for (j=0;j<3-i;j++) { //System.out.print(' '); } for (k=1;k<=5-2*j;k++) { //System.out.print('*'); } System.out.print('\n'); } //output the lower triangle, line by line, space first, and then "*" //*** // * for (i=1;i<=2;i++) { for (j=1;j<=i;j++) { System.out.print(' '); } for (j=1;j<=5-2*i;j++) { System.out.print('*'); } System.out.print('\n'); } } }
- 10-23-2012, 06:27 PM #2
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Not sure what this particular for loop is doing
The j in the for loop mentioned is not a local to the for loop, it's declared outside of it, so every time the j changes, its value is stored outside of it and it can be used in other loops.
I hope you understand and if I only raised more questions feel free to ask them.
- 10-23-2012, 07:29 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Converting a for loop to a do-while loop
By awesom in forum New To JavaReplies: 1Last Post: 11-23-2011, 03:02 PM -
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 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


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks