3 x if

Below snippets in C check if foo() and bar() returns nonzero value and execute something() if needed. Are they equivalent? Why?

    // first version
    if (foo() && bar()) {
        something();
    }
    // second version
    if (bar() && foo()) {
        something();
    }
    // third version
    if (foo()) {  
        if (bar()) {
            something();
        }
    }

3 comments:

  1. With the second version, if bar() is false then foo() won't be checked.

    ReplyDelete