Need a clarification in the "for" loop

Discussion in 'Java' started by Praveen, Jan 21, 2008.

  1. Praveen

    Praveen Guest

    Hi,

    I came across the following piece of code in java

    String []a;
    String []b;

    for (String x:a, String y:b) {
    // do something

    }

    Is the above piece of code correct?
    If so can anyone explain me what does it do?
     
    Praveen, Jan 21, 2008
    #1
    1. Advertising

  2. Praveen

    Jack Guest

    Hi,

    can't you try by yourself ? Juste oaste the code in any java file!!

    Eclispe reject your code, so seems it's incorrect (with jdk1.5).


    Praveen a écrit :
    > Hi,
    >
    > I came across the following piece of code in java
    >
    > String []a;
    > String []b;
    >
    > for (String x:a, String y:b) {
    > // do something
    >
    > }
    >
    > Is the above piece of code correct?
    > If so can anyone explain me what does it do?
     
    Jack, Jan 21, 2008
    #2
    1. Advertising

  3. Jack <> wrote:
    >> I came across the following piece of code in java
    >> String []a; String []b;
    >> for (String x:a, String y:b) { /* do something */ }
    >> Is the above piece of code correct?

    > can't you try by yourself ? Just paste the code in any java file!!
    > Eclipse reject your code, so seems it's incorrect (with jdk1.5).


    Just a sidenote: Perhaps someone mixed up tcl with java here,
    since tcl's foreach-loop does support parallel iteration of
    multiple lists.

    Perhaps the code was actually a sample for a "wishlist item" :)
     
    Andreas Leitgeb, Jan 21, 2008
    #3
  4. Praveen

    Praveen Guest

    Thanks got it!!
     
    Praveen, Jan 21, 2008
    #4
  5. Praveen

    Lew Guest

    (Top-posting corrected. Please do not top-post.)

    Praveen a écrit :
    >> I came across the following piece of code in java
    >>
    >> String []a;
    >> String []b;
    >>
    >> for (String x:a, String y:b) {
    >> // do something
    >>
    >> }
    >>
    >> Is the above piece of code correct?


    No, as others have mentioned.

    >> If so can anyone explain me what does it do?


    Jack wrote:
    > can't you try by yourself ? Juste oaste the code in any java file!!
    >
    > Eclispe reject your code, so seems it's incorrect (with jdk1.5).


    One could always check out the JLS, since we know Eclipse can and does report
    as errors things that are actually legal Java.
    <http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2>
    > The enhanced for statement has the form:
    >
    > EnhancedForStatement:
    > for ( VariableModifiersopt Type Identifier: Expression) Statement


    QED.

    Language syntax questions can pretty much always be resolved by reference to
    the JLS. Library questions can often be resolved by reference to the Javadocs.

    Faster, more completely and more reliably than by Usenet.

    --
    Lew
     
    Lew, Jan 21, 2008
    #5
  6. Praveen

    Roedy Green Guest

    On Mon, 21 Jan 2008 01:19:42 -0800 (PST), Praveen
    <> wrote, quoted or indirectly quoted someone
    who said :

    >for (String x:a, String y:b) {


    I think that was not written in Java.
    --
    Roedy Green, Canadian Mind Products
    The Java Glossary, http://mindprod.com
     
    Roedy Green, Jan 21, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prem
    Replies:
    1
    Views:
    1,636
    Shankara Narayanan
    Nov 3, 2004
  2. HDSkiFreak
    Replies:
    2
    Views:
    3,590
    HDSkiFreak
    May 11, 2004
  3. Krishna
    Replies:
    5
    Views:
    1,821
    Howard
    Apr 1, 2005
  4. Tool69
    Replies:
    2
    Views:
    302
    Tool69
    Dec 15, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    460
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page