Operator Precedence
Table 4-2 shows the order of precedence for all C# operators, from highest to lowest. This
table includes several operators that will be discussed later in this book.
Highest
()
[]
.
++
(postfix)
– –
(postfix)
checked
new
sizeof
typeof
unchecked
!
~
(cast)
+
(unar y)
–
(unar y)
++
(prefix)
– –
(prefix)
*
/
%
+
–
<<
>>
<
>
<=
>=
is
==
!=
&
^
|
&&
||
??
?:
=
op
=
=>
Lowest
T
ABLE
4-2
The Precedence of the C# Operators
www.freepdf-books.com
5
Program Control Statements
T
his chapter discusses C#’s program control statements. There are three categories of
program control statements:
selection
statements, which are the
if
and the
switch
;
iteration
statements, which consist of the
for
,
while
,
do-while
, and
foreach
loops; and
jump
statements, which include
break
,
continue
,
goto
,
return
, and
throw
. Except for
throw
,
which is part of C#’s exception-handling mechanism and is discussed in Chapter 13, the
others are examined here.
The if Statement
Chapter 2 introduced the
if
statement. It is examined in detail here. The complete form of
the
if
statement is
if(
condition
)
statement
;
else
statement
;
where the targets of the
if
and
else
are single statements. The
else
clause is optional. The
targets of both the
if
and
else
can be blocks of statements. The general form of the
if
using
blocks of statements is
if(
condition
)
{
statement sequence
}
else
{
statement sequence
}
If the conditional expression is true, the target of the
if
will be executed; otherwise, if it
exists, the target of the
else
will be executed. At no time will both of them be executed.
The conditional expression controlling the
if
must produce a
bool
result.
Here is a simple example that uses an
if
and
else
to report if a number is positive or
negative:
// Determine if a value is positive or negative.
using System;
85
CHAPTER
www.freepdf-books.com
86
P a r t I :
T h e C # L a n g u a g e
class PosNeg {
static void Main() {
int i;
for(i=-5; i <= 5; i++) {
Console.Write("Testing " + i + ": ");
if(i < 0) Console.WriteLine("negative");
else Console.WriteLine("positive");
}
}
}
The output is shown here:
Testing -5: negative
Testing -4: negative
Testing -3: negative
Testing -2: negative
Testing -1: negative
Testing 0: positive
Testing 1: positive
Testing 2: positive
Testing 3: positive
Testing 4: positive
Testing 5: positive
In this example, if
i
is less than zero, then the target of the
if
is executed. Otherwise, the
target of the
else
is executed. In no case are both executed.
Nested ifs
A
nested
if
is an
if
statement that is the target of another
if
or
else
. Nested
if
s are very
common in programming. The main thing to remember about nested
if
s in C# is that an
else
clause always refers to the nearest
if
statement that is within the same block as the
else
and not already associated with an
else
. Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d;
else a = c; // this else refers to if(k > 100)
}
else a = d; // this else refers to if(i == 10)
As the comments indicate, the final
else
is not associated with
if(j < 20)
because it is not
in the same block (even though it is the nearest
if
without an
else
). Rather, the final
else
is associated with
if(i == 10)
. The inner
else
refers to
if(k > 100)
because it is the closest
if
within the same block.
The following program demonstrates a nested
if
. In the positive/negative program
shown earlier, zero is reported as positive. However, as a general rule, zero is considered
signless. The following version of the program reports zero as being neither positive nor
negative.
www.freepdf-books.com
PART I
C h a p t e r 5 :
P r o g r a m C o n t r o l S t a t e m e n t s
87
PART IPART I
// Determine if a value is positive, negative, or zero.
using System;
class PosNegZero {
static void Main() {
int i;
for(i=-5; i <= 5; i++) {
Console.Write("Testing " + i + ": ");
if(i < 0) Console.WriteLine("negative");
else if(i == 0) Console.WriteLine("no sign");
else Console.WriteLine("positive");
}
}
}
Here is the output:
Testing -5: negative
Testing -4: negative
Testing -3: negative
Testing -2: negative
Testing -1: negative
Testing 0: no sign
Testing 1: positive
Testing 2: positive
Testing 3: positive
Testing 4: positive
Testing 5: positive
The if-else-if Ladder
A common programming construct that is based upon the nested
if
is the
if-else-if
ladder
.
It
looks like this:
if(
condition
)
statement
;
else if(
condition
)
statement
;
else if(
condition
)
statement
;
.
.
.
else
statement
;
The conditional expressions are evaluated from the top downward. As soon as a true condition
is found, the statement associated with it is executed, and the rest of the ladder is bypassed.
www.freepdf-books.com
88
P a r t I :
T h e C # L a n g u a g e
If none of the conditions is true, then the final
else
clause will be executed. The final
else
often acts as a default condition. That is, if all other conditional tests fail, then the last
else
clause is executed. If there is no final
else
and all other conditions are false, then no action
will take place.
The following program demonstrates the
if-else-if
ladder. It finds the smallest single-
digit factor (other than 1) for a given value.
// Determine smallest single-digit factor.
using System;
class Ladder {
static void Main() {
int num;
for(num = 2; num < 12; num++) {
if((num % 2) == 0)
Console.WriteLine("Smallest factor of " + num + " is 2.");
else if((num % 3) == 0)
Console.WriteLine("Smallest factor of " + num + " is 3.");
else if((num % 5) == 0)
Console.WriteLine("Smallest factor of " + num + " is 5.");
else if((num % 7) == 0)
Console.WriteLine("Smallest factor of " + num + " is 7.");
else
Console.WriteLine(num + " is not divisible by 2, 3, 5, or 7.");
}
}
}
The program produces the following output:
Smallest factor of 2 is 2.
Smallest factor of 3 is 3.
Smallest factor of 4 is 2.
Smallest factor of 5 is 5.
Smallest factor of 6 is 2.
Smallest factor of 7 is 7.
Smallest factor of 8 is 2.
Smallest factor of 9 is 3.
Smallest factor of 10 is 2.
11 is not divisible by 2, 3, 5, or 7.
As you can see, the
else
is executed only if none of the preceding
if
statements succeeds.
The switch Statement
The second of C#’s selection statements is
switch
. The
switch
provides for a multiway branch.
Thus, it enables a program to select among several alternatives. Although a series of nested
if
statements can perform multiway tests, for many situations the
switch
is a more efficient
approach. It works like this: The value of an expression is successively tested against a list
of constants. When a match is found, the statement sequence associated with that match is
executed. The general form of the
switch
statement is
www.freepdf-books.com
Do'stlaringiz bilan baham: |