0
int x = 99;
int a = 4;
int parity;
while (x < 100)
{
    if (a % 2 == 0)
    {
        parity = 0;
    }
    else
    {
        parity = 1;
    }
    switch (parity)
    {
    case 0:
        cout << "even ";
    case 1:
        cout << " odd ";
    default:
        cout << "Error";
    }
    x++;
}
int p = 1;

as the parity value hass to pass through if else statement so will there be aby independent path in which only the default case will be executed cuz when parity is 0 case:0,1and default will execute dute to fall through similarly with case1 so what will be the cyclomatic complexity (acc to me it will be 4)

1 Answers1

0

The cyclomatic complexity of the given code is 4. This is because the code contains 4 independent paths of execution. These paths are determined by the conditional statements in the code, including the if statement and the switch statement. Each of these statements creates a new independent path, and the combination of these paths determines the overall cyclomatic complexity of the code.

In this case, the if statement creates two independent paths, one for when a % 2 == 0 and one for when a % 2 != 0. The switch statement also creates two independent paths, one for each of the case statements. Together, these paths create a total of 4 independent paths of execution.

It is worth noting that the default case in the switch statement will not create a new independent path, as it will only be executed if none of the other case statements is executed. In other words, the default case is not an independent path of execution, but rather a fallback option that is only executed if the other paths fail.

EDIT

These are 4 distinct paths can be found from the code:
  • The first path is when parity is 0 and x is less than 100. In this case, the code will print "even", and then increment x by 1.

  • The second path is when parity is 1 and x is less than 100. In this case, the code will print "odd", and then increment x by 1.

  • The third path is when parity is 0 and x is not less than 100. In this case, the code will not execute the while loop, and will not print anything.

  • The fourth path is when parity is 1 and x is not less than 100. This is the same as the third path, and the code will not execute the while loop or print anything.

  • If you see the logic for setting the value of parity, for there will only be 2 independent paths. If we go through parity=0 path of if statement, the switch case path will always be case 0, 1, default and when parity=1 path of else statement, the switch case path will be case 1 and default. Hence, I suspect the cyclomatic complexity maybe 2. – Jishan Shaikh Dec 08 '22 at 08:58
  • @JishanShaikh the code contains 4 independent paths of execution, not just 2. – Khalid Saifullah Fuad Dec 08 '22 at 09:01
  • 1
    Two paths I've enlisted, please tell me the other twos with any example. Not relevant, but notice that there is no break statement in switch case. – Jishan Shaikh Dec 08 '22 at 09:02
  • @JishanShaikh Yes, there is no break statement, thats why its 4. If the break statement was present then it would be 2. – Khalid Saifullah Fuad Dec 08 '22 at 09:18
  • Break statement, as I mentioned, is not relevant here. – Jishan Shaikh Dec 08 '22 at 09:31
  • See, `a` is a constant with value 4, so its mod to 2 will always be 0, so the else statement will never get to execute. – Jishan Shaikh Dec 08 '22 at 09:32
  • 1
    The problem, with this code as constructed, is that the path through the `switch` (and cases) is uniquely determined by the preceding `if`. Additionally, the `else` will never be reached so, going into the `switch`, `parity` will always have a value of `0`. So this is a case for which code from an optimising compiler can quite feasibly (even quite likely) eliminate most of the decision points. That sort of thing makes a metric like cyclomatic complexity problematical - it depends on whether you count all paths the code permits, even if they never occur - or not. – Peter Dec 08 '22 at 10:22
  • 1
    By the definition of cyclomatic complexity, you should count all paths the code permits, even if they never occur. This metric is meant for estimating how likely the code contains bugs. Such a task should not contain any reasoning about the code's semantics. – anatolyg Dec 08 '22 at 13:24