Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested if statements and "&&" operator

if(a() && b() && c() && d())
   doSomething();


if(a())
   if(b()) 
      if(c())
         if(d())
            doSomething();

Is there "any" performance difference between these two?

For example, in a situation that a() turns 0, will it keep running b(), c() and d() in the first if statement? Or will it work same as the second nested if statement?

like image 467
user3858240 Avatar asked Aug 24 '14 15:08

user3858240


People also ask

Can you use ifs and and together in Excel?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

Can IF statement have 2 conditions?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

Can you nest the AND or OR functions within an IFS function?

nesting and, or, not inside an if function. Using AND, OR, NOT in conjunction with the IF function will let you return custom outputs or run calculations. You can automate the evaluation of logical tests by NESTING the AND, OR, NOT functions inside a single IF function.


1 Answers

They're exactly identical.

To test this yourself, run gcc -S test.c (presuming that this is where you've put your source) and observe the contents of test.s.


Here's how the nested-if approach compiles in gcc 4.8.1 with default options (annotated with comments):

main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $0, %eax
    call    A                        # try to call A
    testl   %eax, %eax               # look at its return value
    je  .L3                          # short-circuit if it returned 0
    movl    $0, %eax                 # ...repeat for B, et al.
    call    B
    testl   %eax, %eax
    je  .L3
    movl    $0, %eax
    call    C
    testl   %eax, %eax
    je  .L3
    movl    $0, %eax
    call    D
    testl   %eax, %eax
    je  .L3
    movl    $0, %eax
    call    doSomething
.L3:
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc

Here's how the && approach compiles:

main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $0, %eax
    call    A                           # try to call A
    testl   %eax, %eax                  # look at its return value
    je  .L3                             # short-circuit if it returned 0
    movl    $0, %eax                    # ...repeat for B, et al.
    call    B
    testl   %eax, %eax
    je  .L3
    movl    $0, %eax
    call    C
    testl   %eax, %eax
    je  .L3
    movl    $0, %eax
    call    D
    testl   %eax, %eax
    je  .L3
    movl    $0, %eax
    call    doSomething
.L3:
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
like image 101
Charles Duffy Avatar answered Oct 11 '22 02:10

Charles Duffy