• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing odd and even list items of a String list in a single for loop

 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Say I have a String List like so:



I am trying to access all even elements and odd elements in the same for loop like so:



This code is obviously wrong because doing an i+1 while trying to access odd elements, increases the value of i which leads to wrong result by the time I try to access the next even element. I understand that my code is wrong but I was just trying to show what is it that I am seeking to do. Could someone tell me what is the right way to do this?

Thanks,
Sony
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using i++ to increment the index by one, you just need to increment it by two. Make sense? I'll let you figure out the little bit of code yourself, but just replacing that i++ is going to do it.
 
sony vijay
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic