Jump to content
  • Advertisement
Sign in to follow this  
nectron101

what does for() do ??

This topic is 4860 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'm currently doing a homework for C programming.. In one of the examples the teacher gave us there's for (;;) { ..... } what does for(;;) do?

Share this post


Link to post
Share on other sites
Advertisement
it loops forever, or until something breaks out of hte for loop. you might use it for a game loop. ie start the loop and continue forever if the user quits you break out of it and cleanup.

Cheers
Chris

Share this post


Link to post
Share on other sites
Hey nectron101,

I would recommend reading the forum FAQ. It specifically states not to ask homework questions on here; if we just give you the answer, you don't learn. However, I can push you in the right direction. Think about what the purpose of the parenthesized section of the for loop is, and then think about the result without those components.

Best of luck,
Aviosity

Share this post


Link to post
Share on other sites
if you meant what do the ';;'s do in teh for loop. They're the delimiters. Even though the for loop has no statements in its definition they need to be there for proper syntax. Their absence means that the loop doesn't execute any loop overhead statements as it iterates.

Cheers
Chris

Share this post


Link to post
Share on other sites
Well, let us reason together. The for syntax is "initialisation; test; increment". Initialisation is performed at the beginning of your loop. Test is performed each time you start to cycle trhough the loop; if it is false, the loop ends. And increment is performed at the end of each loop. So far I'm not telling you anything you didn't know, I hope.

Now, what happens when you do none of these things? Well, you initialise nothing; fine, the program logic will just skip directly to the first test. Let me come back to this later. At the end of the loop, there is no increment; very good, again we circle around to the test.

Now then, what does it mean to have an empty statement as a test? Truly, I have no idea. It may even be undefined behaviour; or it could be compiler-dependent. But there are really only two options : It evaluates either to false or true, right? So if it is false, then your loop is the equivalent of

while (false) {}

which is not very interesting. Therefore, assuming your teacher is not an idiot, it probably evaluates to true at least in whatever compiler you are using. In which case you have the equivalent of

while (true) {(...)}

I hope your teacher intended this, and has a break statement somewhere in there.


Edit : Gah, I fail at life. Five posts while I was writing this?

Share this post


Link to post
Share on other sites
We shouldn't be helping you with homework, but seeing as this is merely syntax and not a typical question(like how to draw a line, for instance), I geuss we can help.

'For' is like a 'while' loop, but it compacts it. For instance, the code:

int index=0;
while(index<10)
{ index++;}

could be reduced to:

for(int index=0;index<10;index++)
{ /*other code*/ }

with a for loop. Basically, the first first section is what the for loop does to start out. Typically, you use that to initialize variables. The second section is the condition. Like a while loop, that is tested every loop and it exits if that condition is false. The third section is done every loop. While I geuss that you could put all the code for the foor loop there, thats very hard to read and unstandard. Most people just do something like increment a variable. Note: all the 'sections' are ended with semicolons and if you have multiple statements inside a 'section', you can seperate the statements with commas.

The for(;;){...} that you saw will repeat forever. If a section is left blank and only has a semicolon, it will simple do nothing. Since there is no condition, that will repeat forever.

If you have more questions along these lines, consult google first. Some c/c++ programming tutorials can be found at: http://www.cprogramming.com/

Oh, and one more thing, you don't have to put a semicolon at the end of the third section, just incase you were wondering.

Good luck with C!

Share this post


Link to post
Share on other sites
I don't think the question is the question asked for the homework. I think he just wants to understand the unorthodox syntax so he can get on with doing hte assignment.

Similarly to how many of us might look up the exact syntax for a directX or opengl function call.

Cheers
Chris

Share this post


Link to post
Share on other sites
I did not ask you how to solve my homework..

I'm just telling you that I found something strange in my homework and I don't know what it is..

Thanks for the replies guys.. you've been helpful.. ;)

Share this post


Link to post
Share on other sites
My 2 cents: I wouldn't use that syntax as it leaves you scratching your head. Code should be self documenting. That is, the intent of the programmer should be fairly clear from the code. Writing for(;;) doesn't express the intent of a continuous loop nearly as well as while(true). That's my opinion, anyway.

Good luck with your class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!