public static int mystery (int n)
{
if (n == 0)
return 0;
else
return n + mystery(n-1);
//The following is an exampler of what recursion looks like using a loop instead
/* int intCount = n;
if (intCount == 0)
return 0;
else
{
while (n != 0)
{
intCount = intCount + n-1;
n--;
}
//return n + mystery(n-1);
}
return intCount;*/
}
ASKER
ASKER
intintCount=n;
if(intCount==0)
return0;
else
{
while(n!=0)
{
intCount=intCount+n-1;
n--;
}
}
return intCount;
ASKER
Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.
TRUSTED BY
A loop would be simple iteration of the same code without changing of context (stack, etc.). Each iteration uses the same variables.
Recursion is calling of same code while changing the context (pushing local vars, etc.) so each iteration uses a new set of variables (on the stack). Each call of the same function gets its own copy of the named variables.
You must do some reading on "execution context".
Code is made up of 2 things, executable instructions and context. It is the context where your different versions of the same variables are stored.