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
xbecause 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
xequal to or greater than 6.That is if
xfor example is equal to7then 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.