Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help with manually stepping through these loops

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Help with manually stepping through these loops

    Hey everyone, I was wondering if you could explain the 'best approach' in manually stepping through the following lines of code. It has two loops but the main problem in understanding the code is when the variables change. I want to be able to follow line for line but I loose track of the current value and so I was wondering how you keep track of the updating variables.

    public static int pascalsTriangle(int n){
    	int[][] pt = new int[n][];
     
    	for(int i=0; i<n; i++){
    		pt[i] = new int[i+1];
    		pt[i][0]=1;
     
    		for(int j=1; j<1; j++){
    			pt[i][j] = pt[i-1][j-1]+pt[i-1]+pt[j];
    		}
    		pt[i][i]=1;
    	}
    	return pt;
    }

    This is a method for the pascal triangle. As you can see, the variables of i (in the first loop) and j(in the second loop) are updating. How do you keep track with the changing variables to see what is going on?

    Thanks!


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Help with manually stepping through these loops

    Take a copy and pencil, start from the first line and use your copy page as the stack or heap and your pencil as a pointer to store values in stack and heap. Now keep on tracing the code. Forexample;
    for(int i=0;i<3;i++){
        System.out.println(i);
    }
    First, i must know what for loop do, so i know that it iterates the body until it's condition gets false.
    Now i=0 and condition is if 0<3 that is true, it will go to the body, print the i value, increment the i value to 1 and again check the condition 1<3, true again, body will be executed again, i will be incremented and so on.....

  3. The Following User Says Thank You to Mr.777 For This Useful Post:

    u-will-neva-no (February 21st, 2012)

Similar Threads

  1. Need Help! For loops and if/else
    By kram in forum Loops & Control Statements
    Replies: 2
    Last Post: February 11th, 2012, 06:22 PM
  2. Manually recoloring buffered images
    By nitrogenFingers in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2011, 05:52 PM
  3. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  4. How to change JTree's node icon manually?
    By LeonLanford in forum AWT / Java Swing
    Replies: 2
    Last Post: July 27th, 2010, 03:28 AM
  5. setting cookies manually
    By dotanguy in forum Java Networking
    Replies: 2
    Last Post: July 2nd, 2010, 09:51 AM