Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

increment for loop by step 2 in C#?

11 views
Skip to first unread message

Rich P

unread,
Dec 22, 2009, 12:04:58 PM12/22/09
to
Basic question: if I need to increment a for loop by step 2 (or step 3,
step 4, ...) -- in VB I say this:

For i = 0 To 10 Step 2
Debug.Print i
Next

Is there an equivalent for C# what does that look like? If there is no
equivalent would I have to do something like this (for the same
example):

for (int i = 0; i < 5; i++)
Console.WriteLine(i * 2)


Rich

*** Sent via Developersdex http://www.developersdex.com ***

Karl Mitschke

unread,
Dec 22, 2009, 12:20:41 PM12/22/09
to
Hello Rich,

i++ increments i by 1, so I think you can do this:

for (int i = 0; i < 11; i += 2)
{
Console.WriteLine(i);
}


Karl
http://unlockpowershell.wordpress.com/


mick

unread,
Dec 22, 2009, 12:19:10 PM12/22/09
to

"Rich P" <rpn...@aol.com> wrote in message
news:eK3$kjygKH...@TK2MSFTNGP02.phx.gbl...

> Basic question: if I need to increment a for loop by step 2 (or step 3,
> step 4, ...) -- in VB I say this:
>
> For i = 0 To 10 Step 2
> Debug.Print i
> Next
>
> Is there an equivalent for C# what does that look like? If there is no
> equivalent would I have to do something like this (for the same
> example):
>
> for (int i = 0; i < 5; i++)
> Console.WriteLine(i * 2)
>

for(int i = 0; i < 5; i +=2) /
Console.WriteLine(i)

mick

Jeremy McPeak

unread,
Dec 22, 2009, 12:24:10 PM12/22/09
to
for (int ii = 0, foo = 0; (foo = 2 * ii) < 5; ii++)
{
Console.WriteLine(foo);
}

---
frmsrcurl: http://msgroups.net/microsoft.public.dotnet.languages.csharp/increment-for-loop-by-step-2-in-C

Rich P

unread,
Dec 22, 2009, 12:58:26 PM12/22/09
to
thanks all for the replies. I am glad it turned out to be simpler than
I thought it would be.

Gregory A. Beamer

unread,
Dec 22, 2009, 3:46:57 PM12/22/09
to
Rich P <rpn...@aol.com> wrote in news:eK3$kjygKHA.3792
@TK2MSFTNGP02.phx.gbl:

> for (int i = 0; i < 5; i++)
> Console.WriteLine(i * 2)
>


I see you already have the answer, but wanted to share.I saw this in a
program I was fixing recently:

for (int i = 0; i < x.Count; i++)
{
//Do work
i++;
}

Hoepfully that gives someone a Christmas smile.

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************

Arne Vajhøj

unread,
Dec 22, 2009, 5:32:59 PM12/22/09
to
On 22-12-2009 15:46, Gregory A. Beamer wrote:
> Rich P<rpn...@aol.com> wrote in news:eK3$kjygKHA.3792
> @TK2MSFTNGP02.phx.gbl:
>> for (int i = 0; i< 5; i++)
>> Console.WriteLine(i * 2)
>>
>
> I see you already have the answer, but wanted to share.I saw this in a
> program I was fixing recently:
>
> for (int i = 0; i< x.Count; i++)
> {
> //Do work
> i++;
> }
>
> Hoepfully that gives someone a Christmas smile.

More like wondering where my axe is ...

:-)

Arne

0 new messages