about arduino IDE complier with (while+for) loop

i found the problem I dont know how to explain that
this problem maybe related with IDE complier?

this is my TEST code

#define SubFunction1 0
#define SubFunction2 1
#define DirectForMode1 2 //Didnt work
#define DirectForMode2 3

#define testMode DirectForMode2 // SubFunction1 SubFunction2 DirectForMode DirectForMode2
#define BaudRate 115200

void setup()
{
Serial.begin(BaudRate);

}

void loop()
{
while(1)
{
#if testMode == SubFunction1
test();
#elif testMode == SubFunction2
test2();
#elif testMode == DirectForMode1
for(int i,j = 0; i < 20; i++)
{
Serial.println("Test while+for for(int i,j = 0; i < 20; i++)");
}
#else //DirectForMode2
for(int i = 0,j = 0; i < 20; i++)
{
Serial.println("Test while+for for(int i = 0,j = 0; i < 20; i++)");
}
#endif
}
}

void test(void)
{
for(int i,j = 0; i < 20; i++)
{
Serial.println("Test while+for with SubFunction for(int i,j = 0; i < 20; i++)");
}
}

void test2(void)
{
for(int i = 0,j = 0; i < 20; i++)
{
Serial.println("Test while+for with SubFunction for(int i = 0,j = 0; i < 20; i++)");
}
}

this code have 4 situation(situation 3 is Fail)
1.SubFunction1(with Subroutine in while(1) and for(int i,j = 0; i < 20; i++))
2.SubFunction2(with Subroutine in while(1) and for(int i = 0,j = 0; i < 20; i++))
3.DirectForMode1(without Subroutine in while(1) and for(int i,j = 0; i < 20; i++))
4.DirectForMode2(without Subroutine in while(1) and for(int i = 0,j = 0; i < 20; i++))

in situation 1 and 3
i use (for(int i,j = 0; i < 20; i++)) ,only different part situation1 is use Subroutine ,situation3 not use Subroutine

in situation 4 and 3
i write code in while(1) directly,only different part situation4 is use for(int i = 0,j = 0; i < 20; i++),situation3 use for(int i,j = 0; i < 20; i++)

i cant explain code doesnt work in situation3 ,i am just curiously

sketch_apr15c.ino (1.12 KB)

  while(1)Why ?

What is it that you are trying to achieve with those preprocessor conditionals ?

I don't think that "int i,j=0" does what you think it does.
I don't think it initializes I at all...

  #define testMode DirectForMode2             //  SubFunction1     SubFunction2   DirectForMode   DirectForMode2

Do you use "DirectForMode" as it says in the comment, or "DirectForMode1" which is a defined name?

I also agree with westfw: you failed to initialize your loop variable. Your loop would more properly be written:

      for(int i = 0; i < 20; i++)
      {
        int j=0;
        Serial.println("Test while+for    for(int i = 0; i < 20; i++)");
      }

Hi johnwasser and westfw :

#define testMode DirectForMode2 this is my test
i can set "testMode" to other four condition,it is easy way to test condition only change testMode parameter
so you can test these condition easily

and, i change my code following:

#define SubFunction1 0
#define SubFunction2 1
#define DirectForMode1 2 //Didnt work
#define DirectForMode2 3

#define testMode DirectForMode2 // SubFunction1 SubFunction2 DirectForMode DirectForMode2
#define BaudRate 115200

void setup()
{
Serial.begin(BaudRate);
Serial.print("HI Setup");
}

void loop()
{
Serial.print("HI Loop");
while(1)
{
Serial.println("HI While");
#if testMode == SubFunction1
test();
Serial.println("SubFunction1");
#elif testMode == SubFunction2
test2();
Serial.println("SubFunction2");
#elif testMode == DirectForMode1
for(int i,j = 0; i < 20; i++)
{
j++;
Serial.print("i =");Serial.print(i);Serial.print(" ||");Serial.print("j =");Serial.print(j);Serial.print(" ||");
Serial.println("Test while+for for(int i,j = 0; i < 20; i++)");
Serial.println("DirectForMode1");
}
#else //DirectForMode2
for(int i = 0,j = 0; i < 20; i++)
{
j++;
Serial.print("i =");Serial.print(i);Serial.print(" ||");Serial.print("j =");Serial.print(j);Serial.print(" ||");
Serial.println("Test while+for for(int i = 0,j = 0; i < 20; i++)");
Serial.println("DirectForMode2");
}
#endif
}
}

void test(void)
{
for(int i,j = 0; i < 20; i++)
{
j++;
Serial.print("i =");Serial.print(i);Serial.print(" ||");Serial.print("j =");Serial.print(j);Serial.print(" ||");
Serial.println("Test while+for with SubFunction for(int i,j = 0; i < 20; i++)");
}
}

void test2(void)
{
for(int i = 0,j = 0; i < 20; i++)
{
j++;
Serial.print("i =");Serial.print(i);Serial.print(" ||");Serial.print("j =");Serial.print(j);Serial.print(" ||");
Serial.println("Test while+for with SubFunction for(int i = 0,j = 0; i < 20; i++)");
}
}

in situation : SubFunction1
i use SerialWindow to print i and j value
and my coding way"for(int i,j = 0; i < 20; i++)" is OK~
it can print i and j counter value
so i dont think is the wrong coding

my question is in these situation(SubFunction1, SubFunction2, DirectForMode2) is success to work
but in condition DirectForMode1 is unsuccessful
you can see my program print string"HI Loop" when program go into "loop"
print string"HI while" when program go into "while(1)"
print string"SubFunction1" when program "testMode == SubFunction1"
print string"SubFunction2" when program "testMode == SubFunction2"
print string"DirectForMode2" when program "testMode == DirectForMode2"
but cant print "DirectForMode1"
because the program is not go into "for"loop(for(int i,j = 0; i < 20; i++))
but in the same condition "for" loop(for(int i,j = 0; i < 20; i++)) in situation SubFunction1
it is success to print the string"SubFunction2",so i dont think my for loop is not initializes parameter i and j
only different is i use Subroutine in situation SubFunction1

Hi UKHeliBob

my main program is use condition "while" to detect some parameter
if parameter is equal my condition
my program will execute "for" loop

i use while(1) to descripe that when the condition is OK
this way is easy to simulate my program for presentation

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

      for(int i,j = 0; i < 20; i++)

That is STILL wrong.