C# loop optimization

See here for an interactive tutorial on control flows in C#.

At the end of the tutorial, you see this piece of code, to sum all numbers between 1 and 20, which are divisible by 3

int sum = 0;
for (int number = 1; number < 21; number++)
{
  if (number % 3 == 0)
  {
    sum = sum + number;
  }
} 

Check the cyclomatic complexity of this code. Is there a way you can simplify it by removing the nesting?

Type your thoughts below.

Going ahead, we'll look at using loops to iterate over a collection

Comments

Popular posts from this blog

Relationships in a Relational Database

Data Schema and Blobs

Power On!