Running a loop for four times, with diffe

Hello,

How can I run for loop
 
for (int i=0; i<=n; i++)


for four different values for n? I assume I have to have an additional for loop but how could I set it up, so that it only runs for these four values i.e. n=10, n=100, n=1000, n=10000?

1
2
3
4
5
6
7
int n =10;
for (int j=0; j<=n; j++)
{...



n=n*10}


Something of the sort, right? But the problem is, that there is no restriction set on this loop so it will run infinitely.
Last edited on
closed account (SECMoG1T)
you are almost there

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    int n =10;

    for(int i=0;i<4; i++)
    {
        std::cout<<"\n\nrunning with "<<n<<" as limit\n";
        char c = getchar();///pause
        for(int j=0; j<n; j++)
        {
           std::cout<<j<<" ";
        }

        n*=10;
    }
}
Ah, thanks Yolanda!
There are other ways too:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    for ( int n=10; n<=10000; n*=10 )
    {
        std::cout<<"running with "<<n<<" as limit\n";
        // use n
    }
}
Last edited on
Or perhaps:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    for ( int n : {10, 100, 1000, 10000}  )
    {
        std::cout<<"running with "<<n<<" as limit\n";
        // use n
    }
}

Topic archived. No new replies allowed.