for loop inside an if statement...

Hello,

I want to know if I have a for loop inside an if statement, and the condition of the if statement goes false earlier than when the for can finish, will the for loop be terminated automatically?
Something like this:

if(condition)
{
   for(int i = 0, i < 1000, i++)
   {
        // do something dependent on the if condition 
   }
}

Thanks

No, it will not. If that's what you want, you'll have to check the condition in the for loop too.

No. The condition is evaluated ONCE when the code reaches the IF statement, and then the whole for loop would be executed without ever checking the condition again.

There are a couple of alternatives:

   for(int i = 0; i < 1000; i++)
   {
        if (!condition) {
           break;
        }
        // do something dependent on the if condition 
   }

or, since the second part of the FOR loop is a generic statement, you could do:

   for(int i = 0; (i < 1000) && condition; i++)
   {
        // do something dependent on the if condition 
   }

Thanks a lot wildbil and westfw!

wildbill:
No, it will not. If that's what you want, you'll have to check the condition in the for loop too.

I know this is an old post, but to help others..... FardinBs' code might function if the syntax were correct:

if(condition)
{
   for(int i = 0; i < 1000; i++)
   {
        // do something dependent on the if condition
   }
}

Semicolons not commas

Tonyi:
I know this is an old post, but to help others..... FardinBs' code would function if the syntax were correct:

if(condition)

{
  for(int i = 0; i < 1000; i++)
  {
       // do something dependent on the if condition
  }
}



Semicolons not commas

Not sure why you brought up a post from 5 years ago to correct the punctuation. Furthermore, the question and answers were unrelated to the punctuation. If you put in semicolons in place of commas, the If condition is STILL evaluated only once, and if true, the entire for loop is executed, regardless of the if condition.

I tried it on my arduino Uno today, and it didn't work, but it, oddly, does work, for some, unknown (to me) reason, on my ESP 12E. Maybe you can explain how it works. Look in the void loop in the linked sketch.

ESP8266BlankSSID_password_V05B_LocationWeatherServer.ino (6.29 KB)

Tonyi:
I tried it on my arduino Uno today, and it didn't work, but it, oddly, does work, for some, unknown (to me) reason, on my ESP 12E. Maybe you can explain how it works. Look in the void loop in the linked sketch.

I will assume you mean the portion

if (payloadOn != 1)
  {
  for (int i = 0; i < 3; i++)
    {
    getWeatherCondition(); //roddy test
    delay(500);
    }
  }

What do you mean by "doesn't work"? Are you getting an error from the compiler? Are you getting behavior different than expected? What is the expected behavior? What is the result from the ESP12E? What is the result from the UNO? Are both set up in identical systems?

westfw:
No. The condition is evaluated ONCE when the code reaches the IF statement, and then the whole for loop would be executed without ever checking the condition again.

There are a couple of alternatives:

   for(int i = 0; i < 1000; i++)

{
        if (!condition) {
          break;
        }
        // do something dependent on the if condition
  }




or, since the second part of the FOR loop is a generic statement, you could do:



for(int i = 0; (i < 1000) && condition; i++)
  {
        // do something dependent on the if condition
  }

i tried both , not working for me