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: Having trouble with for loop statement.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Having trouble with for loop statement.

    I am doing an exercise in a book and there is a part of the code that I don't understand. To my understanding, a for loop always has three parts, initialization, condition, and iteration. Yet, in the code below, the for loop has none of the three. Although it does have two semicolons inside the parentheses, which makes me guess that the three parts are left blank, I still do not understand how any comparison, initialization or iteration is done without any values entered. Please explain to me how the loop works with the parts left blank. The code is below. I did not include any of the class definitions only the method calls.

    Thanks,
    Kevin Haynes



    class HelpClassDemo {
      public static void main(String args[])
        throws java.io.IOException {
        char choice, ignore;
        Help hlpobj = new Help();
     
       for(;;) {
          do {
            hlpobj.showmenu();
     
            choice = (char) System.in.read(); 
     
            do { 
               ignore = (char) System.in.read(); 
             } while(ignore != '\n'); 
     
          } while( !hlpobj.isvalid(choice) );
     
          if(choice == 'q') break;
     
          System.out.println("\n");
     
     
          hlpobj.helpon(choice);
        }
      }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Having trouble with for loop statement.

    This

    for (;;) {
     
    }

    is the for-loop equivalent to

    while (true) {
      //...
    }

    It loops forever, or until a break statement breaks out of the loop..

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    Truck35 (November 24th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Having trouble with for loop statement.

    yeah......for( ; ; ) is an infinite loop.....u can use it like...
    for( ; ; )
    {
    if(somecondition) break;
    }

    it means until and unless that condition is satisfied loop runs

  5. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Having trouble with for loop statement.

    yeah......for( ; ; ) is an infinite loop.....u can use it like...
    for( ; ; )
    {
    if(somecondition) break;
    }

    it means until and unless that condition is satisfied loop runs

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. [SOLVED] Trouble with my while loop
    By masterhand89 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 17th, 2012, 12:45 PM
  3. How do I loop this if statement?
    By dunnage888 in forum Loops & Control Statements
    Replies: 5
    Last Post: February 10th, 2012, 12:16 PM
  4. Nested loop if statement help
    By CSUTD in forum Loops & Control Statements
    Replies: 7
    Last Post: November 8th, 2011, 02:05 PM
  5. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM