# Thread: alternate for loops? possible?

1. ## alternate for loops? possible?

Is there a way to make a compiler execute 2 for loops simultaneously and the loops are alternate so that:
Code:
```for loop1:
for (a=0; a<unknown1 ; a++)
{
cout<<"a"<<a<<" ";
}

for loop2:
for (b=0; b<unknown2 ; b++)
{
cout<<"b"<<b<<" ";
}

output:
a0 b0 a1 b1 a2 b2 a3...```
?

2. For the type of result you want - Try nesting them.
Code:
```FOR a to N
FOR b to N - 1
do et cetera
ENDFOR
ENDFOR```

3. That doesn't alternate them.

In theory, you can do that with gotos. I'll hit you if you do

Why do you need it?

4. I tried both but it just can't be compiled.
I am not sure how to use 'goto'. Is it 'goto for(.......);'?

5. If you only have one "unknown", you could just write:
Code:
```for (int i = 0; i < unknown; ++i)
{
// print a and b here
}```
If you really do need two "unknowns", then one way is to write:
Code:
```bool flag = true;
for (int i = 0; flag; ++i)
{
flag = false;
if (i < unknown1)
{
// print a here
flag = true;
}
if (i < unknown2)
{
// print b here
flag = true;
}
}```

6. What happens when unknown1 is different to unknown2 ?

Do you just get the excess of the greater?
Code:
```	for (a=0, b=0; a<unknown1 && b<unknown2 ; a++, b++)
{
cout<<"a"<<a<<" ";
cout<<"b"<<b<<" ";
}

for (; a<unknown1 ; a++)
{
cout<<"a"<<a<<" ";
}

for (; b<unknown2 ; b++)
{
cout<<"b"<<b<<" ";
}```

7. Thank you, everyone. Actually, I am attempting an exercise of array. It has to be 2 unknowns. Whether any 1 of the content of the array is excess or not is not important here. The code is like this:
Code:
```int array1[10],array2[10];

//user input size1(0-10) of array1

//user input a list of integers to array1(no.  of integers=size1)

//user input size2(0-10) of array2

//user input a list of integers to array2(no.  of integers=size2)

//display the content of array1 & array2 alternately```
If I use 1 unknown and any 1 of the arrays has empty content, wrong code will be output.

This is the screenshot of the unwanted output and only 1 unknown is used here:

http://img213.imageshack.us/img213/5849/unwantednw8.jpg

If the empty content can be prevented from being displayed, then 1 unknown is enough but I just have no idea.

8. This can be done rather simply with a single for loop. I don't want to give too much away because figuring this out is likely the point of the assignment, but your for loop doesn't have to be fancy. It can just loop from 0 to 10. Then inside the loop you output both the array1 and array2 value if they are valid.

9. Originally Posted by Daved
This can be done rather simply with a single for loop. I don't want to give too much away because figuring this out is likely the point of the assignment, but your for loop doesn't have to be fancy. It can just loop from 0 to 10. Then inside the loop you output both the array1 and array2 value if they are valid.
Thank you for your big hint! I forget I can use boolean value to do that! What an idiot!lol Now this part is fixed.