Creating Expressions and Statements

It's now time to start actually doing something with the data discussed so far. The first essential topic in C# coding is the expression . An expression can be evaluated and it yields a single value. At the most basic, literals are expressions, because they evaluate to a value. Variables are also expressions, because they evaluate to the value they contain.

Here's another expression 5 + 3 which evaluates to 8. In fact, int1 = 1 , where you assign 1 to the variable int1 , is an expression, and its value is 1. Because int1 = 1 is an expression yielding 1, it's legal in C# to create expressions like this: int3 = int2 = int1 = 1 . We'll be using the term expression frequently throughout this book, and the thing to remember is that an expression is something the C# compiler can evaluate to produce a value.

The next step is to create whole C# statements. C# code consists of statements, not just expressions, and each statement ends with a semicolon. Here are three statements:

 
 int int1; int1 = 5; System.Console.WriteLine("Hello from C#."); 

You can also create compound statements if you enclose a set of statements in curly braces, { and } . In C#, you can use a compound statement wherever you'd use a single statement. For example, the if statement we're about to take a look at in a few pages works as if statements usually do, evaluating an expression and executing a statement if that expression evaluates to true :

 
 if(  expression  )  statement  ; 

Although if statements are technically defined to only execute a single statement if their test expression is true, you can make that single statement a compound statement made up of many single statements:

 
 if(  expression  )   {  statement1  ;  statement2  ;  statement3  ;     .     .     .   } 

With the concepts of expressions and statements under our belts, it's time to turn to the next step in C# codingthe C# operators.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net