Maximum number of nested conditions allowed
Does anyone knows the limit of nested conditions (I mean conditions under another, several times)? In, let's 开发者_Go百科say, Java and Visual Basic.
I remembered when I was beginning with my developing trace, I make, I think 3 nested conditions in VB 6, and the compiler, just didn't enter the third one, now that I remember I never, knew the maximun nested coditions a language can take.
No limit should exist for a REAL programming language. For VB.NET and Java I would be shocked if there is any limit. The limit would NOT be memory, because we are talking about COMPILE TIME constraints, not executing environment constraints.
This works just find in C#: It should be noted that the compiler might optimize this to not even use the IFs.
static void Main(string[] args)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{
if (true)
{ Console.WriteLine("It works"); }
}
}
}
}
}
}
}
}
}
}
}
}
}
}
This should not be optimized too much:
static void Main(string[] args)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{
if (DateTime.Now.Month == 1)
{
if (DateTime.Now.Year == 2011)
{ Console.WriteLine("It works"); }
}
}
}
}
}
}
}
}
}
}
}
}
}
Console.ReadKey();
}
I agree with most of the people here that there is no limit on writing the if blocks. But there is a max limit on the java method size. I believe its 64K.
If you mean nested if
blocks then there is no theoretical limit. The only bound is the available disk space to store the source code and/or compiled code. There may also be a runtime limit if each block generates a new stack frame, but again that is just a memory limit.
The only explanation for your empirical result of 3 is either an error in programming or an error in interpreting the results.
I must agree that the limit is purely based on memory limit. If you reached it, I would expect that you would reach some kind of stack overflow limit, however I doubt there is a possibility that you would reach this limit.
I could not find a source of reference to back this up, but a quick test of 40+ nested if statements compiled and ran fine.
The limit to the number of nested conditionals will almost certainly be based upon the size of the compiler's stack and data structures, and not anything to do with the run-time environment except possibly in cases where the code space of the target environment is severely constrained relative to the memory available to the compiler (e.g. using a modern PC to compile a program for a small microcontroller with 512 bytes of flash). Note that no RAM (beyond any used to store the code itself) is required at run-time to evaluate a deeply-nested combination of logical operators, other than whatever would be required by the most complex term thereof (i.e. memory required to compute '(foo() || bar()) && boz()' is the largest of the memory required to compute foo(), bar(), or boz()).
In practical terms, there is no way one would reach a limit using a modern compiler on a modern machine, unless one were writing a "program" for the specific purpose of exceeding it (I'd expect the limit would probably be between 1,000 and 1,000,000 levels of nesting, but even if it's "only" 1,000 there's no reason to nest things that deep).
Logically I would think that the limit would be based on the memory available to the application in Java.
精彩评论