Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to iterate the for loop with multiples of 16

For example, if the length = 20, then my loop should be

C#
for (int i =0; i<x; i++) //x needs to be 16 here
{ 
   //do
}

for (int i = 10; i< remaining length; i++) //remaining length needs to be 4
{
   //do
}


if the length = 32, then my loop should be


C#
for (int i =0; i<x; i++) //x needs to be 16 here
{ 
   //do
}

for (int i = 10; i< remaining length; i++) //remaining length needs to be 16
{
   //do
}


What I have tried:

Need to iterate it with a fixed length irrespective of the input length
Posted
Updated 29-Mar-22 1:04am
v2

You can use nested loops:
C++
int X=20;
for(int Start=0; Start < X; Start += 16){
    int End= (Start+16 < X) ? Start+16 : X;
    for(int i= Start; i < End; i++) {
        // your logic
    }
}
 
Share this answer
 
Do you really mean
C#
int blocks = x / 16;
int spare = x % 16;
int counter = 0;
for (int b=0; b<blocks; ++b)
{
    for (int n=0; n<16; ++n)
    {
        //...
    }
}
for (int n=0; n<spare; ++n)
{
    //..
}

?
 
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