## 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.

`#include <iostream.h> ` ` ` `// Function to check whether n is odd ` `bool` `isOdd(` `int` `n); ` ` ` `// Function to check whether n is prime ` `bool` `isPrime(` `int` `n); ` ` ` `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++; ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

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 than2are not prime but the implementation still checks them for prime.

## Logical OR (||)

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

`#include <iostream.h> ` ` ` `// Function to check whether n is odd ` `bool` `isEven(` `int` `n); ` ` ` `// Function to check whether n is prime ` `bool` `isPrime(` `int` `n); ` ` ` `int` `main() ` `{ ` ` ` `int` `cnt = 0, n = 10; ` ` ` ` ` `// Implementation 1 ` ` ` `for` `(` `int` `i = 3; i <= n; i++) { ` ` ` `if` `(isEven(i) || !isPrime(i)) ` ` ` `cnt++; ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

As described earlier that the probability of a number being even is more than that of it being a non-prime. The current order of execution of the statements doesn’t allow even numbers greater than 2 to be checked whether they are non-prime (as they are all non-primes).

**Note:** For larger inputs, the order of the execution of statements can affect the overall execution time for the program.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Difference between Backtracking and Branch-N-Bound technique
- What are the differences between bitwise and logical AND operators in C/C++?
- Operators in C | Set 2 (Relational and Logical Operators)
- Logical and Cartesian Indexing in Julia
- Order of operands for logical operators
- Logical Not ! operator in C with Examples
- Code valid in both C and C++ but produce different output
- 7 Tips To Write Clean And Better Code in 2020
- How to write a running C code without main()?
- Writing OS Independent Code in C/C++
- Code to generate the map of India (with explanation)
- Printing source code of a C program itself
- Convert C/C++ code to assembly language
- Self Destructing Code in C
- How to add code to existing article (Using Improve Article)?
- LEX code to extract HTML tags from a file
- Convert C/C++ program to Preprocessor code
- C++: Methods of code shortening in competitive programming
- Python Code for time Complexity plot of Heap Sort
- How to write a Pseudo Code?

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.