1

The Cyclomatic Complexity of the pseudocode below is "4".

Read A
Read B
IF A > 0 THEN
     IF B  = 0 THEN
    Print “No values”
     ELSE
    Print B
    IF A > 21 THEN
        Print A
    ENDIF
     ENDIF
ENDIF

How do we count it? I heard that it's # of conditions + 1? Do we count those else statements? I'm confused.

EDIT: Case 2: What if we have:

IF (x < y)
statment 1

IF (x < z)
statemnt 2

What will be the Cyclomatic complexity? 2? or 3?

Bart
  • 13
  • 1
  • 4
  • 1
    possible duplicate of [What is Cyclomatic Complexity?](http://stackoverflow.com/questions/911637/what-is-cyclomatic-complexity) – WW. Jul 13 '11 at 23:55
  • 1
    For Case 2 the answer is 3 becuase of Equation **M = Total Condition Count + 1** – Ravi Mehta Aug 27 '15 at 14:00

1 Answers1

2

No. Main flow + 3 x 'If's = 4

From the wiki:

The cyclomatic complexity of a section of source code is the count of the number of linearly independent paths through the source code. For instance, if the source code contained no decision points such as IF statements or FOR loops, the complexity would be 1, since there is only a single path through the code.

If the code had a single IF statement containing a single condition there would be two paths through the code, one path where the IF statement is evaluated as TRUE and one path where the IF statement is evaluated as FALSE.

Community
  • 1
  • 1
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541