Transforming for-loop.. i have tried to figure out this so much.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Transforming for-loop.. i have tried to figure out this so much.

  1. #1
    Join Date
    Aug 2004
    Location
    Finland
    Posts
    10

    Question Transforming for-loop.. i have tried to figure out this so much.

    I had this Java exam last week and I didnīt know the answer to this question and it has bothered me since. So if someone could tell me what was the right answer so I can have peaceful mind.

    So the question was:

    Transform the following code into an equivalent do and do-while-loop. (sorry about my English)



    Code:
    int f=0; 
    for(int i=N-1; i >=0; i--) 
    if( a[i] > a[f]) 
    f = i;
    PS. I pass the test but this is driving me crazy.

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    here's a hint, think of the condition part in the FOR loop (i >= 0) as a while condition.

  3. #3
    Join Date
    Aug 2004
    Location
    Finland
    Posts
    10

    Hey i think i got it???

    Is this right answer:


    while:

    int f = 0;
    int i = N - 1;
    while (i >= 0)
    {
    if (a[i] > a[f])
    f = i;
    i--;
    }



    do-while:

    int f = 0;
    int i = N - 1;

    if (i >= 0)
    do {
    if (a[i] > a[f])
    f = i;
    i--;
    } while (i >= 0);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center