Logical Operators
• Theseare used to combine 2 (or) more expressions logically.
• They are logical AND (&&) logical OR ( || ) and logical NOT (!)
• main ( ){
• int a= 10, b = 20, c= 30;
• printf (" %d", (a>b) && (a<c));
• printf (" %d", (a>b) | | (a<c));
• printf (" %d", ! (a>b));
8.
• A short-circuitevaluation of an expression is one in which the result is
determined without evaluating all of the operands and/or operators.
• For example, the value of the arithmetic expression
• (13 * a) * (b / 13 - 1)
• is independent of the value of (b / 13 - 1) if a is 0 , because 0 * x = 0 for
any x . So, when a is 0 , there is no need to evaluate (b / 13 - 1) or
perform the second multiplication. However, in arithmetic expressions,
this shortcut is not easily detected during execution, so it is never taken.
9.
• Problem withnon-short-circuit evaluation
index = 0;
while (index <= length)&& (LIST[index] != value)
index++;
• When index=length, LIST[index] will cause an
indexing problem (assuming LIST is length - 1
long)
• C, C++,and Java:
use short-circuit evaluation for the usual Boolean operators
(&& and ||), but also provide bitwise Boolean operators that
are not short circuit (& and |)
• All logic operators in Ruby, Perl, ML, F#, and
Python are short-circuit evaluated
• Ada: programmer can specify either (short-circuit
is specified with and then and or else)
12.
Mixed-Mode Assignment
• Assignmentstatements can also be mixed mode
• In Fortran, C, Perl, and C++, any numeric type value can be assigned to
any numeric type variable
• In Java and C#, only widening assignment coercions are done
• In Ada, there Is no assignment coercion
Widening refers to assigning a value from a smaller or more specific
type to a larger or more general type. This is considered safe because it
avoids data loss.
int num = 100;
double d = num; // Widening: int to double (allowed)
The integer value 100 is being widened to a double, and since a double
can represent integers safely, this is allowed without explicit casting.