Comparing Equalities


Comparing values, particularly variables, is even more common than performing arithmetic (but you need to know how Visual 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, Visual C# considers the expression in 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 Visual C# code:

IF DOGS BARK, THEN SMILE.


If this were in Visual C# code format, Visual 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 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 Visual 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.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

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