Home > Back-end >  Why the function is executing statements even after return statement is executed?
Why the function is executing statements even after return statement is executed?

Time:12-03

I checked with debugging also it is going to statement =>if(sum==1) return true; but then also it is executing further statements.

static long solve(int l, int r){
    // Your code goes here
    long sum=0;
    boolean t=0;
    for (int i=l;i<=r;i  ) {
        t=beautiful(i);
        if (t==true) {
            sum=sum i;
        }
    }
    return sum;
}

static boolean beautiful(int num) {
    if (num<=0) return false;
    if (num==1) return true;
    int sum=0;
    while (num>0) {
        int rem=num%10;
        sum=sum rem*rem;
        num=num/10;
    }
    if (sum==1) {
        System.out.print("some");
        return true;
    }
    System.out.print("one");
    beautiful(sum);
    return false;
}

CodePudding user response:

After then you call beautiful method, it goes into it and gets the result and continues. So you can do this despite you call beautiful method and return false;

return beautiful(sum);
  • Related