Can't get out of for loop [solved]

Hi,

I am adding up data, redSum and greenSum, from a photo-diode in inner for loop and subtracting Difference = redSum - greenSum in outer for loop. But the problem is that once a false value of the variable in inner for loop is reached, the loop repeats again and the sketch never goes to do stuff after the end of the inner loop!

Here is my code

int RedLED = 5;
int GreenLED = 6;
int PhotoDiode = A2;
int switchPin = 9;
int switchValue;
int green;
int red;
int redSum;
int greenSum;
int Difference
 
 
void setup() {                
  pinMode(RedLED, OUTPUT);     
  pinMode(GreenLED, OUTPUT);
  pinMode(PhotoDiode, INPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH); //default state off - HIGH pitential
  Serial.begin(9600);           // set up Serial library at 9600 bps
}

void loop() 

{
  switchValue = digitalRead(switchPin);
    if (switchValue == LOW)
    
    {
      for (int j=0; j<3; j++)
      
      {
        red = 0;
        green = 0;
        
        for (int i = 0; i < 6; i++)
       
       {
      
         Serial.println("i");
         Serial.println(i, DEC); 
         delay(1);
         digitalWrite(RedLED, HIGH);   
         delay(1000);               
         analogReference(INTERNAL); //10 bits analog pin, 1024 max is the voltage of 1.1 V, internal reference
         red = analogRead(A2); 
         delay(1);
         Serial.println("red");
         Serial.println(red, DEC);
         redSum = redSum + red;
         delay(1);
         Serial.println("redSum");
         Serial.println(redSum, DEC);
         delay(1);
         digitalWrite(RedLED, LOW);
         delay(1000);
         digitalWrite(GreenLED, HIGH);
         delay(1000);
         analogReference(INTERNAL);
         green = analogRead(A2);
         Serial.println("green");
         Serial.println(green, DEC);
         greenSum = greenSum + green;
         delay(1);
         Serial.println("greenSum");
         Serial.println(greenSum, DEC);
         delay(1);  
         digitalWrite(GreenLED, LOW);    
         delay(1000);               
       }
       
       //delay(1000);
       Serial.println("red sum");
       Serial.println(redSum, DEC);
       Serial.println("green sum");
       Serial.println(greenSum, DEC);
       Difference = redSum - greenSum;
       delay(1);
       Serial.println("Difference");
       Serial.println(Difference, DEC);
       delay(1);
      }
    }
}

and here is my serial monitor:

i
0
red
45
redSum
45
green
36
greenSum
36
i
1
red
45
redSum
90
green
36
greenSum
72
i
2
red
45
redSum
135
green
36
greenSum
108
i
3
red
45
redSum
180
green
36
greenSum
144
i
4
red
45
redSum
225
green
35
greenSum
179
i
5
red
45
redSum
270
green
36
greenSum
215
red sum
270
green sum
215
Difference
55

i
0
red
45
redSum
315
green
36
greenSum
251
i
1
red
45
redSum
360
green
36
greenSum
287
i
2
red
45
redSum
405
green
36
greenSum
323
i
3
red
45
redSum
450
green
36
greenSum
359
i
4
red
45
redSum
495
green
36
greenSum
395
i
5
red
45
redSum
540
green
36
greenSum
431
red sum
540
green sum
431
Difference
109
i
0
red
45
redSum
585
green
36
greenSum
467
i
1
red
45
redSum
630...

Any ideas?

Thanks!

Solved now. Sorry.

OlenaKulyk:
But the problem is that once a false value of the variable in inner for loop is reached,

What do you mean by "a false value"? What variable is getting this "false" value.

         Serial.println("i");
         Serial.println(i, DEC);

Your serial monitor would be much easier to read if you didn't put labels and values on seperate lines. Try something like this:

Serial.print("i = ");
Serial.println(i, DEC);