I have a console application that uses a number of command line switches to control various methods. Each of the command line switches is handled by a switch statement that is within a for loop that iterates over each of the args.
for (int x = 0; x < args.Length; x++)
{
switch (args[x])
{
...
}
}
This works perfectly for my needs, however I need to add a –loop switch that causes the preceding args to be looped indefinitely based upon a timeout period specified by the –set-timeout switch.
The code I have so far is:
switch (args[x])
{
case "--set-timeout-5m":
timeout = 300000;
System.Threading.Thread.Sleep(timeout);
break;
....
case "--loop":
x = 0;
break;
}
The problem is setting x does not cause the for loop to continue from the start of args. It just sits there.
I am expecting x to have scope as the code is within the for loop and no errors are generated in Visual Studio. I am also expecting the break statement to break the case and pass x to the for loop.
Can anyone explain why it does not work or perhaps post a workaround?
prints
because
x++is executed after each iteration.If you want it to print
you need to change it to