Simplifying if-else conditions

1
2
3
4
5
6
7
if(checkA){
    if(!checkA || checkB){
        if(checkB){
            return true;
        }
    }
}

Which of the following is equivalent to the above piece of a C++ code?

A

1
2
3
if(checkA && checkB){
    return true;
}

B

1
2
3
if(checkA || checkB){
    return true;
}

C

1
2
3
if(!checkA && checkB){
    return true;
}

D

1
2
3
if(!checkA || checkB){
    return true;
}

A D B C

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Kexin Zheng
Jun 23, 2016

The second line of the code can only be reached if the first line was true, and the first line states that checkA has to be true. Therefore, the condition !checkA in the second line will never be true, which leaves the entire statement to be true only when checkB is true. Summing these two conditions, the line "return true" will only be executed when both checkA and checkB are true, giving A \boxed{A} as our answer.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...