473,670 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

short circuit evaluation

bool finished = is_done();

if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?

Sep 16 '08 #1
30 2865
"Lassie" <zh*@woaini.com writes:
if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if finished
is false that try_again() will never be executed?
Yes.

--
Jean-Marc
Sep 16 '08 #2
On Sep 16, 6:55 pm, "Lassie" <z...@woaini.co mwrote:
bool finished = is_done();

if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?

No, it does not evaluate if finished is 0.

Thus, this is correct:

return 0 && 1 / 0;

The expression (0 && 1 / 0) is evaluated like this:

(a && b)

First, a is evaluated. If a is false, then the whole expression is
replaced by 0. (false)
If a is true, then b is evaluated, and the expression is replaced by
true or false, depending on what b was evaluated.

This is also correct

return 1 || 1 / 0;

Sep 16 '08 #3
vi******@gmail. com writes:
On Sep 16, 6:55 pm, "Lassie" <z...@woaini.co mwrote:
>bool finished = is_done();

if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?


No, it does not evaluate if finished is 0.

Thus, this is correct:

return 0 && 1 / 0;

The expression (0 && 1 / 0) is evaluated like this:

(a && b)

First, a is evaluated. If a is false, then the whole expression is
replaced by 0. (false)
If a is true, then b is evaluated, and the expression is replaced by
true or false, depending on what b was evaluated.

This is also correct

return 1 || 1 / 0;
Wow what a long winded way of explaining something.

Short answer : as soon as one "false" is matched from left to right the
&& statement "exits" and returns false.
Sep 16 '08 #4
On Sep 16, 8:55*pm, "Lassie" <z...@woaini.co mwrote:
bool finished = is_done();

if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?
no it will not be evaluated ever,
but are you trying it in "C".
In "C" there is no "bool" built in data type .
If you type casted "bool" as some other built in data type then please
post as much relevant code as possible in queries in future

--
vIpIn
Sep 16 '08 #5
In article <ga**********@r egistered.motza rella.org>,
Richard <rg****@gmail.c omwrote in regards to our friend vippy:
....
>Wow what a long winded way of explaining something.
That is the way - if you are a reg wannabee.
>Short answer : as soon as one "false" is matched from left to right the
&& statement "exits" and returns false.
Another way:
&& means "evaluate while true[*]; return last evaluated expression"
|| means "evaluate until true[*]; return last evaluated expression"
[*] Or end of list reached.

Sep 16 '08 #6
On Sep 16, 7:23 pm, sh.vi...@gmail. com wrote:
On Sep 16, 8:55 pm, "Lassie" <z...@woaini.co mwrote:
bool finished = is_done();
if (finished && try_again()) { }
Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?

no it will not be evaluated ever,
but are you trying it in "C".
In "C" there is no "bool" built in data type .
If you type casted "bool" as some other built in data type then please
post as much relevant code as possible in queries in future
There's a bool macro in <stdbool.hin C99.
Sep 16 '08 #7
sh.vi...@gmail. com wrote:
On Sep 16, 8:55�pm, "Lassie" <z...@woaini.co mwrote:
bool finished = is_done();

if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?

no it will not be evaluated ever,
but are you trying it in "C".
In "C" there is no "bool" built in data type .
No, but it is a standard typedef. This is just a code fragment, which
doesn't even provide declarations for is_done() and try_again(). I
would assume that the missing code also contains a line which says

#include <stdbool.h>
If you type casted "bool" as some other built in data type then please
post as much relevant code as possible in queries in future
Agreed: explaining a problem by providing a complete program that
demonstrates it always makes for more productive responses.
Sep 16 '08 #8
On Sep 16, 7:38 pm, jameskuy...@ver izon.net wrote:
sh.vi...@gmail. com wrote:
On Sep 16, 8:55 pm, "Lassie" <z...@woaini.co mwrote:
bool finished = is_done();
if (finished && try_again()) { }
Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?
no it will not be evaluated ever,
but are you trying it in "C".
In "C" there is no "bool" built in data type .

No, but it is a standard typedef. This is just a code fragment, which
doesn't even provide declarations for is_done() and try_again(). I
would assume that the missing code also contains a line which says

#include <stdbool.h>
It is actually a macro, it's explicity mentioned in the standard to be
so.
See 7.16 p 2
Sep 16 '08 #9
ja*********@ver izon.net writes:
sh.vi...@gmail. com wrote:
>On Sep 16, 8:55�pm, "Lassie" <z...@woaini.co mwrote:
bool finished = is_done();

if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if
finished is false that try_again() will never be executed?
Or does it get evaluated sometimes?

no it will not be evaluated ever,
but are you trying it in "C".
In "C" there is no "bool" built in data type .

No, but it is a standard typedef. This is just a code fragment, which
doesn't even provide declarations for is_done() and try_again(). I
would assume that the missing code also contains a line which says

#include <stdbool.h>
Either that, or it was compiled with a C++ compiler.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 16 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
3918
by: Dave Opstad | last post by:
In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that doesn't evaluate the second argument if the first is a valid key? For now I'll code around it, but this behavior surprised me a bit...
3
3806
by: Steven Wong | last post by:
Just wondering, is it portable to write: something* pSomething = getSomething(); if( pSomething && pSomething->stuff() ) { .... }
3
2137
by: ram | last post by:
hi all, apart from readability, and not wanting the otherwise different statements to be assumed as one single loong statement what are the pros/cons of both snippets? i tried this out on a small piece of code and the latter part using short circuiting takes about half the time as the if else version; gcc 3.2 , amdk6-2 running vectorlinux; cheers
2
4120
by: webposter | last post by:
Hi, I am looking for information on a data structure (and associated algorithm) to do short-circuit evaluation of boolean expressions and haven't found a single one even after googing for two days! Can anyone point me to good resources (or implementations) that do this. Basically is there any way to optimize a boolean expression expressed in RPN (reverse polish notation)? I want to implement the algorithm/data structure in C. If you...
5
1912
by: Michael Jørgensen | last post by:
Hi there, Is there any difference between bool success = SomeFunctionReturningFalse(); success &= SomeOtherFunction(); and bool success = SomeFunctionReturningFalse();
5
1329
by: Phil Jones | last post by:
I'm just (as a matter of course) using AndAlso and OrElse statements to short circuit checking, even for nominal things like Boolean comparisons. I'm wondering, is that a good thing to do (for performance reasons) or is there some hidden down-side to it. I can see why it's good for say not checking an object ref if it doesn't exist - I'm just wondering if it's a good habit to get into for everything. Thanks for any insights. ===
6
1617
by: James Curran | last post by:
In recent weeks, I've twice come upon people who consider the short- curcuited evaluation of logical && (and ||) to be just a vendor- defined compiler quirk, and recommend against depending on it. To help me reason with these people, can anyone here: 1) Verify that short-curcuit evaluation is specified in the orginal K&R "C Programming Lanuage" (I only have the ANSI 2ed) 2) Can anyone recall a early C compiler where this was this was...
0
1819
by: Greg Nash | last post by:
G'day I have a before-update trigger a bit like .... REFERENCING OLD AS O NEW AS N .... WHEN (N.STATUS 0 AND EXISTS (SELECT EXTREF FROM MY.ET WHERE EXTREF=N.REF OR EXTREF=O.REF))
17
1926
by: somenath | last post by:
Hi All, I have one question regarding the bellow mentioned code #include<stdio.h> int main(void) { int x = 0; int y = 0;
0
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8388
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8663
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.