Code Blocks ()

Code Blocks ({})

In the previous if statement examples, only one statement follows if and else, a single System.Console.WriteLine(), similar to Listing 3.23.

Listing 3.23. if Statement with No Code Block

 if(input<9)    System.Console.WriteLine("Exiting");                               

However, sometimes you might need to execute multiple statements. Take, for example, the highlighted code block in the radius calculation in Listing 3.24.

Listing 3.24. if Statement Followed by a Code Block

class CircleAreaCalculator { static void Main()  {   double radius; // Declare a variable to store the radius.   double area; // Declare a variable to store the area.   System.Console.Write("Enter the radius of the circle: ");   // double.Parse converts the ReadLine()   // return to a double.   radius = double.Parse(System.Console.ReadLine());   if(radius>=0)   {                                                                                  // Calculate the area of the circle.                                          area = 3.14*radius*radius;                                                    System.Console.WriteLine(                                                     "The area of the circle is: {0}", area);                                 }                                                                             else   {        System.Console.WriteLine(            "{0} is not a valid radius.", radius);   }  } } 

Output 3.14 shows the results of Listing 3.24.

Output 3.14.

Enter the radius of the circle: 3 The area of the circle is: 28.26

In this example, the if statement checks whether the radius is positive. If so, the area of the circle is calculated and displayed; otherwise, an invalid radius message is displayed.

Notice that in this example, two statements follow the first if. However, these two statements appear within curly braces. The curly braces combine the statements into a single unit called a code block.

If you omit the curly braces that create a code block in Listing 3.24, only the statement immediately following the Boolean expression executes conditionally. Subsequent statements will execute regardless of the if statement's Boolean expression. The invalid code is shown in Listing 3.25.

Listing 3.25. Relying on Indentation, Resulting in Invalid Code

if(radius>=0)    area = 3.14*radius*radius;    System.Console.WriteLine(    // Error!! Needs code block.        "The area of the circle is: {0}", area);

In C#, indentation is for code readability only. The compiler ignores it, and therefore, the previous code is semantically equivalent to Listing 3.26.

Listing 3.26. Semantically Equivalent to Listing 3.25

if(radius>=0) {    area = 3.14*radius*radius; } System.Console.WriteLine(    // Error!! Place within code block.   "The area of the circle is: {0}", area);

Programmers should take great care to avoid subtle bugs like this, perhaps even going so far as to always include a code block after a control flow statement, even if there is only one statement.

Advanced Topic: Math Constants

In Listing 3.25 and Listing 3.26, the value of pi as 3.14 was hardcodeda crude approximation at best. There are much more accurate definitions for pi and E in the System.Math class. Instead of hardcoding a value, code should use System.Math.PI and System.Math.E.

Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185 © 2008-2017.
If you may any questions please contact us: