# convert do-while to for-loop

Hi,

I have code which I want to change from do-while to for-loop but it is showing me strange result. Please help me to fix it. I am placing the original code and my attempt, the code is in c language

`````` unsigned int x, y;
const unsigned int x0 = 0U, y0 = 0U;
unsigned int x1, y1;
``````

``````x = x1;
if (getvalue(img) != 0) {
do {
y = y1;
do {
// do something
} while (y-- != y0);

} while (x-- != x0);
}
``````

I tried it like below but it is showing is strange/wrong result

``````x = x1;
for(x=x1;x<=x0;x--)
{
y=y1;
for(y=y1;y<=y0;y--)
{
// do something

}
}
``````
Please let me know how can I fix it.

Thanks
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Given that you are counting down in both loops, your loop condition should be '>=', not '<='. I.e.

``````x = x1;
for(x=x1;x>=x0;x--)
{
y=y1;
for(y=y1;y>=y0;y--)
{
// do something

}
}

``````

Remember, it is not a stop condition, but rather one that allows the loop to continue if it evaluates to 'true'
0
Author Commented:
thanks for response...I changed the <   to ">" but application is crashing now. With do while, it works fine. Please guide me if conditions are correct??
0
Commented:
Hmm, what does 'do something' actually do?
0
Author Commented:
In 'do something', I am using some image processing libraries.  I believe if while loop has no problem then correct for loop should work same. Are you sure my for loop is correct?? I do not have a lot of expertise of C language!
0
Commented:
Yes, the loops should do the same. Could the missing 'if (getvalue(img) != 0)' be an issue?

Regarding the loops, it seems that it shouldn't be  '>=', but rather '>', e.g.

``````for(x=x1;x>x0;x--)
{

for(y=y1;y>y0;y--)
{
// do something

}
}
``````
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Still having trouble?
0
Author Commented:
Loop works but result is not correct. It looks that for loop loops little more or less!!
0
Commented:
Do you have some "real world" values for the loop variables?
0
Author Commented:
thanks
0
Commented:
it is not trivial to turn a do while loop to a for loop, cause the do-while condition was checked at end of loop and the for loop checks at begin. because of that a do-while loop was performed at least one time without check of the condition what might be difficult to achieve with a for loop as the condition always was checked at begin of a loop circle.

hence, the conversion either has to duplicate the code of the loop and put it above the loop to get the same result, or it has to choose start, condition, and step such that the loop also was performed at least once.

in your code there is an additional difficulty as your do-while loops are not safe and easily could run infinitely. if you don't believe me, simply set x1 to x0-1 ...

note, the while condition (x1-- != x0) is bad coding for two reasons. first one is that it requires x1 never to be less than x0 or the loop never ends. the second is that the post-increment operator-- never should be used for the left operand as it makes the statement badly readable then. so the correct condition should be (x0 <= x1--).

so in your case the loops easily could be turned to for loops as shown by jkr's first comment what also would make them safer granted that the x1 and x0 (respectively y1 and y0) are signed integers (for unsigned integers the decrementing of x or y could create an underflow).

Regarding the loops, it seems that it shouldn't be  '>=', but rather '>'

no. the 'do { ... } while (x-- != x0)' would run exactly one loop circle if x1 == x0, same as the 'for (x=x1; x>=x0; --x) { ... }

Sara
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.