are multiple statements inside an if the same as multiple ifs?

Viewed 129

is

if(a == 0 && b == 0 && c == 0) { return; }

the same as

if(a == 0) { return; }

if(b == 0) { return; }

if(c == 0) { return; }

or

if(a == 0) { return; }

else if(b == 0) { return; }

else if(c == 0) { return; }

I want to write fast code and am interested in how different these statements are interpreted into assembly instructions usually.

2 Answers
if(a == 0 && b == 0 && c == 0) { return; }

This is equivalent to:

if(a == 0)
  if(b == 0)
    if(c == 0)
      return;

Whereas, these two:

//1
if(a == 0) { return; }
if(b == 0) { return; }
if(c == 0) { return; }
//2
if(a == 0) { return; }
else if(b == 0) { return; }
else if(c == 0) { return; }

are equivalent to if( a==0 || b == 0 || c == 0){ return; }.

As mentioned above

if(a == 0 && b == 0 && c == 0) { return; }

is the same as

if(a == 0) 
 if(b == 0) 
  if(c == 0) 
    return;

and

if(a == 0) { return; }

else if(b == 0) { return; }

else if(c == 0) { return; }

is equal to

if(a == 0 || b == 0 || c == 0) { return; }

but only in this example, because we return 0 in every if state.

Related