Logical Operators
Operator |
Notation |
OR |
|| |
AND |
&& |
NOT |
! |
The operators follow the same precedence in compound conditions as in other languages-NOT (!) is evaluated before AND (&&) which is evaluated before OR (||).
Brackets() are used to change this order.
The following example demonstrates the use of these logical operators:
#include <stdio.h>
/* function to demonstrate the use of the logical operators */
main()
{
char niv;
puts ("Enter 'Y' for Yes or 'N' for No");
niv=getchar();
if (niv != 'Y' && niv != 'y' && niv != 'N' && niv != 'n')
puts("Invalid Input");
else if (niv = = 'Y' || niv = = 'y')
puts ("Input was Y or y");
else if (niv = = 'N' || niv = = 'n')
puts ("Input was N or n");
}
Copyright © 2008 Manikkumar. All rights reserved.