# How to calculate condition coverage?

I am trying to prepare to test and I see the next exercise in internet:

How many test cases are needed to achieve 100 % condition coverage?

``````if ((temperature < 0) or (temperature > 100)) {
if ((speed > 100) and (load <= 50))}
speed = 50;
}
} else {
check = false;
}
A. 5
B. 4
C. 2
D. 3
``````

The right answer is "A. 5". Why? Could someone explain it?

I tried to make test cases that use every value for every atomic condition:

t = -1, 1, 101

s = 99, 101

l = 49, 51

and if we have to take combination of all values, I can do it with 3 cases, not 5. Where am I wrong?

• Are you after statement or condition coverage? The title says condition, but your quoted question statement. – pavelsaman Sep 16 at 20:19
• sorry, my bad, the question is about condition testing – Dem0n13 Sep 16 at 21:11

This is the table showing the values which impact the conditions in your code: Since there are four lines with temperature `<0, >100` condition, you can pick different values near both the temperature threshold levels for different tests.

With your reference to your test case what you have provided is correct but think how the application/function will take the input. It can take only three inputs so when your conditions are split based on the input then we need 5 test cases to execute it.

t = -1, 1, 101

s = 99, 101

l = 49, 51

so your test case will be like

1. t=1 s=99 l=49 (positive)
2. t=-1 s=99 l=49 (negative for temperature)
3. t=101 s=99 l=49 (negative for temperature)
4. t=1 s=99 l=51 (negative for load)
5. t=1 s=101 l=49 (negative for speed)

Think

## Boundary Testing

based on the conditions shown.

The conditions are:

• t < 0
• t > 0 and t < 100 and...
• s > 100 and l <= 50
• s > 100 and l > 50
• s < 100 or l > 50
• s < 100 and l < 50
• I can't follow your answer, why did you take t < 0 as the first condition? I have t > 0 and t < 100 for false condition in my example. And I have three different boundaries for t: t < 0 (true) t > 0 and t < 100 (false) and t > 0 (true) – Dem0n13 Sep 16 at 21:13

If you are after statement coverage, that is, ensuring that all lines of the code sample have been tested, then you'll want to use these test cases. The idea is to test the logical conditions with different boundary values.

1. Temperature alerts "danger" when using values -1 (or lower)
2. Temperature alerts "danger" when using values 101 (or higher)
3. Temperature alerts "danger" and speed is changed to 50 when temperature is -1 (or lower) AND speed is 101 (or higher) and load 50 (or lower)
4. Temperature alerts "danger" and speed is changed to 50 when temperature is 101 (or higher) AND speed is 101 (or higher) and load 50 (or lower)
5. Check is false when temperature is between 0 and 100, "danger" is not alerted

Or, to rephrase in a different way, here's how to look at the code conditions:

Number 1:

``````if (temperature < 0) {
}
``````

Number 2:

``````if (temperature > 100) {
}
``````

Number 3:

``````if (temperature < 0) {
if ((speed > 100) and (load <= 50))}
speed = 50;
}
}
``````

Number 4:

``````if (temperature > 100) {
if ((speed > 100) and (load <= 50))}
speed = 50;
}
}
``````

Number 5:

``````else {
check = false;
}
``````
• I am sorry, my bad, I am about condition testing. – Dem0n13 Sep 16 at 21:12
• My answer remains the same. Each test listed does test each logical condition. – Lee Jensen Sep 16 at 22:02
• why don't we test the second condition when it false? – Dem0n13 Sep 16 at 22:19
• You need to understand the logical OR. In the first if statement, you can't test for temperature of t < 0 or t > 100 at the same time; t cannot be both of these at the same time. You have to test these as two separate test cases. If you have a logical AND, as in the second if statement, then those variable values get tested together. – Lee Jensen Sep 16 at 22:42