Home Segments Index Top Previous Next

183: Sidetrip

It is possible to use && instead of an if statement by exploiting the property that the right-side operand of an && expression is evaluated only if the value of the left-side operand is not 0. Thus, the following two expressions are equivalent:

if (ratio > 30) printf ("It is expensive!\n"); 
  
(ratio > 30) && printf ("It is expensive!\n"); 

Similarly, it is possible to use || instead of an if-else statement by exploiting the property that the right-side operand of an || expression is evaluated only if the left-side operand evaluates to 0. Thus, the following two expressions are equivalent:

if (ratio > 30) ; else printf ("It is NOT expensive!\n"); 
  
(ratio > 30) || printf ("It is NOT expensive!\n");