Is nested If-Statement like (And condition)!

Are these two codes the same, if not, then why?!

if ((val == 1) && (old_val == 0))
{
if (state == LOW)
{
state = HIGH;
}
else {
state = LOW;
}
}

if ((val == 1) && (old_val == 0) && (state == LOW))
{

state = HIGH;
}
else {
state = LOW;
}

They are obviously not the same so I assume that you are asking whether they are equivalent

Formatted using Auto format in the IDE they look like this

if  ((val == 1) && (old_val == 0))
{
  if (state == LOW)
  {
    state = HIGH;
  }
  else
  {
    state = LOW;
  }
}

if  ((val == 1) && (old_val == 0) && (state == LOW))
{
  state = HIGH;
}
else
{
  state = LOW;
}

Now it is easier to see that they are not equivalent because in the second one state will be set to LOW if any of the tests fail whereas in the first one it will be set to LOW only if val equals 1, old_val equal 0 and state is LOW

thanks for reply

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.