FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
330 views
in Tutorial & Interview questions by Goeduhub's Expert (9.3k 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 (9.3k 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.

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
0 answers 316 views
0 like 0 dislike
1 answer 310 views
0 like 0 dislike
1 answer 345 views
0 like 0 dislike
1 answer 672 views
0 like 0 dislike
1 answer 396 views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...