Flylib.com

Books Software

 
 
 

- page 27


Summary

In this chapter, we explored the underpinnings of the .NET platform. This tour included topics covering the built-in security provided by the Common Language Runtime, along with the cross-language support and common data types. In addition, you've seen your first C# example in this book.


Further Reading

Programming C# , Third Edition by Jesse Liberty, O'Reilly. ISBN: 0596004893.

The Applied Microsoft .NET Framework Programming in C# Collection , Microsoft Press. ISBN: 0735619751.


Chapter 3. Expressions and Control Structures

IN BRIEF

In this chapter, we will explore the basic control structures provided by C#. Each of these constructs will be explored and discussed in terms of syntax and proper usage.

WHAT YOU NEED

RECOMMENDED SOFTWARE

.NET Framework Visual Studio .NET

RECOMMENDED HARDWARE

.NET-enabled desktop client

SKILLS REQUIRED

C# coding


EXPRESSIONS AND CONTROL STRUCTURES AT A GLANCE

Expressions and Control Structures

53

   

Basic Expressions

53

   

Legal Variable Names

53

   

Using C# Operators

54

   

Pre/Post Operators

60

   

Program Flow Control: Control Structures

60

   
 

The Program Execution Path

60

Short Circuit Evaluation

62

 

Conditional Statements

60

Using the Ternary Operator

64

 

The if Control Structure

61

The switch Statement

65

 

The if/else Control Structure Combination

61

   

Looping

68

   
 

The for Loop

68

The while Loop

70

 

Using the for Statement

68

The do..while Loop

72

 

The foreach Statement

69

   


Expressions and Control Structures

The goal with any programming language is to implement application logic in order to achieve the desired functionality. C# is a modern language that offers traditional expressions and control structures along with a few new ones to simplify the development process. In addition, C# is a type-safe language and enforces type safety at various levels. Such adherence to type safety is both a blessing and a curse. Depending on the language you're familiar with, some of your old habits will cause you a lot of frustration with C# because it enforces correct code and type safety. The purpose of this chapter is to explore the type safety offered by C# while delving into the expression syntax and control structures offered by the language.


Basic Expressions

An expression is defined as a syntactically correct program statement. However, for the purpose of this chapter, the expressions we're most interested in revolve around assignment and evaluation, and true/false determination. An assignment expression has at least one variable and one value to be assigned to that variable. Such is the case of declaring an integer value and assigning it the default value of 10. All variables must be initialized before use; otherwise , the C# compiler will bitterly complain about the usage of an uninitialized variable. So, what does this mean exactly? Take a look at the following C# statements :



int i;         //C# complains about the uninitialized variable





int j = 10;    //variable j has the initial value of 10




int k;




k = 10;        //okay as long as done before usage of k



C# requires all variables to be initialized before usage. The variable can be initialized during declaration or before its first usage. However, it's best to initialize a default value during its declaration; doing so will aid in future debugging attempts.