# nested four loops

• 02-25-2011
begginer
nested four loops
ok so what i am trying to do is have an led flash on and off at one speed and then flash at at another speed for a set period of time...i am pretty sure that i need two for loops to do this but i am not sure how this works

i would like to have the first for loop just flash the led four times and then have the second for loop flash the led 8 times

the leds flashing time is determined by an interrupt and the is working correctly...i just need help getting the for loops to work

Code:

```int i=0, j=0 for(i=0; i<4; i++){   if(G_Flag){       led on;     }     else{     led off;     }  i++; }if(i=4){ for(i=4;i<8<i++) {     if(G_Flag)     led on;     else     led off   i++ } } i=0 } interrupt_ISR {   G_Flag = !G_Flag }```
the way i am controlling the leds is not an issue i have that underwraps but i am not sure how to just have the led flash on four four times and the have it flash on again at a different speed for eight times

i thought that this code would work because once i is equal to four it would go into the second for loop but it doesn't work and i don't understand why
• 02-25-2011
Babkockdood
You would have to call interrupt_ISR once in each for loop to negate G_Flag. Also, in the second for loop, you typed "if" instead of "for".
• 02-25-2011
Code:

```for(i=0;i<12;i++) {  //12=number of total flashes I want   if(i<4)  //4=number of long flashes I want       longFlash   else       shortFlash }```
Both 4 and 12 in the above could be defined macros:
#define LONG_NUM 4
#define FLASHES 12

or just variables.
• 02-25-2011
begginer
the only problem i have is that i am using a mciro controller and not programming in visual studies so i don't know if i can use the long number of any of thoes macros that you could typically use in c, but may i am not sure
• 02-25-2011
In that case, "magic" numbers are OK.

You may need to use two different loops. No if(i == 4) is needed, since i will always equal 4 at the end of the first loop.
• 02-25-2011
begginer
that what im confused about...i am not that familar with c, the only real programming classes i have had is c++ what i was thinking is that the...

the first for loop would count up, to four, and then once it hit four, it would kick into the second four loop?
• 02-25-2011
begginer
that what im confused about...i am not that familar with c, the only real programming classes i have had is c++ what i was thinking is that the...

the first for loop would count up, to four, and then once it hit four, it would kick into the second four loop?
• 02-25-2011
Quote:

Originally Posted by begginer
that what im confused about...i am not that familar with c, the only real programming classes i have had is c++ what i was thinking is that the...

the first for loop would count up, to four, and then once it hit four, it would kick into the second four loop?

The first loop could count up to four, and then it would kick over into the second for (you almost got me to write "four" :p loop.

But you don't need the test for i == 4 in there. After the first loop, I will always be four. No test needed.

This is an error, since = is for assignment anyway. Inside an if statement, you need == (and anywhere else you test for equality.

Code:

```if(i=4){  //wrong. Should be deleted, anyway. if(i==4) { //correct syntax for a test of equality. Not needed before the second for loop in your code.```
• 02-25-2011
begginer
lol, i guess that is to many "for" and "four" in one issue but i got it to work...thanks for all the help
• 02-25-2011
"to many"

You are KILLING me here! :p :p
• 02-25-2011
CommonTater
Quote:

Originally Posted by Adak
"to many"

You are KILLING me here! :p :p

C'mon Adak... fore loops are fun! :rolleyes:
• 02-25-2011
It's "to" mulch for me "too" bare! :p

Hello!
• 02-25-2011
CommonTater
Quote:

Originally Posted by Adak
It's "to" mulch for me "too" bare! :p

Hello!

I bearly know how to answer such a clipping comment... :cool:

(Stop me when we get to light bulb puns... Illuminating though they may be I tend to fade rather quickly on the current path of it all.)