Question: I argued with a friend the other day about those two snippets. Which is faster and why ?

```
value = 5;
if (condition) {
    value = 6;
}

```

and:

```
if (condition) {
    value = 6;
} else {
    value = 5;
}

```

What if `value` is a matrix ?

Note: I know that `value = condition ? 6 : 5;` exists and I expect it to be faster, but it wasn't an option.

**Edit** (requested by staff since question is on hold at the moment):

* please answer by considering either *x86 assembly* generated by mainstream compilers (*say g++, clang++, vc, mingw*) in both optimized and non optimized versions or *MIPS assembly*.
* when assembly differ, explain why a version is faster and when (*e.g. "better because no branching and branching has following issue blahblah"*)

Answer: 
The answer from [CompuChip](https://stackoverflow.com/a/43202221/2805305) shows that for `int` they both are optimized to the same assembly, so it doesn't matter.

> 
> What if value is a matrix ?
> 
> 
> 

I will interpret this in a more general way, i.e. what if `value` is of a type whose constructions and assignments are expensive (and moves are cheap).

then

```
T value = init1;
if (condition)
   value = init2;

```

is sub-optimal because in case `condition` is true, you do the unnecessary initialization to `init1` and then you do the copy assignment.

```
T value;
if (condition)
   value = init2;
else
   value = init3;

```

This is better. But still sub-optimal if default construction is expensive and if copy construction is more expensive then initialization.

You have the conditional operator solution which is good:

```
T value = condition ? init1 : init2;

```

Or, if you don't like the conditional operator, you can create a helper function like this:

```
T create(bool condition)
{
  if (condition)
     return {init1};
  else
     return {init2};
}

T value = create(condition);

```

Depending on what `init1` and `init2` are you can also consider this:

```
auto final_init = condition ? init1 : init2;
T value = final_init;

```

But again I must emphasize that this is relevant only when construction and assignments are really expensive for the given type. And even then, only by **profiling** you know for sure.
Ok, since assembly is one of the tags, I will just assume your code is pseudo code (and not necessarily c) and translate it by human into 6502 assembly.

1st Option (without else)

```
        ldy #$00
        lda #$05
        dey
        bmi false
        lda #$06
false   brk

```

2nd Option (with else)

```
        ldy #$00
        dey
        bmi else
        lda #$06
        sec
        bcs end
else    lda #$05
end     brk

```

Assumptions: Condition is in Y register set this to 0 or 1 on the first line of either option, result will be in accumulator.

So, after counting cycles for both possibilities of each case, we see that the 1st construct is generally faster; 9 cycles when condition is 0 and 10 cycles when condition is 1, whereas option two is also 9 cycles when condition is 0, but 13 cycles when condition is 1. (*cycle counts do not include the `BRK` at the end*).

**Conclusion: `If only` is faster than `If-Else` construct.**

And for completeness, here is an optimized `value = condition + 5` solution:

```
ldy #$00
lda #$00
tya
adc #$05
brk

```

This cuts our time down to 8 cycles (*again not including the `BRK` at the end*).