Advertisement
Name of logical operators | Logical Operators |
---|---|
Short-circuit logical operator | &&, || |
Non-short-circuit logical operator | &, |, ! |
<boolean-expression> && <boolean-expression>
// Example of && operator in C++
#include<iostream>
using namespace std;
int main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(a<15 && a>10) // a is less than 15 and a is greater than 10
cout<< "a<15 && a>10, is true " << "\n";
if(b<25 && b>19) // b is less than 25 and a is greater than 19
cout<< "b<25 && b>19, is true " << "\n";
if(ch1=='a' && ch2=='b') // ch1 has 'a' and ch2 has a 'b'
cout<< "ch1=='a' && ch1=='c', is true " << "\n";
if(d>2 && ch2=='b') // d is greater than 2 and ch2 has a value 'b', is true
cout<< "d>2 && ch2==b, is true " << "\n";
if(a<=10 && d<=2.5) // a is less than or equal to 10 and d is less than or equal to 2.5
cout<< "a<=10 && d<=2.5, is true " << "\n";
if(a<10 && ch1=='a') // a is less than 10 and ch1 has a value 'a'
cout<< "a<=10 and b>=20, is true";
return 0;
}
b<25 && b>19, is true
ch1=='a' && ch1=='c', is true
d>2 && ch2=='b', is true
a<=10 && d<=2.5, is true
<boolean-expression> || <boolean-expression>
// Example with || operator in C++
#include<iostream>
using namespace std;
int main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(a<15 || a>10) /* a is less than 15 or a is greater than 10 */
cout<<"a<15 || a>10, is true" <<"\n";
if(ch1=='a' || ch2=='b') /* ch1 has 'a' or ch2 has a value 'b' */
cout<<"ch1=='a' || ch1=='c', is true" <<"\n";
if(b<25 || b>19) /* b is less than 25 or a is greater than 19 */
cout<<"b<25 || b>19, is true " <<"\n";
if(d>2 || ch2=='b') /* d is greater than 2 or value in ch2 has a value 'b', is true */
cout<<"d>2 || ch2==b, is true" <<"\n";
if(a<=10 || d<=2.5) /* a is less than or equal to 10 or d is less than or equal to 2.5 */
cout<<"a<=10 || d<=2.5, is true " <<"\n";
if(a<10 || ch1=='a') /* a is less than 10 or ch1 has a value 'a' */
cout<<"a<10 || ch1=='a', is true";
return 0;
}
a<15 || a>10, is true
ch1=='a' || ch1=='c', is true
b<25 || b>19, is true
d>2 || ch2==b, is true
a<=10 || d<=2.5, is true
a<10 || ch1=='a', is true
Advertisement
<boolean-expression> && <boolean-expression>
// Example with & operator in C++
#include
using namespace std;
int main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(a<15 & a>10) // a is less than 15 and a is greater than 10
cout<< "a<15 & a>10, is true" <<"\n";
if(b<25 & b>19) // b is less than 25 and a is greater than 19
cout<< "b<25 & b>19, is true" <<"\n";
if(ch1=='a' & ch2=='b') // ch1 has 'a' and ch2 has a value 'b'
cout<< "ch1=='a' & ch1=='c', is true " <<"\n";
if(d>2 & ch2=='b') // d is greater than 2 and value in ch2 has a value 'b', is true
cout<< "d>2 & ch2=='b', is true " << "\n";
if(a<=10 & d>=2.5) // a is less than or equal to 10 and d is greater than or equal to 2.5
cout<< "a<=10 & d<=2.5, is true " << "\n";
if(a<10 & ch1=='a') // a is less than 10 and ch1 has a value 'a'
cout<< "a<10 & ch1=='a', is true";
return 0;
}
b<25 & b>19, is true
ch1=='a' & ch1=='c', is true
d>2 & ch2=='b', is true
a<=10 & d<=2.5, is true
Note: There should never be any space between > and = of >= operator, or a compile error is thrown.
<boolean-expression> | <boolean-expression>
// Example with | operator in C++
#include<iostream>
using namespace std;
int main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(a<15 | a>10) // a is less than 15 and a is greater than 10
cout<< "a<15 | a>10, is true" << "\n";
if(b<25 | b>19) // b is less than 25 and a is greater than 19
cout<< "b<25 | b>19, is true" << "\n";
if(ch1=='a' | ch2=='b') // ch1 has 'a' and ch2 has a value 'b'
cout<< "ch1=='a' | ch1=='c', is true" << "\n";
if(d>2 | ch2=='b') // d is greater than 2 and value in ch2 has a value 'b', is true
cout<< "d>2 | ch2=='b', is true " << "\n";
if(a<=10 | d>=2.5) // a is less than or equal to 10 and d is greater than or equal to 2.5
cout<< "a<=10 | d<=2.5, is true " << "\n";
if(a<10 | ch1=='a') // a is less than 10 and ch1 has a value 'a'
cout<< "a<10 | ch1=='a', is true";
return 0;
}
Output -
a<15 | a>10, is true
b<25 | b>19, is true
ch1=='a' | ch1=='c', is true
d>2 | ch2=='b', is true
a<=10 | d<=2.5, is true
a<10 | ch1=='a', is true
Note: There should never be any space between = and = of == operator, or a compile error is thrown.
!<boolean-expression>
// Example with ! (NOT operator)
#include<iostream>
using namespace std;
int main()
{
int a=10, b=20;
char ch1='a', ch2 ='b';
double d=2.5;
if(!(10>15)) // Inverting the value of an expression (10>15)
cout<< "!(10>15), is true \n";
if( !(b<25 || b>19) ) // Inverting the value of an expression (b<25 || b>19)
cout<< "!(b<25 | b>19), is false" << "\n";
if( !(ch1=='a' && ch2=='b') ) // Inverting the boolean value of (ch1=='a' && ch2=='b')
cout<< "!(ch1=='a' && ch2=='b'), is false" << "\n";
if(d>2 & !(ch2=='b')) // Inverting the boolean value of an expression (ch2=='b')
cout<< "d>2 & !(ch2=='b'), is true" << "\n";
if( !(a<=10 | d>=2.5) ) // Inverting the boolean value of (a<=10 | d>=2.5)
cout<< "a<=10 | d<=2.5, is true" << "\n";
if(!(a<10)) // Inverting the boolean value of (a<10)
cout<< "!(a<10) is true" << "\n";
if(!0) // Inverting 0(false) to 1(true)
cout<< "This will be printed" << "\n";
if(!1) // Inverting 1(true) to 0(false)
cout<< "This will not be printed" << "\n";
return 0;
}
!(10>15), is true
!(a<10) is true
This will be printed
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement