Sunday, January 20, 2019

Logical operators in c++ programming

There are three types of logical operators in c++ programming.

logical operators in c++ programming, learn c++ programming
learn c++ programming


Logical AND(&&) operator.
Logical OR(||) operator.
Logical NOT(!) operator.

1: Logical AND(&&) operator:

To check whether both conditions are true among two conditions, we used AND operator.

Logical symbol =  &&

Example:
             if(i>=0 && i<=100)      
            {             
              std::cout<<"\n you enter the value between the the 0                          and 100";                 
            }
Note: if both the conditions are true then the condition for if statement is true, so if you want to check that both conditions are true among two conditions, use the AND(&&) operator. in the above example, if both conditions are true then the condition for the if statement is true and compiler execute the code of if statement. 

  

Logical OR(||) operator:

To check whether anyone or both condition is true among two conditions we used the logical operator OR.

Logical symbol = ||

Example:
               if(i>=0 || j<=100)
               {
                std::cout<<"\n you enter the value between the the 0                          and 100";                                

                }
Note: if the any one or both condition is true among two conditions. then the condition for if statement is true, so if you want to check that any one condition is true among two conditions, use the OR(||) operator. in the above example when one of the conditions or both condition is true among both condition, then the condition for the if statement is true and the compiler executes the code of if statement. 

Logical NOT(!) operator:

we use the NOT(!) operator to reverse a condition.

Logical symbol =!
Example:
                if(i%2 != 0)         
               {
               std::cout<<"\n your number is not devisible by 2";                             }

Note: To find that a condition is not true we use NOT(!) operator. in the above example when the condition is not true then the condition for if block is true and the code of if statement is executed.

So, now I think you totally understand about the logical operators, and you can self-used these in your program. 

Continue learning:



0 comments: