DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  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);

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links