Go to your University || Python Lab || DSA Lab || AI & Machine Learning Lab || Linux Lab || OOPs Lab || DBMS Lab || JAVA Lab ||| Free Online Tutorials ||| 
companies for Campus Placement || Logical Reasoning || Quantitative Aptitude || General English || Technical-MCQ and Interview Questions || HR Interview Questions
0 like 0 dislike
8 views
in Tutorial & Interview questions by Goeduhub's Expert (7.8k points)

Short circuiting is a functionality that skips evaluating parts of a (if/while/...) condition when able. 

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.8k points)
 
Best answer

Short circuiting is a functionality that skips evaluating parts of a (if/while/...) condition when able. In case of a logical operation on two operands, the first operand is evaluated (to true or false) and if there is a verdict (i.e first operand is false when using &&, first operand is true when using ||) the second operand is not evaluated.

Example:

#include <stdio.h> 

int main(void) {  int a = 20;  int b = -5; 

  /* here 'b == -5' is not evaluated,     since a 'a != 20' is false. */  

if (a != 20 && b == -5) 

{    

printf("I won't be printed!\n");  

}     

return 0;

}

Check it out yourself:

#include <stdio.h> 

int print(int i) 

{  

printf("print function %d\n", i);  

return i; 

} 

int main(void) 

{  

int a = 20; 

  /* here 'print(a)' is not called,     since a 'a != 20' is false. */  

if (a != 20 && print(a)) 

{    

printf("I won't be printed!\n");  

}

  /* here 'print(a)' is called,     since a 'a == 20' is true. */  

if (a == 20 && print(a)) 

{    

printf("I will be printed!\n");  

}

  return 0; 

}

Output:

$ ./a.out 

print function 20 

I will be printed!

Short circuiting is important, when you want to avoid evaluating terms that are (computationally) costly. Moreover, it can heavily affect the flow of your program.

Related questions

0 like 0 dislike
0 answers 14 views
0 like 0 dislike
1 answer 7 views
0 like 0 dislike
1 answer 4 views
0 like 0 dislike
1 answer 9 views
0 like 0 dislike
1 answer 9 views
  Realize your learning potential with courses starting at ₹ 420
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Online Free Training:  MACHINE LEARNINGPython Programming | Database Management System(DBMS) | Object Oriented Programming(OOPs) using C++ | Data Structures and Algorithms(DSA) | Java Programming | Linux/Unix | C Programming
Exams: List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Goeduhub
About Us | Contact Us   Social::   |  | 
...