Logical Operators
The logical operators are used to form more complex and useful logical conditions. For this purpose, two or more relational expressions are combines using logical operators.C-language provides three logical operators:
(i) AND Operator (&&)
(ii) OR Operator (||)
(iii) NOT Operator (!)
i) AND Operator (&&)
The AND operator is denoted by && (double ampersand signs). It is used to combine two relational expressions. It returns True if both expressions are True. It returns False if any one of the expressions is False.ii) OR Operator (||)
The OR operator is denoted by || (double pipe signs). It is used to combine two relational expressions. It returns True if either of the expressions is True. It returns false if both relational expressions are False.iii) NOT Operator (!)
The NOT operator is denoted by ! sign. It is used to reverse the result of the relational expression opr condition. It returns True if condition is False and returns False if condition is True.Logical Expression
An expression that is combination of relational expressions (or simple operands) joined together by logical operators is called logical expression. The logical expression is also known as compound expression. Like relational expression, the logical expression also returns true (or 1) or false (or 0).Suppose a = 10, b = 5, and c = 10. The following table shows the output of the compound expressions:
Compound Expression
|
Output
|
( a > b )
&& ( a > c )
|
False
|
( a > b )
|| ( a = c )
|
True
|
!( a > b )
|
False
|
Examples
If 'total', 'math', 'eng' and 'computer' are four integer variables. The variable 'total' stores the sum of the values of variables 'math', 'eng' and 'computer'.
- To find out if the value of variable 'total' is greater than or equal to 700 and the value in variable 'computer' is greater than 90, the compound expression is written as
- To find out if value of 'total' is greater than 600 or value in 'eng' is greater than 70, the compound expression is written as;
- To find out if value in variable 'computer' is not greater than 33, the expression is:
No comments:
Post a Comment
Thanks For Visiting Here...!! I will Reply you as soon as possible.