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: Predicting the Outcome without a compiler **Not a Homework Question**

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Predicting the Outcome without a compiler **Not a Homework Question**

    I am trying to understand the out come of the following problem:

    public class Whatever {
        public static void main(String[] args) {
     
     for(int i = 1; i < 5; i++) {
     
                for(int j = 1; j < i; j++) {
     
                        System.out.print(i + ",");
                    }
                System.out.println();
                    }    
            }
    }

    I understand the concept of j = 1 and i = 1 and how the output will be the following:
    2,
    3,3,
    4,4,4,

    what I don't understand is if I change j = 0 and leave i = 0 the result is the following:
    1,
    2,2,
    3,3,3,
    4,4,4,4,

    can someone please explain why this occurs?

    Thank you in advance,


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Predicting the Outcome without a compiler **Not a Homework Question**

    Play computer with the code. Evaluate each statement using the current value of the variables. Do the statements one by one. Use paper and pencil to keep track of the values of variables.
    If you have a question about a statement, post the question and the values of the variables used in that statement when the statement is executed.

    Review how for loops work.
    http://docs.oracle.com/javase/tutori...bolts/for.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Homework Question
    By EPC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 24th, 2013, 01:33 PM
  2. Question about homework problem.
    By Rain_Maker in forum Java Theory & Questions
    Replies: 13
    Last Post: February 7th, 2013, 08:11 PM
  3. [Help] For Loops - Number Predicting Program
    By iAce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 13th, 2012, 04:05 PM
  4. A For loop that calculates every possible outcome for 2 AND 3 chars...
    By MyNamesMatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2012, 10:59 AM
  5. [SOLVED] What in SWINGS name do i use to get this outcome?!
    By JonLane in forum AWT / Java Swing
    Replies: 2
    Last Post: February 25th, 2012, 04:55 PM