Very basic if else question

Hello are the following two code blocks actually doing the same? If not what is the difference please.

If(a == 1) b = 1;
else if (a == 2) b= 2;
else b = 3;


If(a == 1) b = 1;
If(a == 2) b = 2;
If(a == 3) b = 3;

Is there an advantage to one over the other?


No, they are not the same.

(Hint: what if a is 5? sp. “if”)

In the top snippet, if an if is true no more lines will be executed. So, if a == 1 the rest of the statements will not be executed. Or if a == 2 the else will not be executed.

In the bottom snippet, all 3 lines will always be executed, even if the first is true.

Got it, thanks!