GOEDUHUB Online Courses || Last Batch Student's Projects || COVID-19 Projects(AI-ML) || Universities  ||  Placement Preparation  
+91-7976731765 Free Online Tutorials ||  MACHINE LEARNING || Python || DBMS || OOPs || DSA || Java || Linux/Unix ||  C Programming
0 like 0 dislike
16 views
in Tutorial & Interview questions by Goeduhub's Expert (8k points)

Logical AND

Performs a logical boolean AND-ing of the two operands returning 1 if both of the operands are non-zero. The logical AND operator is of type int.

0 && 0  /* Returns 0. */ 

0 && 1  /* Returns 0. */ 

2 && 0  /* Returns 0. */ 

2 && 3  /* Returns 1. */

Logical OR

Performs a logical boolean OR-ing of the two operands returning 1 if any of the operands are non-zero. The logical OR operator is of type int.

0 || 0  /* Returns 0. */

0 || 1  /* Returns 1.  */ 

2 || 0  /* Returns 1.  */ 

2 || 3  /* Returns 1.  */

Logical NOT

Performs a logical negation. The logical NOT operator is of type int. The NOT operator checks if at least one bit is equal to 1, if so it returns 0. Else it returns 1;

!1 /* Returns 0. */ 

!5 /* Returns 0. */ 

!0 /* Returns 1. */

Short-Circuit Evaluation

There are some crucial properties common to both && and ||:

  • the left-hand operand (LHS) is fully evaluated before the right-hand operand (RHS) is evaluated at all, 
  • there is a sequence point between the evaluation of the left-hand operand and the right-hand operand, 
  • and, most importantly, the right-hand operand is not evaluated at all if the result of the left-hand operand determines the overall result.

This means that:

  • if the LHS evaluates to 'true' (non-zero), the RHS of || will not be evaluated (because the result of 'true OR anything' is 'true'), 
  • if the LHS evaluates to 'false' (zero), the RHS of && will not be evaluated (because the result of 'false AND anything' is 'false').

This is important as it permits you to write code such as:

const char *name_for_value(int value) 

{    

static const char *names[] = { "zero", "one", "two", "three", };    

enum { NUM_NAMES = sizeof(names) / sizeof(names[0]) };    

return (value >= 0 && value < NUM_NAMES) ? names[value] : "infinity"; 

}

If a negative value is passed to the function, the value >= 0 term evaluates to false and the value < NUM_NAMES term is not evaluated.

Please log in or register to answer this question.

Related questions

0 like 0 dislike
1 answer 15 views
0 like 0 dislike
1 answer 7 views
0 like 0 dislike
1 answer 15 views
0 like 0 dislike
1 answer 17 views
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...