Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Plexmark

Concept thinking to develop loops

This topic is 5601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently trying to learn C++ and the most trouble I am having is not learning the syntax or anything like that, I can understand it and read code just fine. It is the process of developing loops. I was wondering if anyone could share there methods for when they see a problem what they do to develop the loop. I am just doing as many questions dealing with loops I can find and am hoping through practice it starts to click when I see stuff. But just incase there is a method of breaking stuff down to design there loops. What do you do to design your loops? Plexmark

Share this post


Link to post
Share on other sites
Advertisement
<< What do you do to design your loops? Plexmark >>

Well, there's basically three "rules" for loops if you're talking C/C++. (1) If you know precisely how many times you want the loop to iterate, you use the For loop. (2) If your loop must iterate at least once, and your test for exiting the loop needs to be at the end, you use Do...While (also good for data validation, you surround your input with a Do...While). And (3) if you need just a "regular" loop, which could iterate zero or a billion times, use a plain While loop where your test for exit is at the beginning of the loop.

That's about it. For "infinite loops" I prefer While (TRUE), then inside somewhere a "break" statement. Also for index variables I prefer i, j, k, etc which goes back to my FORTRAN days 20 years ago where integer vars are i,j,k,l,m,n, etc.

Phil P

[edited by - PhilVaz on August 16, 2003 10:03:08 AM]

Share this post


Link to post
Share on other sites
Also, if you notice any patterns in a list.
For example:

1,2,3,4....etc is simple (consecutive integers starting with 1)
2,4,6,8...etc is just as simple (even integers)
or 2,4,16,256,...etc is exponential

Also, whenever you want to do any processing/manipulation to an array you already have, it''s easy to use a For loop to do it.

Youll get the hang of it soon, if you havent already done so.

Share this post


Link to post
Share on other sites
I do pretty much what PhilVaz suggested, except without the while(true) stuff. I dunno... how complex a loop are we talking, here? They never seem to give me trouble. Example?

Share this post


Link to post
Share on other sites
quote:
Original post by PhilVaz
<< What do you do to design your loops? Plexmark >>

Well, there''s basically three "rules" for loops if you''re talking C/C++. (1) If you know precisely how many times you want the loop to iterate, you use the For loop. (2) If your loop must iterate at least once, and your test for exiting the loop needs to be at the end, you use Do...While (also good for data validation, you surround your input with a Do...While). And (3) if you need just a "regular" loop, which could iterate zero or a billion times, use a plain While loop where your test for exit is at the beginning of the loop.

That''s about it. For "infinite loops" I prefer While (TRUE), then inside somewhere a "break" statement. Also for index variables I prefer i, j, k, etc which goes back to my FORTRAN days 20 years ago where integer vars are i,j,k,l,m,n, etc.

Phil P

[edited by - PhilVaz on August 16, 2003 10:03:08 AM]


Phil is pretty much on track here. But, I''ll help you out with a few examples of what he is talking about

quote:
(1) If you know precisely how many times you want the loop to iterate, you use the For loop.

This is very useful for looping through an array


#define maxNumbers 10; //define size of array

int numbers[maxNumbers]; //array of numbers


for (int i=0;i<maxNumbers;i++) //loops through counting i from 0 to 9

{
numbers[i] = i; //stores the number at i position in the array

}



quote:

2) If your loop must iterate at least once, and your test for exiting the loop needs to be at the end, you use Do...While (also good for data validation, you surround your input with a Do...While).
quote:


This can be really good for a menu system (if using a console type system). Say you need to print the menu to screen, and when the user selects an option it will call the menu option then loop once again printing the menu. So this will show the menu at least once. If you just did a While loop, you would have to have 2 code segements calling your menu to be redrawn, once initially, then again inside the loop after the user has made their selection.


quote:
(3) if you need just a "regular" loop, which could iterate zero or a billion times, use a plain While loop where your test for exit is at the beginning of the loop.


This can be great for capturing key presses (in a very primative form). And you make the loop quit when the last key pressed is the Enter key (Return/Line Feed).


quote:

For "infinite loops" I prefer While (TRUE), then inside somewhere a "break" statement.


This is great for game loops and is very similar to the previous loop, but you don''t have to check it against any variable. Which means the loop is completely independent. Why would you do such a thing? It just gives you that little bit extra out of the CPU, as the CPU doesn''t have to calculate whether or not the statement is true. Some people will argue with me on this, but if your a perfectionist (at times) then you will see the point.


bool quitGame=false;

while (true)
{
if (quitGame) //check if it is time to quit

break; //exits loop

}





Extracting Patch....
Initializing Windows XP Update Path 2543663B....
Core Dumped, Now Installing Linux.....

----
Mike
Team AI: Http://members.iinet.net.au/~slyons/teamai

Share this post


Link to post
Share on other sites
I can''t really see why
while(true)
{
if(quitGame)
break;
}

would be faster than
while(!quitGame)
{
}



"THREE FOOT TELESCOPING DEMON PENIS"
- Pouya

Share this post


Link to post
Share on other sites
quote:
Original post by Valderman
I can''t really see why
while(true)
{
if(quitGame)
break;
}

would be faster than
while(!quitGame)
{
}



"THREE FOOT TELESCOPING DEMON PENIS"
- Pouya


Good point.... hmmm trying to remember back to article I read as to why it is better. I think the reason was that ifs have a run faster than the while loops, they also made the point against case/switch statements too.

Anyone know if this is true or not? Cos if it''s not, completely disregard what I said about it being marginaly faster.



Extracting Patch....
Initializing Windows XP Update Path 2543663B....
Core Dumped, Now Installing Linux.....

----
Mike
Team AI: Http://members.iinet.net.au/~slyons/teamai

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!