Logical Operators

Posted in: C by musiitwa joseph 2016-03-29 at 01:32:27



There are three types of logical operators and these are Logical OR, Logical NOT and Logical AND

Logical AND

If one of the conditions is false, the expression becomes false and if all the operands are true, the expression becomes true

#include <stdio.h>
void main(){
    int x = 0, y=5;
    
    if(x <= 5 && y == 9){
        printf("Hello world");
    }else{
        printf("Hi");
    }
    
}

Hi will be printed out

Logical OR

If one of the conditions is true, the expression is true and if all the operands are false, the expression becomes false

#include <stdio.h>
void main(){
    int x = 0, y=9;
    
    if(x <= 5 || y == 9){
        printf("Hello world");
    }else{
        printf("Hi");
    }
    
}

Hello world will be printed out

Logical NOT

Used for reversing the expresion. If the expression evaluates to true, it reverses it to false and the reverse is true.

#include <stdio.h>
void main(){
    int x = 0, y=9;
    
    if(!(x <= 5 && y == 9)){
        printf("Hello world");
    }else{
        printf("Hi");
    }
    
}

Hi will be printed out

Tags:
c


Search

Leave a comment

Fields with * are required
Your email address will not be displayed
4 + 5 =