Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Having read SO, just found this question:

if (x > y)
     print (x)
else if (x < y)
     print (y)
     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

share|improve this question
          /  \    < ---- 2 branches
      false true
      /  \        < ---- 2 branches
   false true
    |             < ---- 5th and final branch 

as else part gives a decision, it is a branch too

share|improve this answer
That is why I do not understand either. There is no decision in the else part, it just a result of FALSE in the previous ELSE IF. – user970696 Jan 31 '13 at 8:55
@user970696 See, when a true or false condition of an IF is met you do some operation in it, in the same way you do an operation even when an ELSE part is reached. As that ELSE part has some functionality, it is taken as a branch. – Vivekanand S V Jan 31 '13 at 9:02
Exactly but when the ELSE IF evaluates to FALSE, no operation can be done and the flow continues with ELSE. What can you do between ELSE IF>FALSE and ELSE? – user970696 Jan 31 '13 at 9:05
If the test-is-false condition and the else part count as separate branches for the second if, then why not for the first, making it a total of 6 branches? – Bart van Ingen Schenau Jan 31 '13 at 9:18
@BartvanIngenSchenau You have 2 decision outcomes for both IFs..but it would make 4. The last ELSE just continues from the False branch of the second IF. That is why I think 4 is enough. – user970696 Jan 31 '13 at 9:23

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 ifs 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.

share|improve this answer
up vote 0 down vote accepted

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:

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.