Home > Net >  Both If and Else are executing. What is wrong here?
Both If and Else are executing. What is wrong here?

Time:09-21

I’m just really learning Java, going through tutorials through online and books, so please forgive any basic mistakes I’m making. I am not understanding why both if and else is executing.

import java.io.*;

public class Primes {

    static boolean prime(long num) {
        boolean isPrime = false;
        for (int i = 2; i <= num - 1; i  ) {
            if (num % i == 0) {
                isPrime = false;
            } else {
                isPrime = true;
            }
        }
        return isPrime;
    }

    static void generate(long max) {
        long num = 2;
        for (int i = 2; i <= max; i  ) {
            if (prime(num)) {
                System.out.println(num);
            }
            num  ;
        }
    }

    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Maximum number of primes?");
        int max = Integer.parseInt(br.readLine());
        generate(max);
    }

}

If I change the code little bit. It is working accordingly. I changed code like this.

static boolean prime(long num) {
    boolean isPrime = true;
    for (int i = 2; i <= num - 1; i  ) {
        if (num % i == 0) {
            isPrime = false;
        }
    }
    return isPrime;
}

But I want to know to why the first condition is not working.

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

How the condition can be both true and false in my code. Anybody please help me on this.

CodePudding user response:

Get a look at your code:

boolean isPrime = false;
for (int i = 2; i <= num - 1; i  ) {
        if (num % i == 0) {
            isPrime = false;
        } else {
            isPrime = true;
        }
}
return isPrime;

Assume num = 10. So, loop iterating over these steps: i = 2, i = 3, i = 4, i = 5, i = 6, i = 7, i = 8, i = 9

Each step processes inner logic:

        if (num % i == 0) {
            isPrime = false;
        } else {
            isPrime = true;
        }

And, for last step, where i = 9, isPrime = true.

And after that, when loop steps are over, return isPrime; is performed.

So, result of the last step will be returned. And as you should see, previous steps are meaningless.

CodePudding user response:

in every loop isPrime is assigned to true / false , but you are not returning the statement. so that loop works till end. instead return the loop.

Public boolean prime(long num){
boolean isPrime = false;
for (int i = 2; i <= num - 1; i  ) {
 if (num % i == 0) {
  isPrime = false;
  return isPrime;
  } else {
 isPrime = true;
    return isPrime;
            }
   }
   return isPrime;
 }

CHANGE LIKE THIS AND TRY AGAIN

  •  Tags:  
  • java
  • Related