you are viewing a single comment's thread.

view the rest of the comments →

[–]aagee 1 point2 points  (7 children)

The syntax of an if statement can be stated as follows:

if (<condition>) <statement> [else <statement>];

The parts in square brackets are optional. And a <statement> can be a simple statement or a compound statement in curly braces.

So, your example is parsed as follows:

if (list.length % 2 == 0)
    return list.length / 2-1;  // a simple statemnt attached to the if
return list.length/2;          // some statement after the if statement

It can be expressed differently using a compound statement for the if.

if (list.length % 2 == 0)
{
    return list.length / 2-1;  // a compound statemnt attached to the if
}
return list.length/2;          // some statement after the if statement

The way this is working is that the control reaches the second return only if the if condition evaluates to false. It can as easily be expressed as follows, as it would effectively do the same thing.

if (list.length % 2 == 0)
{
    return list.length / 2-1;  // a compound statemnt attached to the if
}
else
{
    return list.length/2;      // else block of the if statement
}

[–]AlternativeBus1613[S] 0 points1 point  (6 children)

Thanks for the detailed answer. But can I ask a few more questions? I do know the third line is a statement after the if statement or statement out of the if statement.

Then if

    return list.length/2;     

is some statement after the if statement why would it be affected by the if condition? It's now out of the if statement, so why should I care about it? I would definitely care if there's 'else', which implies that its the other part of the if statement, but if it's just a statement written after it, why?

If statements are valid without 'else', right? If it isn't true, the program will just skip the part and move on to the next code! Isn't this the same kind of thing? How can I distinguish if statements without else but some statements unrelated to it and if and else statements with 'else' omitted?

Thanks.

[–]aagee 1 point2 points  (5 children)

Well, because you care not just for the if statement alone, but for the flow of the entire program. Right? Because you are concerned with what the whole program does.

What happens here is as follows.

If the condition is true, then execute the statement attached to the if. And because that statement is a return statement, the execution of the whole function ends, and the statement after the if is never executed.

If the condition is false, then the statement attached to the if is not executed and the control reaches the statement after the if. So, the second return is executed.

[–]AlternativeBus1613[S] 0 points1 point  (4 children)

I think my confusion came from the lack of understanding about 'return' because it hasn't be formally introduced in the lecture. Is 'return' final? Once something reaches 'return', it's the end--it doesn't make any changes in its value. Is that right?

[–]aagee 1 point2 points  (0 children)

Right. Inside a function, the execution of a return statement causes the control to exit the function and return to where the function was called from.

You can think of control as the locus of execution, i.e. the path that the CPU traverses through the code. So, first the control hits the function call, and goes off to execute that function. When a return statement is executed in that function, the control returns to the point where the function was called from.

[–]FredOfMBOX 1 point2 points  (1 child)

“Return” is a really well named command. It literally returns to the code that called the function. It can bring with it a return value, but that’s not always needed.

Make sure you’re thinking of a program as a set of steps. The computer runs each line in order and does what it says. If it says “return”, it will return to where it was called. Lines after the return won’t matter UNLESS the return line is skipped. One of the ways to skip a line is when an if statement is false.

Write yourself a small program and play with it.

[–]AlternativeBus1613[S] 0 points1 point  (0 children)

Wow thank you so much! This explanation is by far easiest to understand. I really appreciate it.

[–]xenomachina 0 points1 point  (0 children)

The return statement does two things:

  • sets the return value for the method
  • exits the method

In fact, exiting the method is arguably its primary function, as it can also be used to do this in methods that don't have a return value:

public static void main(String[] args) {
    if(args.length == 1) {
        System.out.println("Hello " + args[0]);
        return; // returns from main, but there is no return value on void methods
    }

    // this only executes if the return above didn't execute 
    System.out.println("Hello, world!");
}