Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 82.9k

problem with incrementing in a FOR loop

Oct 27 2021 10:45 AM

Hi

This code:

int i, j, b;
 b = 2;
        for (i = 1; i <=3; i++)
        {
            for (j = 1; j <= i; j++)
                Console.Write("{0} ", b++);
            Console.Write("\n");
        }
provides this:

2
3 4
5 6 7

How can i increment the 'b' value by 2 instead of by 1 but showing the first value (2) first?

I tried this:Console.Write("{0} ", b+=2);
but this gives
4
6 8
10 12 14

I want this:
2
4 6
8 10 12

Thanks

 

 


Answers (4)