for (j=-3; j<=3; j++)
{
if (( j >= 0) && (i++))
count = count + j;
}
From the above loop code, we can see that the loop iterates $7$ times for $j \in \{-3,-2,-1,0,1,2,3\}.$
Now, we have an $\text{“if”}$ condition and inside it, we have a logical AND operator (&&). In $C$ language we have the following short-circuit rule for binary logical operators
- The second operand of logical $\text{OR}$ operator
||
is ignored if the first operand is non-zero.
- The second operand of logical $\text{AND}$ operator
(&&)
is ignored if the first operand is $0$.
So, for $j \in \{-3,-2,-1\}$ the first operand of &&
operator (j >= 0)
will be $0,$ and hence the second operand (i++)
will be ignored.
For $j \in \{0,1,2,3\}$ the first operand of &&
operator (j >= 0)
will be $1,$ and hence the second operand (i++)
will get evaluated $4$ times and final value of $i = 4.$
Initial value of $i = 0.$
The postincrement operator i++
, returns the original value of $i$ and then increments $i.$ So, when the first time i++
happens, the second operator of logical $\text{AND}$ operator is $0$ and hence the $\text{“if”}$ condition fails. So, count = count +j
happens only for $j \in \{1,2,3\}$ and we get $\text{count} = 0 + 1 + 2 + 3 = 6.$
After the loop, we have count = count + i
, which makes $\text{count} = 6 + 4 = 10.$
So, the correct option is B.
Reference: https://gateoverflow.in/62409/what-is-the-output