Logical Operators: AND Operator && - C Operator

C examples for Operator:Logic Operator

Introduction

The logical AND operator, &&, is a binary operator that combines two logical expressions.

test1 && test2 evaluates to true if both expressions test1 and test2 evaluate to true.

If either or both of the operands are false, the result of the operation is false.

Here's an example:

Demo Code

#include <stdio.h> 

int main(void) { 

   int age = 15;/*from  ww w  .j a va  2  s. c  o m*/

   if(age > 12 && age < 20)
      printf("teenager.");

  return 0; 
} 

Result

The operands of the && operator can be bool variables.

Demo Code

#include <stdio.h> 

int main(void) { 

    int age = 15;

    bool test1 = age > 12;
    bool test2 = age < 20;
 
    if(test1 && test2)
        printf("teenager.");

  return 0; /*from  w ww.j ava  2s . c  o m*/
} 

Result

You can use more than one of these logical operators in an expression:

Demo Code


#include <stdio.h> 

int main(void) { 

    int age = 15;
    int grade = 7;

    if(age > 12 && age < 20 && grade > 5)
       printf("5th grade.");


  return 0; /*from w ww.j a v  a  2 s .co m*/
}

Result


Related Tutorials