Solved

convert do-while to for-loop

Posted on 2014-12-08
190 Views
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
0
Question by:Bertrand Russell
• 5
• 4

LVL 86

Expert Comment

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 Comment

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

LVL 86

Expert Comment

Hmm, what does 'do something' actually do?
0

Author Comment

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

LVL 86

Accepted Solution

jkr earned 500 total points
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

LVL 86

Expert Comment

Still having trouble?
0

Author Comment

Loop works but result is not correct. It looks that for loop loops little more or less!!
0

LVL 86

Expert Comment

Do you have some "real world" values for the loop variables?
0

Author Closing Comment

thanks
0

LVL 32

Expert Comment

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

Featured Post

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.