开发者

Logic for FOR loop in c#

My method has a parameter, I have to use that in my For loop to iterate.

For example, I have a text file with 4 lines.

开发者_开发百科

If the Param is 1, the for loop must iterate through the last three lines If the Param is 2, the for loop must iterate through the last two lines If the Param is 3, the for loop must iterate through the last one line

How can i pass this param in my For loop to achieve all the three scenarios stated above ?


for(int i = param; i < lines.Count ; i++) {...}

or with LINQ:

foreach(var line in lines.Skip(lines.Count - param)) {...}


You should try something like

for (int i = param; i < whateverCount; i++)
{
//   do something
}

Where param would be the item to start from. Just remember that MOST arrays/list are zero based, but there are cases where they are 1 based.


private void YourFunction(int value)
{
  for(int x=0;x<4-value;x++)
  {
    //loop will happen 4 - value times, 4-3 = 1, 4-2 =2, 4-1 = 3 times
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜