Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Questions on short circuiting, c programming?

Which parts of the expressions are not evaluated due to short circuiting?

int a = 10;

int b = -5;

int c = 15;

int flag = 1;

(a) c == b - a || !flag

(b) a != -a || !flag && c <= (-b * a)

(c) !(a % b) && b <= -5

(d) (a > 15) || c < a + b

(e) !!a == 10 && c >= 15 || b

I think the answers are:

a. Everything after the b in the equation gets short circuited.

b. Everything after the c gets short circuited.

c. Everything after the second b gets short circuited.

d. Everything after the 15.

e. Nothing.

I am not sure and am really bad at this, could some explain to me the idea and the solutions. And any errors in my thinking. Thanks.

Update:

Yeah this is homework questions and it deals with order of operations. So I cannot change what is printed.

5 Answers

Relevance
  • 9 years ago
    Favorite Answer

    The purpose of this is not only order of operations, but short circuiting is useful in that entire sections of code are not evaluated if the first part is false. This is useful because something evaluating as null may crash the program, therfore you test for it in the same statement and if something is null then you don't even get to the more complicated evaluation. Make sense?

    So here are the answers.

    a. Everything on the left side of the || gets evaluated. the right side is ignored. Your version is wrong because the "a" gets evaluated.

    b. Everything on the right side of the && gets ignored. Your answer is wrong because the "c" is ignored as well. The expression is true because the left side is true.

    c. The whole expression gets evaluated because the left half is true. Incidently the whole expression is true.

    d. Everything on the right side of the || gets ignored. I am not sure here what your definition of short circuit is, so I am using "evaluated and ignored". Your answer may be wrong because the "||" is actually evaluated and is important to the result.

    e. This one is a bit of a trick because "&&" gets evaluated before the "||". So, the expression evaluates to true, and it does so BEFORE we take into consideration the right side of the "||". The "b" is irrelevant and is not evaluated.

    I am afraid you have scored 0%. I hope I have helped.

  • Anonymous
    9 years ago

    This may prove useful:

    http://en.cppreference.com/w/cpp/language/operator...

    Since C++ is backwards compatible with most C code, I assume the operator precedence is the same in both languages. Just ignore the operators C doesn't have. I'm sorry I can't be of more help, but like the other poster, I've never learned or needed to know exactly which operators are evaluated when.

  • 9 years ago

    Without looking at the specfic question I can tell you what's involved.

    Logical expressions are evaluated left to right.

    Thus in:

    A || B

    if A is true then the expression is true regardless of the value of B. Thus many logical expressions are only evaluated as far as needed to get the final value.

    In

    A && B

    if A is false the there's no need to know the value of B because the expression is necessarily false.

  • Anonymous
    9 years ago

    I never memorized the order of operations, but I try to use paranthesis when a statement can be made clearer with them. If your instructor is making you memorize that, instead of giving interesting assignments, he/she is a geek.

    Yes I can see it deals with order of operations, that is what I said, and it deals with "short circuiting" and it deals with third grade arithmetic. Short circuiting is used all the time, but nobody in their right mind would use an expression like (a) without paranthesis unless they were a total geek.

  • How do you think about the answers? You can sign in to vote the answer.
  • Anonymous
    4 years ago

    Out of interest have you ever written in Perl. In that language any cost which couldn't 0 evaluates to real (and nil is pretend). right here there aren't any logical expressions that evaluate to real or pretend, which makes it complicated to verify what's going to extremely ensue.

Still have questions? Get your answers by asking now.