Dear
I am new to C++ and got a question on the nested for loop. I will just show a simple code.
for (i = 0, i<3, i++)
{
for (j = 0, j<3, j++)
{
cout << i<<""<<j<<endl;
}
}
So this code will give me:
0 0
0 1
1 0
1 1
2 0
2 1
2 2
HOWEVER i dont want the inner loop to finish. I only want the inner loop to run once and then update the outer loop i value/
So my expected output is like:
0 0
1 1
2 2
Does anyone know how to do it in C++?
Warmest regards,
Jason
So this code will give me:
0 0
0 1
1 0
1 1
2 0
2 1
2 2 |
Not quite, but I get what you mean.
1 2 3 4 5 6
|
int j = 0;
for ( i=0; i<3; i++)
{
cout << i << " " << j << endl;
j++;
}
|
or if i and j will never be different, just output i twice.
1 2 3 4
|
for ( i=0; i<3; i++)
{
cout << i << " " << i << endl;
}
|
Last edited on
Hi Thanks
However this doesnt solve my problem.
I have to create two for loops for my specific purposes.
Say I have two 1d array with same length of 10.
I wanna access the 0-index value for first and second array and do some things, then access the following.
Any ideas how to do that?
The main point is to do things concurrently as we know c++ language is sequential.
Cheers,
JASON
Thanks everyone.
I solved the problem.
Cheers,
Jason