nested if statements and for loops altering test conditions

I am working with if statements and for loops and I am trying to understand what should happen when the nesting test conditions are altered by commands within a nested block.

Such as this test sketch I am trying to use for learning/debugging:

int a, b, c, d, e, f, i, j, k;

void setup() {
}

void loop() 
{
  if(i == 0)
  {
    if(j == 0)
    {
      a = 1;
      b = 2;
      c = 3;
      i = 1;
      d = 4;
      e = 5;
      for (k = 0; k < 5; k++)
      {
        f = f + 1
        if(f == 3)
        {
          j=1;
        }
      }
    }
  }
}

What should be happening in this sketch? Specifically:

  1. When i is assigned to 1 should the rest of the statements in the “i if test” block be executed?

  2. When j is assigned to 1 in the for loop should the rest of the for loop be completed, and should the nesting “if test” blocks be immediately stopped?

Thanks

Can you change the topic title, please? You have no nested loops.

Ok, here is a test sketch I am running:

int a, b, c, d, e, f, i, j, k, loopCounter;

void setup() {
  Serial.begin(9600);
}

void loop() 
{
  Serial.print("loopCounter = ");
  Serial.println(loopCounter);
  if(i == 0)
  {
    Serial.print("i = ");
    Serial.println(i);
    if(j == 0)
    {
      Serial.print("j = ");
      Serial.println(j);
      a = 1;
      b = 2;
      c = 3;
      i = 1;
      d = 4;
      e = 5;
      Serial.println("vars set, i = 1");
      for (k = 0; k < 5; k++)
      {
        Serial.print("k = ");
        Serial.println(k);
        Serial.print("f = ");
        Serial.println(f);
        f = f + 1;
        if(f == 3)
        {
          Serial.println("f = 3, j = 1");
          j=1;
        }
      }
    }
  }
  delay(2000);
  loopCounter = loopCounter + 1;
}

and this is the serial monitor output displayed:

loopCounter = 0
i = 0
j = 0
vars set, i = 1
k = 0
f = 0
k = 1
f = 1
k = 2
f = 2
f = 3, j = 1
k = 3
f = 3
k = 4
f = 4
loopCounter = 1
loopCounter = 2
loopCounter = 3
...

So I am assuming that anything you do within an if statement or for loop does not affect the test conditions until all the included statements within the block are complete. Is this generally true for all combinations of nested if blocks and for-loop blocks?

Thanks

I'm not sure I understand the question.

If your question is, “Does the Arduino magically go back and figure out what might have happened because a value changed later?” the answer is no. Instructions are run one at a time, from top to bottom of the code. Once you pass a conditional statement then you’ve passed it until you get to it again.

Yes I was was somehow assuming the test condition was evaluated before each statement in the block.

Could you answer a related question if you don't mind, if the value of a global variable is changed in an if test block or for loop block when should the value change be sensed by another test such as by an if test in a timer interrupt ?

Thanks

  1. When i is assigned to 1 should the rest of the statements in the "i if test" block be executed?

I is not assigned to 1. The value of i is set to 1. The fact that i is set to 1 does not alter the fact that when it was tested, it contained the value of 0. So, changing the value of i later does not abort the block that is executing.

  1. When j is assigned to 1 in the for loop should the rest of the for loop be completed

Since the while clause of the loop is not concerned about j, the answer is "of course".

perigalacticon: Yes I was was somehow assuming the test condition was evaluated before each statement in the block.

Could you answer a related question if you don't mind, if the value of a global variable is changed in an if test block or for loop block when should the value change be sensed by another test such as by an if test in a timer interrupt ?

Thanks

It will be noticed by whatever line of code the next time that line runs.

if the value of a global variable is changed in an if test block or for loop block when should the value change be sensed by another test such as by an if test in a timer interrupt ?

The next time that the variable is tested. Nothing happens retroactively.