Comparing Equalities

   

Comparing values, particularly variables , is even more common than performing arithmetic (but you need to know how C# arithmetic works before you can understand the evaluation of equalities).

Comparison operators are most often used in decision-making structures, as explained in the next hour . Indeed, these operators are best understood using a simple if decision structure. In an if construct, C# considers the expression on the if statement, and if the expression equates to true, the code statement(s) are executed. For example, the following is an if operation (a silly one at that) expressed in English, not in C# code:

IF DOGS BARK, THEN SMILE.

If this were in C# code format, C# would evaluate the if condition, which in this case is dogs bark. If the condition is found to be true, the code following the expression is Performing Arithmetic, String Manipulation, and Date/Time Adjustments performed. Because dogs bark, you'd smile. Notice how these two things (dogs barking and you smiling) are relatively unrelated. This doesn't matter; the point is that if the condition evaluates to true, certain actions (statements) occur.

You'll often compare the value of one variable to that of another variable or to a specific value when making decisions. The following are some basic comparisons and how C# evaluates them:

 Debug.WriteLine(6 > 3);      //  Evaluates to true Debug.WriteLine(3 == 4);     //  Evaluates to false Debug.WriteLine(3 >= 3);     //  Evaluates to true Debug.WriteLine(5 <= 4);     //  Evaluates to false 

Performing comparisons is pretty straightforward. If you get stuck writing a particular comparison, attempt to write it in English before creating it in code.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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