Logical Operators in C with examples

Logical Operators:


In C language we have various logical operators.
They are....

  1. Logical AND - &&
  2. Logical OR - ||
  3. Logical NOT - !


We will see their examples for better understanding.


Program using logical AND:

#include<stdio.h>
void main()
{
        int i;
        for(i=0;i<10;i++)
         {
           if(i>0 && i>5)
           {
              printf("%d\n",i);
           }
         }
}

In AND operator both conditions need to be true then only it will enter inside the if block, otherwise not.

OUTPUT:
Logical Operators in C with examples        


Program using logical OR:

#include<stdio.h>

void main()
{
int i;
for(i=0;i<10;i++)
  {
if(i>0||i>5)
    {
printf("%d ",i);
     }
   }
}

In OR operator if  any one or both conditions are  true then this enters inside the if block otherwise not.

OUTPUT:


Logical Operators in C with examples


Program using logical NOT:

#include<stdio.h>

void main()
{
int i;
for(i=0;i<10;i++)
if(i!=5)
printf("%d ",i);
}

If we don't want to print any value of the loop then we can use the NOT operator.

OUTPUT
Logical Operators in C with examples

                                 

No comments:

Post a Comment

If you have any doubts, please discuss here...👇