Click here to Skip to main content
16,017,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public int m;

C#
for(m = 1; m<4; ; m++)
{

}


Now based on the value of m, I need the below loop to run that number of times.
I mean to say: if m=1, then below loop should run for 10 times 1.
if m=2 then below loop should run for 10 times 2(which will be 20).
for (int i = 10; i<50; ; i++)
{

}


What I have tried:

Can anyone please tell me how to do this..??

Thanks.
Posted
Updated 10-Aug-16 6:08am

C#
for(m = 1; m<4; m++)
{
    for (int i = 0; i<m*10; i++)
    {
        // your code here
    }
}
 
Share this answer
 
Comments
BillWoodruff 10-Aug-16 12:55pm    
+5
The way you did your for loops makes me think you may not understand the syntax. If so, let me know and i'll clarify for you.

All you need to do is embed one for loop into the other. Take a look at the values of i and totalcounter once the program is done running. the total number of times i runs is 20 as described when m runs 2 times.

C#
for (int m = 0; m < 2; m++)
{
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine("i count: {0}",i);
        Console.WriteLine("totalcounter count: {0}", totalcounter);
        totalcounter += 1;
    }
}


Sample output

C#
i count: 0
totalcounter count: 1
i count: 1
totalcounter count: 2
i count: 2
totalcounter count: 3
i count: 3
totalcounter count: 4
i count: 4
totalcounter count: 5
i count: 5
totalcounter count: 6
i count: 6
totalcounter count: 7
i count: 7
totalcounter count: 8
i count: 8
totalcounter count: 9
i count: 9
totalcounter count: 10
i count: 0
totalcounter count: 11
i count: 1
totalcounter count: 12
i count: 2
totalcounter count: 13
i count: 3
totalcounter count: 14
i count: 4
totalcounter count: 15
i count: 5
totalcounter count: 16
i count: 6
totalcounter count: 17
i count: 7
totalcounter count: 18
i count: 8
totalcounter count: 19
i count: 9
totalcounter count: 20
 
Share this answer
 
v2
Comments
Member 12478311 10-Aug-16 11:59am    
Both are from different Methods in same class.

But m returns value to i,
and i should run the times of m (value of m).
Option 1:
C#
int m;
for (m = 1; m < 4; m++)
{
   for (int j = 1; j <= m * 10; j++)
   {
      for (int i = 10; i < 50; i++)
      {
         // Do work.
      }
   }   
}



Option 2:
C#
void DoWork()
{
   int m;

   for (m = 1; m < 4; m++)
   {
      Process(m);
   }
}

void Process(int multiplier)
{
   for (int count = 1; count <= 10 * multiplier; count ++)
   {
      for (int i = 10; i < 50; i++)
      {
         // Do work.
      }
   }
}
 
Share this answer
 
C language is the ancestor of C++ which is the ancestor of C#. They share the same syntax and structures.
Learning C basics shouldn't harm:
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900