Logical AND (&&)

While using && (logical AND), we must put the condition first whose probability of getting false is high so that compiler doesn’t need to check the second condition if the first condition is false.
 

C++14




#include <bits/stdc++.h>
using namespace std;
// Function to check whether n is odd
bool isOdd(int n) { return (n & 1); }
 
// Function to check whether n is prime
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i <= sqrt(n); i++)
        if ((n % i) == 0)
            return false;
    return true;
}
 
int main()
{
    int cnt = 0, n = 10;
 
    // Implementation 1
    for (int i = 2; i <= n; i++) {
        if (isOdd(i) && isPrime(i))
            cnt++;
    }
 
    cnt = 0;
    n = 10;
 
    // Implementation 2
    for (int i = 2; i <= n; i++) {
        if (isPrime(i) && isOdd(i))
            cnt++;
    }
}


Java




public class Main {
    // Function to check whether n is odd
    public static boolean isOdd(int n) {
        return n % 2 != 0;
    }
 
    // Function to check whether n is prime
    public static boolean isPrime(int n) {
        if (n <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
 
    public static void main(String[] args) {
        int cnt = 0;
        int n = 10;
 
        // Implementation 1
        for (int i = 2; i < n; i++) {
            if (isOdd(i) && isPrime(i)) {
                cnt++;
            }
        }
        System.out.println("Implementation 1: " + cnt);
 
        cnt = 0;
        n = 10;
 
        // Implementation 2
        for (int i = 2; i < n; i++) {
            if (isPrime(i) && isOdd(i)) {
                cnt++;
            }
        }
        System.out.println("Implementation 2: " + cnt);
    }
}


Python3




# Function to check whether n is odd
def isOdd(n):
    pass
 
# Function to check whether n is prime
def isPrime(n):
    pass
 
if __name__ == '__main__':
    cnt = 0; n = 10
 
    # Implementation 1
    for i in range(2,n) :
        if isOdd(i) and isPrime(i):
            cnt+=1
     
 
    cnt = 0
    n = 10
 
    # Implementation 2
    for i in range(2,n):
        if isPrime(i) and isOdd(i):
            cnt+=1
    


C#




using System;
 
class MainClass {
  // Function to check whether n is odd
  public static bool isOdd(int n) {
    return n % 2 != 0;
  }
 
  // Function to check whether n is prime
  public static bool isPrime(int n) {
    if (n <= 1) {
      return false;
    }
    for (int i = 2; i <= Math.Sqrt(n); i++) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
 
  public static void Main() {
    int cnt = 0;
    int n = 10;
 
    // Implementation 1
    for (int i = 2; i < n; i++) {
      if (isOdd(i) && isPrime(i)) {
        cnt++;
      }
    }
    Console.WriteLine("Implementation 1: " + cnt);
 
    cnt = 0;
    n = 10;
 
    // Implementation 2
    for (int i = 2; i < n; i++) {
      if (isPrime(i) && isOdd(i)) {
        cnt++;
      }
    }
    Console.WriteLine("Implementation 2: " + cnt);
  }
}


Javascript




<script>
    // Function to check whether n is odd
    function isOdd(n) { return (n & 1); }
     
    // Function to check whether n is prime
    function isPrime(n)
    {
        if (n <= 1)
            return false;
        for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++)
            if ((n % i) == 0)
                return false;
        return true;
    }
     
     
        let cnt = 0, n = 10;
     
        // Implementation 1
        for (let i = 2; i <= n; i++) {
            if (isOdd(i) && isPrime(i))
                cnt++;
        }
     
        cnt = 0;
        n = 10;
     
        // Implementation 2
        for (let i = 2; i <= n; i++) {
            if (isPrime(i) && isOdd(i))
                cnt++;
        }
     
    // This code is contributed by Pushpesh Raj.
</script>


Consider the above implementation: 

In implementation 1, we avoid checking even numbers whether they are prime or not as primality test requires more computation than checking a number for even/odd. 
Probability of a number getting odd is more than of it being a prime that’s why we first check whether the number is odd before checking it for prime. 
 

On the other hand in implementation 2, we are checking whether the number is prime or not before checking whether it is odd which makes unnecessary computation as all even numbers other than 2 are not prime but the implementation still checks them for prime. 

Code Optimization Technique (logical AND and logical OR)

Similar Reads

Logical AND (&&)

While using && (logical AND), we must put the condition first whose probability of getting false is high so that compiler doesn’t need to check the second condition if the first condition is false....

Logical OR (||)

...

Contact Us