DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

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

1. Registered User
Join Date
Aug 2004
Location
Finland
Posts
10

## 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. Senior Member
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. Registered User
Join Date
Aug 2004
Location
Finland
Posts
10

## Hey i think i got it???

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
•

 FAQ Latest Articles Java .NET XML Database Enterprise