Breaking News

How to use Logical operators ( AND &&, OR || ) in C++

An if Statement is a programming conditional statement that provide two results, true or false. If it proved true, it performs the execution of statements that depends on it and if it proved false it skip the execution of statements that depends on it.

This Conditional Statement is composed by Relational Operators (Comparison Operators). This Operators create an expression called Boolean Expression. We call it Boolean Expression because it returns the value that is a Bool Data Type (true or false). From here, now we have Compound Expression which is the combination of two Boolean Expressions. This Compound Expression is composed by Logical Operators. There are two Logical Operators.
  • AND Operator ( && ). This operator is used to compare the result of the first Boolean Expression and the result of the second Boolean Expression. If these two results are both true, the whole Compound Expression will be true, otherwise it will be false.
  • OR Operator ( || ). This operator is used to compare the result of the first Boolean Expression and the result of the second Boolean Expression. If both or only one among these results is true, the whole Compound Expression will be true, otherwise it will be false.

The Structure of the Compound Expression can be expressed like:

 ( Boolean Expression Logical Operator Boolean Expression )

In the real World of Programming, that structure can be written as the following example:

     if ( 2 >= 5 && 17 != 6 )

The output of the above example is false, Why? 
Because, when we execute the first Boolean expression (left hand side) the output(result) will be false, and when we execute the second Boolean expression (right hand side) the output (result) will be true, so when we recall from our truth table FALSE AND TRUE = FALSE and that is why we say the result is false.

Another example:

     if ( 7 == 1 || 9 > 2 )

The output of the above example is true, Why? 
Because, when we execute the first Boolean expression (left hand side) the output(result) will be false, and when we execute the second Boolean expression (right hand side) the output (result) will be true, so when we recall from our truth table FALSE OR TRUE = TRUE and that is why we say the result is true.

Now let us solve the following question by applying the above concept.

Write a program that will help a teacher to calculate the grade of a student given the average. 
Hints: 
Marks < 0 or Marks > 100 give Error Message.
0 <= Marks < 40 – Grade = C
40 <= Marks < 70 – Grade = B
70 <= Marks <= 100 – Grade = A 
Use if - else statement to solve the question.

The answer of this question will be written in Dev-C++ Compiler as following, but you may use any Compiler.

#include <iostream>
using namespace std;

int main(){
    float marks;
    cout<<"Enter marks of a student: ";
    cin>>marks;

    if (marks >= 0 && marks < 40)
    {
     cout << "Grade C";
    }
    else if (marks >= 40 && marks < 70)
    {
    cout<<"Grade B";
    }
    else if (marks >= 70 && marks <= 100)
    {
    cout<<"Grade A";
    }
    else
    {
    cout<<"Marks is out of range";
    }
}


My input was 56 as my marks and it will display the following output (see picture below).


We are done...!!!

I hope now you understand how to use Logical Operators in C++. Feel free to ask any question if you didn't understand then I can reply as faster as I can. 


Please comment and share with your friends. Don't miss the following post where we will continue to learn different things and application of new concepts.

Thank you for reading, be blessed....!!!

No comments