Software Engineering
![Question 38, GATE 2015: Find cyclomatic complexity of: while(first<=last){if(array[middle]<search) first=middle+1; else if(array[middle]==search) found=TRUE; else last=middle-1; middle=(first+last)/2;} if(first>last) notPresent=TRUE;.](../../../assets/questions/CS-2015-S1-Q38-1.webp)
Answer & solution
Official answer: 5
For a single connected control-flow graph with binary conditions, McCabe complexity is one plus the number of decisions. Count while, if(<), else-if(==), and the final if(>): four decisions, hence five. An unconditional else is not an additional test; assignments are not branches. The code can fail to terminate after finding the value, but that semantic defect does not remove a syntactic decision from its control-flow graph.
Solution: The control-flow graph has four binary decisions: the while condition, the first if, the else-if, and the final if. For one connected entry/exit graph, cyclomatic complexity is decisions+1=5. This graph count does not require every syntactic path to be feasible or terminating.

