if (x>0 && x<6)
{
break;
}
else if(x>6)
{
break;
}
versus
if (x>0)
{
if (x<6)
{
break;
}
}
else
{
if (x>6)
{
break;
}
}
Code 1 doesn't work but code 2 does. Why? I'm a super noobie at programming so please any help would be nice. Programming language is C.
The else statement in the second code snippet never gets the control for any positive value of
x
because the condition of the first if statement at once evaluates to logical true.While in the first code snippet the else statement will get the control for any value of
x
equal to or greater than 6.That is if
x
for example is equal to7
then the condition in the first if statementevaluates to logical false. So the else statement gets the control and the condition of the if sub-statemen also evaluates to logical true.