Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 123079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:13:48+00:00 2026-05-11T04:13:48+00:00

When I use an Iterator of Object I use a while loop (as written

  • 0

When I use an Iterator of Object I use a while loop (as written in every book learning Java, as Thinking in Java of Bruce Eckel):

Iterator it=...  while(it.hasNext()){     //... } 

but sometime i saw than instead somebody use the for loop:

Iterator it=... for (Iterator it=...; it.hasNext();){     //... } 

I don’t’ understand this choice:

  • I use the for loop when I have a collection with ordinal sequence (as array) or with a special rule for the step (declared generally as a simple increment counter++).
  • I use the while loop when the loop finishes with I have’nt this constraints but only a logic condition for exit.

It’s a question of style-coding without other cause or it exists some other logic (performance, for example) that I don’t’ know?

Thanks for every feedback

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:13:48+00:00Added an answer on May 11, 2026 at 4:13 am

    The correct syntax for the for loop is:

    for (Iterator it = ...; it.hasNext(); ){     //... } 

    (The preceding declaration in your code is superfluous, as well as the extra semicolon in the for loop heading.)

    Whether you use this syntax or the while loop is a matter of taste, both translate to exactly the same. The generic syntax of the for loop is:

    for (<init stmt>; <loop cond>; <iterate stmt>) { <body>; } 

    which is equivalent to:

    <init stmt>; while (<loop cond>) { <body>; <iterate stmt>; } 

    Edit: Actually, the above two forms are not entirely equivalent, if (as in the question) the variable is declared with the init statement. In this case, there will be a difference in the scope of the iterator variable. With the for loop, the scope is limited to the loop itself, in the case of the while loop, however, the scope extends to the end of the enclosing block (no big surprise, since the declaration is outside the loop).

    Also, as others have pointed out, in newer versions of Java, there is a shorthand notation for the for loop:

    for (Iterator<Foo> it = myIterable.iterator(); it.hasNext(); ) {     Foo foo = it.next();     //... } 

    can be written as:

    for (Foo foo : myIterable) {     //... } 

    With this form, you of course lose the direct reference to the iterator, which is necessary, for example, if you want to delete items from the collection while iterating.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 196k
  • Answers 196k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To start with the obvious: Know your requirements and tools.… May 12, 2026 at 7:12 pm
  • Editorial Team
    Editorial Team added an answer I think it's best to have a whole bunch of… May 12, 2026 at 7:12 pm
  • Editorial Team
    Editorial Team added an answer Use stuff.Add(i) instead of that, you accessing not an array… May 12, 2026 at 7:12 pm

Related Questions

I'm looking for a class in java that has key-value association, but without using
I'm having problems with Iterator.remove() called on a HashSet. I've a Set of time
I have the following issue related to iterating over an associative array of strings
Okay, here's one for the pro's: For a couple of years now, i've been

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.