5. Writing Code that Works

5.1. Using Language Idioms

Take a look at this counting loop:

int number;   // The number to be printed.
number = 0;   // Start with 0.
while ( 5 > number ) {  // Keep going as long as number is < 6.
    number = number + 1;  // Go on to the next number.
    Console.WriteLine(number);
}
Console.WriteLine("Done!");

Execute it mentally, and write down the output it produces. Pay particular attention to the first and last numbers that are displayed in the output.

If you wrote down the numbers 1, 2, 3, 4, 5, you are correct. This loop produces the same output as the one at the beginning of this chapter. Take a moment to compare this code with the original loop, which I have reproduced below. What are the differences?

int number;   // The number to be printed.
number = 1;   // Start with 1.
while ( number < 6 ) {  // Keep going as long as number is < 6.
    Console.WriteLine(number);
    number = number + 1;  // Go on to the next number.
}
Console.WriteLine("Done!");

This loop initialized number to 1 (the first value to be displayed), rather than 0. It incremented number at the end of the loop, rather than the beginning. And it used the comparison ( number < 6 ) instead of ( 5 > number ).

There are actually several different ways the comparison could be written. Take a look at the following options:

while ( number < 6 ) ...

while ( number <= 5 ) ...

while ( 6 > number ) ...

while ( number != 6 ) ...

The first three are logically equivalent comparisons; all four of them would cause the loop in this example to work identically. Take a moment to convince yourself that the program would produce the same output with all of these comparisons.

Convinced? Now, a word of explanation as to why I wrote the original example (the one at the beginning of the chapter) using the ( number < 6 ) comparison, with the increment at the end of the loop. Every human language has its idioms -- phrases that native speakers typically use to express certain ideas. The phrases don't always make sense to non-native speakers. Just as human languages have idioms, so do computer languages. If you have experience with another programming language, you may have thought that one of these other comparisons would have made better sense. Perhaps you think that if the comparison were expressed as ( number <= 5 ), it would have been more obvious that the last number printed would be 5, rather than 6. However, a professional C# programmer would always use the < operator in a loop like this, because that is the idiomatic way to write counting loops in C#.

There's a more compelling reason to use language idioms than just "it's the way professionals do it," though. Consistency is an important tool to help you avoid bugs. If every time you write a counting loop you always use the same technique, you will be less likely to make a logic mistake. Imagine a program with several different counting loops in it, each one using a different technique -- some with the increment at the beginning, others with the increment at the end, some using the <= operator, etc. A program like that is going to be much more difficult to debug than one that uses a consistent approach, because you will have to work harder to analyze each loop for correctness.

C# Idiom: In counting loops, prefer using the < operator in your comparison, and perform the increment at the end of the loop.