|
-
April 25th, 2009, 07:17 PM
#1
Loop Imitations
Hi!
As some of you may know, I'm currently learning C++ and would like to know something; I know that many of the loops in C/C++ can be represented using a regular while loop. In the following examples, please correct me if I'm wrong:
Representing a while-statement using a do-statement
Code:
do
{
if (/* condition */)
{
// ...
}
else
break;
} while (true);
Representing a for-statement using a do-statement
Code:
int i = 0;
do
{
if (i < MAX)
{
// ...
i++;
}
else
break;
} while (true);
Representing a while-statement using a for-statement
Code:
for (; /* condition */;)
{
// ...
}
Representing a do-statement using a for-statement
Code:
// the statements go here...
for (; /* condition */;)
{
// same statements as above...
}
These are my guesses at the moment. Please feel free to correct me if I happened to make a mistake in any of those or if you see that any of them are inefficient (say, if I included something that was not necessary and there is a better way to represent the same thing, etc.)
Thanks!
-
April 25th, 2009, 07:24 PM
#2
Re: Loop Imitations
Of course, any of these loops can be done using just 'if' and 'goto'. We have all these different loop structures because it makes it a little bit easier on the programmer
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
April 25th, 2009, 11:48 PM
#3
Re: Loop Imitations
I think the most common loop that I use is
Code:
while(true){
...break;
}
and
Code:
for(unsigned int = 0; i < whatever; i++){
}
I really don't think there is a difference in performance, I think by the time they hit the abstract parse tree, they are the same. The only limitation that I can think of is that for loops and such are limited by the type that you use as the iterator.
-
April 26th, 2009, 08:36 AM
#4
Re: Loop Imitations
 Originally Posted by EsX_Raptor
Representing a do-statement using a for-statement
Code:
// the statements go here...
for (; /* condition */;)
{
// same statements as above...
}
This way you have to write the same code twice. You can also write a do-loop as a for-loop and keep the body the same.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
April 26th, 2009, 11:16 AM
#5
Re: Loop Imitations
 Originally Posted by ninja9578
I think the most common loop that I use is
Code:
while(true){
...break;
}
and
Code:
for(unsigned int = 0; i < whatever; i++){
}
I really don't think there is a difference in performance, I think by the time they hit the abstract parse tree, they are the same. The only limitation that I can think of is that for loops and such are limited by the type that you use as the iterator.
while(true) isn't a really good habit to get into. It's better practice to put a condition in the while statement rather than rely on a break somewhere in the body.
-
April 26th, 2009, 11:27 AM
#6
Re: Loop Imitations
I know, I do that whenever I can, but I've found myself recently having multiple situations that can break the loop, or I have code that has to be placed at the end of the loop that I don't want to run. Don't worry, I stress test it and make sure that there is no way for it to run infinately.
-
April 26th, 2009, 11:28 AM
#7
Re: Loop Imitations
 Originally Posted by GCDEF
while(true) isn't a really good habit to get into. It's better practice to put a condition in the while statement rather than rely on a break somewhere in the body.
Generally, I agree (though I prefer for (;;) ), but in some cases such a refactoring (particularly to convert to a do while loop) requires pulling out code into a function, which may be overkill.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|