Home > Blockchain >  return statement in Java use
return statement in Java use

Time:03-12

I'm a beginner Java programmer. I am learning methods, functions and return statements. While solving questions on finding the prime numbers and palindrome numbers within a range given by the user I could notice that in the public static boolean prime(int n) function I needed to compulsorily mention return (true) before ending the prime(int n) function even though I was returning proper true and false statements within the if and else blocks for checking whether its prime or not. However in palindrome(int n) function I was asked not to put return statement before ending the scope even though here also I was returning proper true and false statements within the if and else blocks for checking whether its prime or not.

Code for printing prime numbers within a given range

//printing prime numbers within a range given by the user
  import java.util.*;
  public class primeiinrange
  {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("enter the first range");
        int a = sc.nextInt();
        System.out.println("enter the second range");
        int b = sc.nextInt();
        int c=0;
        for (int i = a; i <= b; i  ) {
            if (prime(i) == true) {
                System.out.print(i   ", ");
                c  ;
            }
        }
        if (c == 0) {
            System.out.println("No prime number found");
        }
    }

    public static boolean prime(int n)
    {
       if(n<=1)
       { 
           return false;
       }
       int c=2;
       while(c*c<=n)
       {
           if(n%c==0)
           { 
               return false;
           }
          c  ;
       }
       if(c*c>n)
       {
           return true;
       }
       return true; //if this return statement is not given then error shown: missing return statement 
    }
}

Code for printing prime numbers within a given range

// palindrome numbers between a given range
import java.util.*;
public class palindrome 
{
    public static void main(String[] args) 
    {
    Scanner sc = new Scanner(System.in);
    System.out.println("enter the first range");
    int a = sc.nextInt();
    System.out.println("enter the second range");
    int b = sc.nextInt();
    int c=0;
    for (int i = a; i <= b; i  ) {
        if (palindrome(i) == true) {
            System.out.print(i   ", ");
            c  ;
        }
    }
    if (c == 0) {
        System.out.println("No palindrome number found");
        }
    }

public static boolean palindrome(int n)
    {
    int copy = n;
    int add = 0;
    int rem = 0;
    while (n > 0) {
        rem = n % 10;
        add = add * 10   rem;
        n = n / 10;
    }
    if (add == copy) {
        return true;
    } else {
        return false;
    }
    //if return (true) statement is given in this line the error shown: unreachable statement
    }
}

why do we have to write return (true) before ending scope of of prime(int n) compulsorily but we cannot write return (true) before the end of scope of palindrome(int n)?If anyone could help a bit, that would be very nice .Thanks. NOTE: I use IntelliJ Idea IDE

CodePudding user response:

The Java compiler will comply and will return that error since, as the error is stating, that line will be unreachable.

That line and every line after are unreachable since you have an "if - else" statement in the prime function, right before it, containing both a return statement. This means that the method will return in any case and will not execute any more statements after the "if - else".

This is independent of the IDE software you are using, but it is part of the Java language specs.

CodePudding user response:

  1. When detecting prime numbers, the statement if (c * c > n) is redundant because this line is reached only when while (c * c <= n) loop has completed, that is, this condition is already verified in the while:
public static boolean prime(int n) {
    if (n <= 1) { 
        return false;
    }
    int c = 2;
    while (c * c <= n) {
        if (n % c == 0) { 
            return false;
        }
        c  ;
    }
    return true; 
}
  1. When detecting a palindrome, instead of if-else statement returning a boolean, a cleaner form should be used to return the result of the comparison:
public static boolean palindrome(int n) {
    int copy = n;
    int add = 0;
    int rem = 0;
    while (n > 0) {
        rem = n % 10;
        add = add * 10   rem;
        n = n / 10;
    }
    return add == copy;
}

CodePudding user response:

In your code, add and copy are both integers and palindrome() method's return type is a boolean flag (datatype will hold either true or false), your method will check if the passed parameter is palindrome or not. Now when add is equal to copy then you will return the flag as true, it will indicate the number is actually palindrome and if its not equal i.e. add not equal to copy then it is considered as not palindrome, you need to send the flag as false, to indicate the caller, the passed number is not palindrome.

if (add == copy) {
        return true;
    } else {
        return false;
    }

Once you write return, the method execution ends then and there itself and control is passed to the caller method.

  • Related