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: Problem with For Loop

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with For Loop

    Here is the code
    for (int g=1;g < V+1 ;g++) {
                            System.out.println("For LOOP"+g);
     
                            if (point1 != g){
                                dis = prim.GetDis(point1, g);
                                System.out.println(point1);
                                System.out.println("g "+ g);
                                System.out.println(dis);
                                if (dis > 0){
                                    g = point[p];
                                    p++;
                                }else{}
                            }else{}
    }

    the output is
    For LOOP1
    3
    g 1
    50
    For LOOP1
    3
    g 1
    50
    For LOOP1
    3
    g 1
    50
    For LOOP1
    3
    g 1
    50
    For LOOP1
    3
    g 1
    50

    I don't know why it continue loop for 1, can you please help me to find out the problem?


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Problem with For Loop

    Step through your code.... Add print statements if you need to.

    Get rid of those "else{}" they are unneeded

    Check out the value of point[p] when you are assigning it to g, if point[p] is always 1, you will see the behavior you are seeing now.

    if (dis > 0){
         g = point[p];
         p++;
    }

Similar Threads

  1. [SOLVED] While Loop Problem :(
    By faisalnet5 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2010, 12:23 PM
  2. Problem with loop theory
    By Lord eMO in forum Loops & Control Statements
    Replies: 0
    Last Post: February 11th, 2010, 02:34 PM
  3. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 05:40 AM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 05:31 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 03:31 PM