maximum nested conditionals / loops

Started by
2 comments, last by daviangel 16 years, 5 months ago
In C is there (or should there be) a limit to the number of levels of nested conditionals and/or loops? Like if you have an if-else (or switch-case) that calls another function based on the condition, and inside that function is another conditional branch that calls another function, and so on. Is there an arbitrary limit to this kind of nesting? Is there a better way in C to do a menu / sub-menu / sub-menu / sub-menu system without nesting function calls within function calls? I have several books on C, C++, and game programming, but none really cover topics like this. And I am speaking in terms of platform-independence, not using the Win32 API.
Advertisement
You can do recursions if you need too many loops also by recursion u can have as many loops as u want.
The maximum number of nested scopes (loops, if statements etc) is probably set by your compiler. If you hit this limit you need to seriously reconsider your design.

The maximum number of successive function calls is limited by the stack. You are quite unlikely to hit this limit unless you are using very deep recursion. You would need to write ALOT of functions for a series of non recursive calls to overflow the stack. (Yes you could overflow the stack in a few calls if you put some enormous data structure on it, but that's just a bad idea all around)
Limit nesting to three levels!
Studies have shown that the ability of programmers to understand a loop deteriorates significantly beyond three levels of nesting(Yourdon 1986a)
and I agree since it drives me loopy!
Try breaking it into a routine or simplifying your control structure.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

This topic is closed to new replies.

Advertisement