#164 – for Loop Clauses Can Contain Lists of Statements

Both the initialization clause and the iteration clause of a for loop can consist of multiple statements, separated by commas.

For example, you could initialize two different variables in the initialization clause and then increment one variable and decrement the other in the iteration clause.

            // Set reverse diagonal of matrix
            for (int i = 0, j = 9;
                 i < 10;
                 i++, j--)
            {
                nums[i, j] = 1;
            }
Advertisement

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

2 Responses to #164 – for Loop Clauses Can Contain Lists of Statements

  1. voxstudios says:

    Do you know if there’s a way to handle different types or initializers? Like having an int and a string for instance.

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 )

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: