- 3.1. Introduction
- 3.2. A Simple C# App: Displaying a Line of Text
- 3.3. Creating a Simple App in Visual Studio
- 3.4. Modifying Your Simple C# App
- 3.5. Formatting Text with Console.Write and Console.WriteLine
- 3.6. Another C# App: 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
A condition is an expression that can be either true or false. This section introduces a simple version of C#’s if statement that allows an app 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.17. 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.17 Relational and equality operators.
Using the if Statement
Figure 3.18 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 app 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 app compares the numbers and displays the results of the comparisons that are true.
Fig. 3.18 Comparing integers using if statements, equality operators and relational operators.
1
// Fig. 3.18: Comparison.cs
2
// Comparing integers using if statements, equality operators
3
// and relational operators.
4
using System;5
6
public class Comparison7
{8
// Main method begins execution of C# app
9
public static void Main( string[] args )10
{11
int number1;// declare first number to compare
12
int number2;// declare second number to compare
13
14
// prompt user and read first number
15
Console.Write("Enter first integer: "
);16
number1 = Convert.ToInt32( Console.ReadLine() );17
18
// prompt user and read second number
19
Console.Write("Enter second integer: "
);20
number2 = Convert.ToInt32( Console.ReadLine() );21
22
if ( number1 == number2 )
23
Console.WriteLine(
"{0} == {1}"
, number1, number2 );24
25
if ( number1 != number2 )
26
Console.WriteLine(
"{0} != {1}"
, number1, number2 );27
28
if ( number1 < number2 )
29
Console.WriteLine(
"{0} < {1}"
, number1, number2 );30
31
if ( number1 > number2 )
32
Console.WriteLine(
"{0} > {1}"
, number1, number2 );33
34
if ( number1 <= number2 )
35
Console.WriteLine(
"{0} <= {1}"
, number1, number2 );36
37
if ( number1 >= number2 )
38
Console.WriteLine(
"{0} >= {1}"
, number1, number2 );39
}// end Main
40
}// end class Comparison
Class Comparison
The declaration of class Comparison begins at line 6
public class Comparison
The class’s Main method (lines 9–39) begins the execution of the app.
Variable Declarations
Lines 11–12
int number1;
// declare first number to compare
int number2;
// declare second number to compare
declare the int variables used to store the values entered by the user.
Reading the Inputs from the User
Lines 14–16
// prompt user and read first number
Console.Write(
"Enter first integer: "
);number1 = Convert.ToInt32( Console.ReadLine() );
prompt the user to enter the first integer and input the value. The input value is stored in variable number1. Lines 18–20
// prompt user and read second number
Console.Write(
"Enter second integer: "
);number2 = Convert.ToInt32( Console.ReadLine() );
perform the same task, except that the input value is stored in variable number2.
Comparing Numbers
Lines 22–23
if ( number1 == number2 )
Console.WriteLine(
"{0} == {1}"
, number1, number2 );
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. 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.
No Semicolon at the End of the First Line of an if Statement
There’s 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 );
// logic error
Console.WriteLine(
"{0} == {1}"
, number1, number2 );
would actually be interpreted by C# as
if ( number1 == number2 )
;
// empty statement
Console.WriteLine(
"{0} == {1}"
, 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 app. The app 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.
Whitespace
Note the use of whitespace in Fig. 3.18. 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 app. It’s incorrect to split identifiers, strings, and multicharacter operators (like >=). Ideally, statements should be kept small, but this is not always possible.
Precedence and Associativity of the Operators We’ve Discussed So Far
Figure 3.19 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.19 Precedence and associativity of operations discussed so far.