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 4 of 4

Thread: help with trace tables for loops

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with trace tables for loops

    For my java homework I have to provide a trace table for the following loop:

    int i = 10; int j = 0; int n = 0;
    while (i > 0) { i--; j++; n = n + i - j; }

    When I put it into eclipse I get the answer:
    i= 0
    j= 10
    n= -10

    but when I try to trace it by hand I get
    i j n i>0
    10 0 0 t
    9 1 8 t
    8 2 14 t
    7 3 18 t
    6 4 20 t
    5 5 20 t
    4 6 18 t

    and so on.... Can someone please tell me why eclipse only prints out the last line? It didn't with any of the other trace tables I was trying to check my work on


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help with trace tables for loops

    Please show the whole while statement and any other info you think would be helpful to answer your question.

    BTW - If your while statement is formatted in your code as it is above, add the expected white space to make it readable before bothering to post it. Most won't bother even reading code formatted as you've shown.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with trace tables for loops

    Sorry, that's just the way it looked in the book

       public class Practice {
     
    	public static void main(String[] args) {
     
    		int i = 10;
    		int j = 0;
    		int n = 0;
     
    		{
    			while (i > 0) {
    				i--;
    				j++;
    				n = n + i - j;
    			}
     
    		}
     
    		System.out.println(i);
    		System.out.println(j);
    		System.out.println(n);
     
    	}
    }

    eclipse just prints out:
    0
    10
    -10

    But when I do it by hand this is what my table looks like:

    i j n i>0
    10 0 0 T
    9 1 8 T
    8 2 14 T
    7 3 18 T
    6 4 20 T
    5 5 20 T
    4 6 18 T
    3 7 14 T
    2 8 8 T
    1 9 0 T
    0 10 -10 F

    (sorry ^ I can't get my table to stay formatted with spaces)

    On the other while statements I put into eclipse to check my work, it printed out the whole table, not just the last line, so I'm wondering if I'm doing something wrong?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help with trace tables for loops

    Thank you. Much better.

    In order for the print commands to be executed multiple times and therefore printing multiple results, they must be inside the while loop. The fact that only one line prints should be expected from the code you've posted. (Why the extra '{ }'?)

    Then, if the 3 results are to be printed on a single line, don't use println() for the first two print statements.

    There will be other formatting cleanups to do, but if you make the above changes, you'll be on your way.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    catisch (October 19th, 2013)

Similar Threads

  1. help make trig values tables by using while or for loops
    By my21 in forum Loops & Control Statements
    Replies: 6
    Last Post: March 9th, 2013, 07:41 PM
  2. What does "play computer" hand trace segment of code mean?
    By michael305rodri in forum Java Theory & Questions
    Replies: 1
    Last Post: October 16th, 2012, 10:30 AM
  3. [SOLVED] how to trace recursion variables in a recursive method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 23rd, 2012, 12:00 PM
  4. Recursion Trace
    By hello_world in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 22nd, 2011, 10:35 PM
  5. Data tables and such like.
    By ShaunB in forum Java Theory & Questions
    Replies: 1
    Last Post: December 30th, 2009, 06:10 PM