If you have two if
statements for:
int n = -1;
if (n < 0)
return null;
if (n <= 1)
return "yay";
Will it return null or yay? Will it run through the code top to bottom and stop at the first if
statement, or will the last one开发者_运维知识库 be used?
The first if
statement will be evaluated first, of course. n
which is -1
is in fact < 0
, so the body of code associated with that if
statement will be executed. null
will be returned.
What might be confusing you is that, though the second if
statement would evaluate true, it will never be evaluated. This is because a return
statement, when executed, leaves the function/method it is inside of.
If you wrote:
int x = 0;
return x;
x = 5;
x
would be set to 0
. x
would be returned. Any lines of code after the return would never execute.
Here's another example, to clarify:
int x = 10;
if(x < 0)
return;
x = 0;
x
would be set to 10
. x
which is 10
is in fact not < 0
, so the if
statement's body will be skipped. x
would be set to 0
.
it will return null, only because returning (in the first test) means the 2nd test will not be executed.
it will return null
, as it's the first condition that matches. After the return the rest of the function isn't executed, and a function is executed sequentially, so they will never conflict.
Another sample of 'conflicting if statements' is when you create code that cannot be reached, for example:
if (n < 0)
return "a";
if (n == -1)
return "b";
This code will never return "b", and the compiler will probably error or warn about it. The reason is that when n = -1
, the first statement is always hit, so the second statement is never reached.
It returns null, because it is the first return it encounters. It doesn't even check the second exception.
When the code is run, JVM checks condition in the first if and, when condition is met, it executes what's inside this if. If there is return null
then it returns null. After return no code is executed in this method. The only exception is when you have return in try
block and finally
block after this try.
This is just a small re-format of your code with some explicit comments. I find that the explicit braces help some people see what is going on. Just follow the program flow. If the function is returned from, then no subsequent statements in the function are ever executed. (This is a white lie, see 'finally' Exception handling, but barring that...) -- it's a very procedural (step-by-step) process.
int n = -1;
if (n < 0) {
return null; // no other statement in this function will be executed
// if this line is reached
}
if (n <= 1) {
return "yay"; // no other statement in this function will be executed
// if this line is reached
}
It'll return null.
btw in Java "If" is invalid, you have to use "if" and round brackets for ifs
Will it run through the code top to bottom and stop at the first IF statement
Yes, it will. If it does turn out that n < 0, then it will immediately return null. (I don't know why you tagged this question as Java though, the code you posted is not Java code.)
Will return null. Once you hit the return statement control passes back to the calling function setting the return value of the function to what you specify after the return keyword.
It will return null. The only value for the code you posted that would ever result in 'Yay' being returned would be 0.
精彩评论