#165 – Any Clause of the for Loop May Be Left Empty

Any of the three clauses of the for loop may be left empty.

If the initialization clause is left empty, no initialization is done before the loop starts executing for the first time.

            // Loop until i reaches 10, no matter what its starting value
            for (; i <= 10; i++)
            {
                Console.WriteLine(i);
            }

If the condition clause is empty, no condition is ever checked to determine when the loop should exit and the loop executes forever.

            // Loop forever, incrementing i
            for (int i = 0; ; i++)
            {
                Console.WriteLine(i);
            }

The iteration clause can also be empty.

            // Get 'er done, Hercules
            for (int laborNum = 1; laborNum <= 12; )
            {
                bool success = HerculesLabors(laborNum);
                if (success == true)
                    laborNum++;
            }
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: