Having read SO, just found this question:
if (x > y)
print (x)
else if (x < y)
print (y)
else
print (x,y)
How many branches and decisions are there? It mentiones there should be 3 decision and 5 branches, yet I cannot see how. For me I can only imagine 4 branches (2 Ifs with two possible results).
Original question here
if
/ < ---- 2 branches
false true
else
if
/ < ---- 2 branches
false true
else
| < ---- 5th and final branch
as else part gives a decision, it is a branch too
12
I think it depends a bit on how you would define decision and branch. From a logical perspective I would say that there are 3 branches, because of the 3 calls to print
. Since two if
s are used, there are two decisions made.
On a machine level this might look different, because the compiler will generate code that might involve more branches and decisions. For example the compiler could generate code for the test x==y
.
Investigating this a bit more, I concluded that the statement about this code having 5 branches is incorrect.
This code definitely has only 4 branches – 4 edges to traverse.
As IF has always two branches (one for true and one for false), with two IFs there are 4 branches in the code.
The diagram of this case (IF, ELSE IF, ELSE) is as follows. You can easily see only four branches: