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: Java Basic - explanation of for loops

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Basic - explanation of for loops

    class GraphPanel extends Panel
    //GraphPanel is being defined as subclass of Panel
        implements Runnable, MouseListener, MouseMotionListener { //runnable
    //using an interface, allows q method from ore than one superclass
        Graph graph; //attribute  or variable
        int nnodes; //attribute  or variable, primitive data type
        Node nodes[] = new Node[100];
        int nedges; //attribute  or variable intial value
        Edge edges[] = new Edge[200];
        Thread relaxer;
     
        GraphPanel(Graph graph) {
        this.graph = graph;
        addMouseListener(this);
        }
     
     
        int findNode(String lbl) {
        for (int i = 0 ; i < nnodes ; i++) {
    //statement executes if  test returns true
            }
        }

    Just in regards to the for loop, not sure what the i < nnodes is testing. Is it comparing i to the individual value of each nnodes variable, or is it comparing to see whether i is less than the length of nnodes array?
    Last edited by helloworld922; May 24th, 2011 at 06:03 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java Basic - explanation of for loops

    nnodes is an integer variable. It's comparing i to the integer value of nnodes.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Java Basic - explanation of for loops

    The "for" statement does 3 things. First you declare/intantiate something. Then you declare a condition. Finally, you declare an action. In your example, you create an integer 'i', check that the value of 'i' is less than the value of 'nnodes', and if so, add 1 to the value of 'i'. The loop is repeated as long as these conditions are met.

    for(int i=0; i< nnodes; i++)
    {
    //Put code here that you want to be executed every time the conditions of the loop are met.
    }

Similar Threads

  1. [SOLVED] Java assignment, concerning Arrays and Loops
    By trejorchest in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 17th, 2011, 12:59 PM
  2. Java Loops - accumulating totals
    By iv3java in forum Loops & Control Statements
    Replies: 0
    Last Post: December 14th, 2009, 10:10 PM
  3. Java Loops help please?
    By Sarah-Perkin in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2009, 02:52 PM
  4. :!! Unsure how to set this up/ Still learning java loops!!!!!
    By raidcomputer in forum Loops & Control Statements
    Replies: 4
    Last Post: September 29th, 2009, 10:28 AM
  5. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 03:10 AM