Programming in "C‟ | List various logical operators and describe use of each with suitable example.

The logical operators are:

&&-logical AND

|| - logical OR

! – logical NOT

&&-logical AND. Used when we want more than one condition to be checked and also the statements should be executed only if all the conditions are true.

Eg : for &&(and)
#include<stdio.h>
#include<conio.h>
void main() {
int i,j;
clrscr();
printf("Enter two values");
scanf("%d%d",&i,&j);
if(i<=5 && j<=7)
printf("i and j are less than 5");
getch();
}

|| - logical OR - Used when we want more than one condition to be checked and also the statements should be executed if either of the conditions are true.

Eg: for ||(or)
#include<stdio.h>
#include<conio.h>
void main() {
int i,j;
clrscr();
printf("Enter two values");
scanf("%d%d",&i,&j);
if(i<5 || j<7) {
printf("The values are%d%d",j,j);
}
getch();
}

! – logical NOT – used we want to negate a condition.

Eg: for !(Not)
#include<stdio.h>
#include<conio.h>
void main() {
int i,j;
clrscr();
printf("Enter two values");
scanf("%d%d",&i,&j);
if(!(i<=5 && j<=7)) {
printf("the values are %d%d",i,j);
}
getch();

Post a Comment

0 Comments