Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Short-circuit evaluation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Common use== ===Avoiding undesired side effects of the second argument=== Usual example, using a [[C (programming language)|C-based]] language: <syntaxhighlight lang="c"> int denom = 0; if (denom != 0 && num / denom) { ... // ensures that calculating num/denom never results in divide-by-zero error } </syntaxhighlight> Consider the following example: <syntaxhighlight lang="c"> int a = 0; if (a != 0 && myfunc(b)) { do_something(); } </syntaxhighlight> In this example, short-circuit evaluation guarantees that <code>myfunc(b)</code> is never called. This is because <code>a != 0</code> evaluates to ''false''. This feature permits two useful programming constructs. # If the first sub-expression checks whether an expensive computation is needed and the check evaluates to ''false'', one can eliminate expensive computation in the second argument. # It permits a construct where the first expression guarantees a condition without which the second expression may cause a [[run-time error]]. Both are illustrated in the following C snippet where minimal evaluation prevents both null pointer dereference and excess memory fetches: <syntaxhighlight lang="cpp"> bool is_first_char_valid_alpha_unsafe(const char *p) { return isalpha(p[0]); // SEGFAULT highly possible with p == NULL } bool is_first_char_valid_alpha(const char *p) { return p != NULL && isalpha(p[0]); // 1) no unneeded isalpha() execution with p == NULL, 2) no SEGFAULT risk } </syntaxhighlight> ===Idiomatic conditional construct=== Since minimal evaluation is part of an operator's semantic definition and not an optional [[compiler optimization|optimization]], a number of coding idioms rely on it as a succinct conditional construct. Examples include: [[Perl]] idioms: <syntaxhighlight lang="perl"> some_condition or die; # Abort execution if some_condition is false some_condition and die; # Abort execution if some_condition is true </syntaxhighlight> [[POSIX shell]] idioms:<ref>{{cite web |url=https://unix.stackexchange.com/questions/190543/what-does-mean-in-bash |title=What does {{!}}{{!}} mean in bash? |publisher=stackexchange.com |access-date=2019-01-09}}</ref> <syntaxhighlight lang="bash"> modprobe -q some_module && echo "some_module installed" || echo "some_module not installed" </syntaxhighlight> This idiom presumes that <code>echo</code> cannot fail.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)