C examples for Operator:Logic Operator
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:
#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; }
The operands of the && operator can be bool variables.
#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*/ }
You can use more than one of these logical operators in an expression:
#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*/ }