Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two counters in a for loop for C#

Hi couldn't find it for C#, I am trying something like that

for (int j = mediumNum; j < hardNum; j++; && int k = 0; k < mediumNum; k++);

but it does not work. Any valid method???

like image 920
Sarp Kaya Avatar asked May 23 '12 21:05

Sarp Kaya


4 Answers

This is what you want

for (int j = mediumNum, k = 0; j < hardNum && k < mediumNum; j++, k++)
like image 66
Md Kamruzzaman Sarker Avatar answered Oct 21 '22 15:10

Md Kamruzzaman Sarker


If I am understanding correctly, you want this:

for (int j = mediumNum, k = 0; j < hardNum && k < mediumNum; j++, k++)

like image 23
Paul Phillips Avatar answered Oct 21 '22 15:10

Paul Phillips


It might express your intent better to use a while loop, perhaps making the code a little easier to read:

int j = mediumNum;
int k = 0;
while (j < hardNum && k < mediumNum)
{
    //...
    j++;
    k++;
}
like image 20
David Avatar answered Oct 21 '22 16:10

David


I wonder if you know for sure that both loops always terminate at the same time. If not, the body of the loop will have to account for that.

int j;
int k;
for (j = mediumNum, k = 0; j < hardNum && k < mediumNum; j++, k++);
like image 24
Jirka Hanika Avatar answered Oct 21 '22 16:10

Jirka Hanika