Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 23rd, 2012, 02:47 PM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Question Trouble Understanding for loop.

I am doing an execise in a book and there is a part of the code that I don't understand. To my undrstanding, a for loop always has three parts, initialization, condition, and iteration. Yet, in the code below, the for loop has none of the three. The code is written as follows "for(,,){" Although it does have two commas 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. Please explaine to me how the loop works with the parts left blank. the code is below. I did not include any of the class definentions only the method calls. I put the code in question in bold and Italcs.

Thanks,
Kevin Haynes

Code:
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);
    }
  }
}
 
Old April 10th, 2014, 12:17 PM
Registered User
 
Join Date: Apr 2014
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello,

It's quite simple mate.

As you said: "a for loop always has three parts, initialization, condition, and iteration."
This means that this -> for(;;), has a infinite initialization, condition and iteration.
This program will only exit in this bit:
Code:
    if(choice == 'q') break;
This means that the for cycle will be forced to break.


Cheers.





Similar Threads
Thread Thread Starter Forum Replies Last Post
For loop with Floating Point Loop Counter - pp. 150 shivesh BOOK: Ivor Horton's Beginning Visual C++ 2010 1 March 28th, 2012 09:31 AM
Having trouble understanding Range Variable Notso BOOK: Beginning ASP.NET 4 : in C# and VB 3 March 17th, 2012 04:24 PM
Loop twice, then inside loop select nodes?? JohnBampton XSLT 2 March 9th, 2009 05:21 AM
Trouble Understanding the BLL jachin BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 7 March 13th, 2008 12:23 AM
for loop trouble (beginner) cdbabcock C++ Programming 1 March 5th, 2006 04:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.