# Incrementing within for loops

• ### Question

• I'm looking to increment such that the first set of values in my for loop remains unchanged by the increment, I think there must be some tidyer way of doing it than to simply subtract the incremental amount outside my loop. Any advise would be welcome, here's my code:

namespace ForIncrementing
{
class Program
{
static void Main(string[] args)
{
int jaboat = 10;
int jaboat2 = jaboat - 6;            //this seems like a bad way to solve the problem
for (int i = 0; i < 3; i++)
{
Line LX = new Line();
LX.Y1 = 10;
LX.Y2 = LX.Y1 + 20;

LX.X1 = jaboat2 +=6;             //want to make this increment after the first instance
LX.X2 = LX.X1;

Console.WriteLine(\$"{LX.X1} {LX.X2}");
Console.WriteLine(\$"{LX.Y1} {LX.Y2}");
}
}
}
}

Thanks

Sunday, January 6, 2019 11:42 AM

### All replies

• I see several solutions. one is this:

for (int i = 0, jaboat2=4; i < 3; i++, jaboat2 += 6)

Another is to only use jaboat2, since you are not using i at all inside the loop:

for (int jaboat2 = 4, jaboat2<22, jaboat2 += 6)

Another is to use an expression on i:

for (int i = 0; i < 3; i++) ... LX.X1 = (i+1)*6+4;

Sunday, January 6, 2019 12:55 PM
• I think it is how assignment operator works. You can simply rewrite it to two rows:

```LX.X1 = jaboat2;
jaboat2 += 6;```

https://docs.microsoft.com/en-US/dotnet/csharp/language-reference/language-specification/expressions#assignment-operators

• Marked as answer by Sunday, January 6, 2019 1:39 PM
Sunday, January 6, 2019 1:03 PM
• Thanks for that Alberto,

the first option works just great,

regards,

Matthew

Sunday, January 6, 2019 1:23 PM
• Thanks Petr,

this is the solution i had been looking for, although I think in this case I will implement Alberto's as it's saving me a little more space for all the repetitions i'll need to make.

thanks again,

Matthew

Sunday, January 6, 2019 1:41 PM