Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

convert do-while to for-loop

Posted on 2014-12-08
10
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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 35

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

610 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