Solved

convert do-while to for-loop

Posted on 2014-12-08
10
211 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
ID: 40487451
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
ID: 40487498
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
ID: 40487533
Hmm, what does 'do something' actually do?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:Bertrand Russell
ID: 40487572
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
ID: 40487589
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
 
LVL 86

Expert Comment

by:jkr
ID: 40487878
Still having trouble?
0
 

Author Comment

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

Expert Comment

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

Author Closing Comment

by:Bertrand Russell
ID: 40489920
thanks
0
 
LVL 33

Expert Comment

by:sarabande
ID: 40491005
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

813 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

16 Experts available now in Live!

Get 1:1 Help Now