Solved

convert do-while to for-loop

Posted on 2014-12-08
10
190 Views
Last Modified: 2014-12-10
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;

Open in new window


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

Open in new window


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
	
			}
		}

Open in new window

Please let me know how can I fix it.

Thanks
0
Comment
Question by:Bertrand Russell
  • 5
  • 4
10 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
	
			}
		}
                                  

Open in new window


Remember, it is not a stop condition, but rather one that allows the loop to continue if it evaluates to 'true'
0
 

Author Comment

by:Bertrand Russell
Comment Utility
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

by:jkr
Comment Utility
Hmm, what does 'do something' actually do?
0
 

Author Comment

by:Bertrand Russell
Comment Utility
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

by:
jkr earned 500 total points
Comment Utility
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
	
			}
		}

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
Still having trouble?
0
 

Author Comment

by:Bertrand Russell
Comment Utility
Loop works but result is not correct. It looks that for loop loops little more or less!!
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Do you have some "real world" values for the loop variables?
0
 

Author Closing Comment

by:Bertrand Russell
Comment Utility
thanks
0
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now