- 3.1 Introduction
- 3.2 A Simple C# Application: Displaying a Line of Text
- 3.3 Creating a Simple Application in Visual C# Express
- 3.4 Modifying Your Simple C# Application
- 3.5 Formatting Text with Console.Write and Console.WriteLine
- 3.6 Another C# Application: Adding Integers
- 3.7 Arithmetic
- 3.8 Decision Making: Equality and Relational Operators
- 3.9 Wrap-Up
3.8 Decision Making: Equality and Relational Operators
This section introduces a simple version of C#'s if statement that allows an application to make a decision based on the value of a condition. For example, the condition "grade is greater than or equal to 60" determines whether a student passed a test. If the condition in an if statement is true, the body of the if statement executes. If the condition is false, the body does not execute. We'll see an example shortly.
Conditions in if statements can be formed by using the equality operators (== and !=) and relational operators (>, <, >= and <=) summarized in Fig. 3.21. The two equality operators (== and !=) each have the same level of precedence, the relational operators (>, <, >= and <=) each have the same level of precedence, and the equality operators have lower precedence than the relational operators. They all associate from left to right.
Fig. 3.21. Equality and relational operators.
Standard algebraic equality and relational operators |
C# equality or relational operator |
Sample C# condition |
Meaning of C# condition |
Equality operators |
|||
= |
== |
x == y |
x is equal to y |
!= |
x != y |
x is not equal to y |
|
Relational operators |
|||
> |
> |
x > y |
x is greater than y |
< |
< |
x < y |
x is less than y |
>= |
x >= y |
x is greater than or equal to y |
|
<= |
x <= y |
x is less than or equal to y |
Figure 3.22 uses six if statements to compare two integers entered by the user. If the condition in any of these if statements is true, the assignment statement associated with that if statement executes. The application uses class Console to prompt for and read two lines of text from the user, extracts the integers from that text with the ToInt32 method of class Convert, and stores them in variables number1 and number2. Then the application compares the numbers and displays the results of the comparisons that are true.
The declaration of class Comparison begins at line 6. The class's Main method (lines 9–39) begins the execution of the application.
Fig. 3.22. Comparing integers using if statements, equality operators and relational operators.
1 |
Enter first integer: 42 Enter second integer: 42 42 == 42 42 <= 42 42 >= 42 |
Enter first integer: 1000 Enter second integer: 2000 1000 != 2000 1000 < 2000 1000 <= 2000 |
Enter first integer: 2000 Enter second integer: 1000 2000 != 1000 2000 > 1000 2000 >= 1000 |
Lines 11–12 declare the int variables used to store the values entered by the user. Lines 14–16 prompt the user to enter the first integer and input the value. The input value is stored in variable number1. Lines 18-20 perform the same task, except that the input value is stored in variable number2.
Lines 22–23 compare the values of the variables number1 and number2 to determine whether they're equal. An if statement always begins with keyword if, followed by a condition in parentheses. An if statement expects one statement in its body. The indentation of the body statement shown here is not required, but it improves the code's readability by emphasizing that the statement in line 23 is part of the if statement that begins in line 22. Line 23 executes only if the numbers stored in variables number1 and number2 are equal (i.e., the condition is true). The if statements in lines 25–26, 28–29, 31–32, 34–35 and 37–38 compare number1 and number2 with the operators !=, <, >, <= and >=, respectively. If the condition in any of the if statements is true, the corresponding body statement executes.
There is no semicolon (;) at the end of the first line of each if statement. Such a semicolon would result in a logic error at execution time. For example,
if ( number1 == number2 );
|
would actually be interpreted by C# as
if ( number1 == number2 )
;
|
where the semicolon in the line by itself—called the empty statement—is the statement to execute if the condition in the if statement is true. When the empty statement executes, no task is performed in the application. The application then continues with the output statement, which always executes, regardless of whether the condition is true or false, because the output statement is not part of the if statement.
Note the use of whitespace in Fig. 3.22. Recall that whitespace characters, such as tabs, newlines and spaces, are normally ignored by the compiler. So statements may be split over several lines and may be spaced according to your preferences without affecting the meaning of an application. It's incorrect to split identifiers, strings, and multicharacter operators (like >=). Ideally, statements should be kept small, but this is not always possible.
Figure 3.23 shows the precedence of the operators introduced in this chapter. The operators are shown from top to bottom in decreasing order of precedence. All these operators, with the exception of the assignment operator, =, associate from left to right. Addition is left associative, so an expression like x + y + z is evaluated as if it had been written as ( x + y ) + z. The assignment operator, =, associates from right to left, so an expression like x = y = 0 is evaluated as if it had been written as x = ( y = 0 ), which, as you'll soon see, first assigns the value 0 to variable y then assigns the result of that assignment, 0, to x.
Fig. 3.23. Precedence and associativity of operations discussed so far.
Operators |
Associativity |
Type |
* / % |
left to right |
multiplicative |
+ - |
left to right |
additive |
< <= > >= |
left to right |
relational |
== != |
left to right |
equality |
= |
right to left |
assignment |