c# - Add selected elements to list using for loop -


i'm trying add integers 6 30, except 22 , 26, list using loop. 22 , 26 still being added. did wrong?

list<int> linenumbers = new list<int>();  (int x = 6; x < 30; x++) {   if ((x != 22) || (x != 26))   {     linenumbers.add(x);                     }                } 

the 2 integers not added if code below:

for (int x = 6; x < 30; x++) {   if (x == 22 )   {   }   else if (x == 26)   {   }   else   {     linenumbers.add(x);                 } } 

you need replace || &&:

for (int x = 6; x < 30; x++) {   if ((x != 22) && (x != 26))   {     linenumbers.add(x);                     }                } 

or perhaps, more clear:

for (int x = 6; x < 30; x++) {   if ((x == 22) || (x == 26))     continue;    linenumbers.add(x); } 

alternatively, perform one-liner using linq. isn't faster or anything, if for loop clearer you, keep as-is.

linenumbers.addrange(enumerable.range(6, 24).except(new[] { 22, 26 })); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -