RM777
RM777

Reputation: 203

Writing an if-statement inside of return

I want to write a program in java that calculates the squareroot and I am not allowed to use the math module

My code is

public class Quadratwurzel

public static double quadratwurzel(double x, int n) {
return(if (n == 0){x+1/2} else{0.5*quadratwurzel(x,n-1)+x/quadratwurzel(x,n-1)})}

I am hoping someone can explain me why I cannot write an if/else statment inside of return because in my lecture notes it says that we have to write a statement inside of return and the statment gets substituated with the parameters.

I.e if I start the method or function

quadratwurzel(32.0,6)

Then the body of the function which is inside of return will be substituated with:

(32.0,6)

And because

6!=1,5!=1,...2!=1

The method would be started 5 times again recursively until it termintates.

That is my idea and I was wondering why the substitution here does not work like that.

I hope somebody can help and give me the correct code for the idea

Upvotes: 1

Views: 15808

Answers (8)

Sxilderik
Sxilderik

Reputation: 798

You can use a simple ternary operation

return n == 0 ? //
    x + 1/2 : //
    0.5 * quadratwurzel(x, n - 1) + x / quadratwurzel(x, n - 1);

// NB: I usually use formatting (here, with double slash comments) to ease code reading

Upvotes: 0

artemis
artemis

Reputation: 7241

Here is the code that should fix your problem. I cannot test if it produces the expected result because I don't have your full program.

public class Quadratwurzel

public static double quadratwurzel(double x, int n) {
    if (n == 0){
        return (x+1/2);
    }else{
        return 0.5*quadratwurzel(x,n-1)+x/quadratwurzel(x,n-1);
    }}

The reason you cannot put an if statement is return is because return requires something to return, meaning, it only knows how to feedback a value or an object. It is not capable of "thinking" or performing the logic in an if statement. This is true for the Java programming language (consistent with your request), but not for other languages like Python, as indicated by @ in the comments below.

Therefore, you must determine what must be returned before you issue the return statement, which is how I edited your code above.

UPDATE TO OP COMMENT "therefore I don't understand why an if/else statement is not an expression. Can you please tell me what is the difference between an expressions and a statement? If...else also returns a value. Why do you say it does not yield a value?"

A very simple way to describe a statement vs an expression, is that: A statement will do something, it will complete an action. So in the example of an IF condition THEN action, the if statement performs the action of evaluating a logical condition and returning a value. An expression is something that holds a value, like: 2 or False or 5+3

Per this article, a good way to think about it is if you can print it or assign it to a variable, it is an expression. Otherwise, it is a statement.

Upvotes: 1

Muahmmad Tahir Saleh
Muahmmad Tahir Saleh

Reputation: 64

you can use either

return (n == 0) ? (x+1/2) : (0.5*quadratwurzel(x,n-1)+x/quadratwurzel(x,n-1)); 

or

    public static double quadratwurzel(double x, int n) {
        if (n != 0){
            double quadratwurzel_val = quadratwurzel(x,n-1);
            return 0.5*quadratwurzel_val+x/quadratwurzel_val;
        }
        return (x+1/2);
    }

but the second approach is better as it doesn't call quadratwurzel(x,n-1) 2 times

Upvotes: -1

Makoto
Makoto

Reputation: 106390

The syntax for return requires an expression. An if-statement is a statement and thus cannot be used as the target of a return expression.

This would mean you need to change your code to this:

public static double quadratwurzel(double x, int n) {
    if (n == 0) {
        return x + 1 / 2;
    } else {
        return 0.5 * quadratwurzel(x, n - 1) + x / quadratwurzel(x, n - 1);
    }
}

Note that there's an integer division problem here which I must leave as an exercise for the reader.

Upvotes: 4

randomboiguyhere
randomboiguyhere

Reputation: 545

You can, but you're using it wrong.

public int foo(int a, int b){
        return a > b ? a + b : a - b;
    }

a > b followed with a ? is the conditional, the first phrase after the ? is what gets returned if the conditional is true, and the second part, after the : is what gets returned if the conditional is false.

Upvotes: -1

Benjamin Urquhart
Benjamin Urquhart

Reputation: 1649

I believe you're looking for the ternary operator:

return condition ? value1 : value2;

Upvotes: 5

Mustahsan
Mustahsan

Reputation: 3862

You need to use the conditional operator here. Read more at this link

return n == 0 ? x+1/2 : (0.5*quadratwurzel(x,n-1)+x/quadratwurzel(x,n-1)); 

Upvotes: 1

Bakon Jarser
Bakon Jarser

Reputation: 721

You can use a conditional statement, which is kind of like a hard to read if statement.

return n == 0 ? x+1/2 : 0.5*quadratwurzel(x,n-1)+x/quadratwurzel(x,n-1);

Upvotes: 2

Related Questions