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

Thread: Question about do-while loops?

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Question about do-while loops?

    Hi guys! There was a question on an old lab test that I got incorrect, and I was wondering if someone could clear it up for me?


    int i=0;
    do {
    int j =1;
    do {
    System.out.print("a");
    j++;
    }
    while(j < i);
    System.out.println();
    i++;
    }
    while (i < 5);



    The answer is:
    a
    a
    a
    aa
    aaa


    So, I understand what it's asking and how it's supposed to work, except I'm having trouble with the while(j<i). Isn't j ALWAYS greater than i, since i=0 was stated in the beginning?

    Scorks


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    606
    My Mood
    Sleepy
    Thanks
    10
    Thanked 90 Times in 83 Posts

    Default Re: Question about do-while loops?

    Hi, please use code tags when posting code, i.e. [code]your code goes here [/code]
    j is not always greater than i because j is reset to 1 in every run of the outer loop, but i is incremented in every run of the same loop. So at one point i is 3 and j is 1, that's where the inner loop starts running more than once.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Question about do-while loops?

    It looks like this is an experiment in the complications of nested loops. I would suggest taking out the prints that are there and adding new prints that tell you what loop you are in and the values of i and j. This way you can see exactly how the loops are being executed and how the counters are incrementing and effecting them.

    Example:
    System.out.println("Inner Loop i=" + i + " j=" + j);
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Java Loops Question
    By ThatBeast in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2012, 10:42 PM
  2. Question on do-while loops
    By Sean448 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 24th, 2012, 01:06 AM
  3. Beginner question about for loops...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 02:57 PM
  4. Loops & Series Question
    By nuttaay in forum Loops & Control Statements
    Replies: 5
    Last Post: November 29th, 2011, 12:46 PM
  5. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM

Tags for this Thread