If the condition evaluates true, the statementor block of statements in the body of theif statement is executed.. The second if statement evaluates true and all the statements in the if
Trang 1It is common to initialize a string variable with a string literal:
string myString = "Hello World";
We cover strings in much greater detail in Chapter 10
Identifiers
An identifier is just the name the programmer chooses for the types, methods, ables, constants, objects, and so on in the program An identifier must begin with aletter or an underscore, and remember that identifiers are case-sensitive, so C# treatssomeName andSomeName as two different identifiers
vari-It is normally not good programming practice to create two variables or
classes with names that are differentiated only by capitalization.
Although the compiler will not be confused, the programmer will be,
and the cost of attempting to maintain such a program can be very high.
The exception to this is the common practice of having a member
vari-able (explained in Chapter 4) and a property with the same name,
differentiated only by using camel notation for the former, and Pascal
notation for the latter.
The Microsoft naming conventions suggest using camel notation (initial lowercase,such as someVariable) for variable names, and Pascal notation (initial uppercase,such asSomeMethodOrProperty) for method names and most other identifiers
Microsoft recommends against Hungarian notation (e.g., iSomeInteger )
and underscores (e.g., Some_Value ) Microsoft’s Charles Simonyi (who
was born September 10, 1948, in Budapest) invented Hungarian
nota-tion, and it was very useful when languages were limited to a small
number of types.
Along with nearly 2 billion other interesting articles, Wikipedia (http://
en.wikipedia.org) provides extensive articles on Hungarian notation, on
Charles Simonyi, and on Richard Dawkins, who holds the Charles
Simonyi Chair for Public Understanding of Science at Oxford
University.
Trang 2In the C# language, spaces, tabs, and newlines are considered to be “whitespace” (so
named because you see only the white of the underlying “page”) Extra whitespace is
generally ignored in C# statements You can write:
myVariable = 5;
or:
myVariable = 5;
and the compiler will treat the two statements as identical
The key word in the preceding rule is “extra” whitespace Some whitespace is notextra; it is required to allow the compiler to differentiate one word from another.Thus, if you were to enter:
int myVariable = 5; // no problem
or:
int myVariable=5; // no problem
both would compile, because the spaces between the identifier myVariable, theassignment operator (=), and the literal value5are “extra.” If, however, you were toenter:
VB programmers take note: in C# the end-of-line has no special
signifi-cance; you end statements with semicolons, not newline characters.
There is no line-continuation character because none is needed.
Statements
In C#, a complete program instruction is called a statement Programs consist of
sequences of C# statements Virtually every statement ends with a semicolon (;) Forexample:
Trang 3int x; // a statement
x = 23; // another statement
int y = x; // yet another statement
C# statements are evaluated in order The compiler starts at the beginning of a ment list and makes its way to the end This would be entirely straightforward, andterribly limiting, were it not for branching There are two types of branches in a C#
state-program: unconditional branches and conditional branches.
Program flow is also affected by looping and iteration statements, which are signaled
by the keywordsfor,while,do,in, andforeach I discuss iteration later in this ter For now, let’s consider some of the more basic methods of conditional andunconditional branching
chap-Unconditional Branching Statements
You can create an unconditional branch in one of two ways The first way is byinvoking a method When the compiler encounters the name of a method, it stopsexecution in the current method and branches to the newly “called” method Whenthat method returns a value, execution picks up in the original method on the linejust below the method call Example 3-6 illustrates
Program flow begins inMain( )and proceeds untilSomeMethod( ) is invoked ing a method is also referred to as “calling” the method) At that point, program flow
(invok-Example 3-6 Calling a method
In Main! Calling SomeMethod( )
Greetings from SomeMethod!
Back in Main( ).
Trang 4branches to the method When the method completes, program flow resumes at thenext line after the call to that method.
The second way to create an unconditional branch is with one of the unconditionalbranch keywords: goto, break, continue, return, or throw I provide additionalinformation about the first three jump statements later in this chapter The returnstatement returns control to the calling method I discuss the final statement,throw,
in Chapter 11
Conditional Branching Statements
A conditional branch is created by a conditional statement, which is signaled by akeyword such asif,else, orswitch A conditional branch occurs only if the condi-tion expression evaluates true
C and C++ programmers take note: unlike C and C++, in which any
expression can be used in a conditional, C# requires that all
condi-tional expressions evaluate to a Boolean value.
if else statements
if elsestatements branch based on a condition The condition is an expression,tested in the head of theifstatement If the condition evaluates true, the statement(or block of statements) in the body of theif statement is executed
if statements may contain an optionalelse statement The elsestatement is cuted only if the expression in the head of theif statement evaluates false:
You can also see that theelsestatement is optional, as it is enclosed in square brackets
Square brackets are used in the documentation to indicate that the
expression is optional Parentheses (in the if statement) are not part of
the documentation, they are actually required in the code.
Although this gives you the syntax of anifstatement, an illustration will make itsuse clear See Example 3-7
Trang 5In Example 3-7, the first if statement tests whether valueOne is greater thanvalueTwo The relational operators such as greater than (>), less than (<), and equal to(==) are fairly intuitive to use.
The test of whethervalueOneis greater thanvalueTwoevaluates false (becausevalueOne
is10andvalueTwois20, sovalueOneis not greater thanvalueTwo) Theelsestatement
is invoked, printing the statement:
ValueTwo: 20 is larger than ValueOne: 10
Example 3-7 if else statements
Console.WriteLine("\nSetting valueTwo to valueOne value, ");
Console.WriteLine("and incrementing ValueOne.\n");
Trang 6The second if statement evaluates true and all the statements in the if block areevaluated, causing two lines to print:
Setting valueTwo to valueOne value,
and incrementing ValueOne.
ValueOne: 31 ValueTwo: 30
Nested if statements
It is possible, and not uncommon, to nest if statements to handle complex tions For example, suppose you need to write a program to evaluate the temperature,and specifically to return the following types of information:
condi-• If the temperature is 32 degrees or lower, the program should warn you aboutice on the road
• If the temperature is exactly 32 degrees, the program should tell you that theremay be ice patches
There are many good ways to write this program Example 3-8 illustrates oneapproach, using nestedif statements
Statement Blocks
You can substitute a statement block anywhere that C# expects a statement A
statement block is a set of statements surrounded by braces.
Thus, where you might write:
Trang 7The logicof Example 3-8 is that it tests whether the temperature is less than or equal
to 32 If so, it prints a warning:
if (temp <= 32)
{
Console.WriteLine("Warning! Ice on road!");
The program then checks whether the temp is equal to 32 degrees If so, it prints onemessage; if not, the temp must be less than 32, and the program prints the secondmessage Notice that this second ifstatement is nested within the first if, so thelogicof theelseis “since it has been established that the temp is less than or equal to
32, and it isn’t equal to 32, it must be less than 32.”
switch statements: an alternative to nested ifs
Nestedifstatements can be hard to read, hard to get right, and hard to debug whenused to excess (do not operate heavy machinery when using more than six)
When you have a complex set of choices to make, the switch statement may be amore readable alternative The logicof aswitchstatement is “pick a matching valueand act accordingly”:
Trang 8As you can see, like anifstatement, the expression is put in parentheses in the head
of the switch statement Eachcase statement then requires a constant expression;that is, a literal or symbolic constant or an enumeration If a case is matched, thestatement(s) associated with that case is executed This must be followed by a jumpstatement Typically, the jump statement isbreak, which transfers execution out ofthe switch An alternative is a gotostatement, typically used to jump into anothercase, as Example 3-9 illustrates
All Operators Aren’t Created Equal
A closer examination of the secondifstatement in Example 3-8 reveals a commonpotential problem Thisif statement tests whether the temperature is equal to 32:
if (temp == 32)
In C and C++, there is an inherent danger in this kind of statement It’s not uncommonfor novice programmers to use the assignment operator rather than the equals opera-tor, instead creating the statement:
if (temp = 32)
This mistake would be difficult to notice, and the result would be that32was assigned
totemp, and32would be returned as the value of the assignment statement Becauseany nonzero value evaluates true in C and C++ theifstatement would return true Theside effect would be thattempwould be assigned a value of32whether or not it origi-nally had that value This is a common bug that could easily be overlooked—if thedevelopers of C# had not anticipated it!
C# solves this problem by requiringifstatements to accept only Boolean values The
32returned by the assignment is not Boolean (it is an integer) and, in C#, there is noautomaticconversion from32totrue Thus, this bug would be caught at compile time,which is a very good thing and a significant improvement over C++, at the small cost
of not allowing implicit conversions from integers to Booleans!
C++ programmers take note: because the buggy assignment statement will be caught
at compile time, it is no longer necessary to use the counterintuitive syntax:
if ( 32 == temp )
that was C++’s solution to this problem
Example 3-9 The switch statement
Trang 9In this whimsical example, we create constants for various political parties We thenassign one value (Libertarian) to the variablemyChoiceand switch according to thatvalue IfmyChoiceis equal toDemocrat, we print out a statement Notice that this caseends withbreak.breakis a jump statement that takes us out of theswitchstatementand down to the first line after the switch, on which we print, “Thank you for voting.”
VB 6 programmers take note: the equivalent of the C#switch
state-ment is the VB 6 Select Case statement Also, whereas VB 6 allows you
to test a range of values using a single Case statement, C# syntax
doesn’t provide for this contingency The following two Case
state-ments are syntactically correct in VB 6:
Case Is > 100 Case 50 to 60 However, these statements aren’t valid in C# In C#, you can test only
a single constant expression To test a range, you must test each value
independently and “fall through” to a common case block.
Console.WriteLine(" Liberal is now Progressive");
goto case Party.Progressive;
case Party.Progressive:
Console.WriteLine("You voted Progressive.\n");
break;
case Party.Libertarian:
Console.WriteLine("Libertarians are voting Democratic");
goto case Party.Democrat;
Trang 10The value ConservativeRepublicanhas no statement under it, and it “falls through”
to the next statement: Republican If the value is ConservativeRepublican orRepublican, the Republicanstatements execute You can “fall through” in this wayonly if there is no body within the statement If you uncommentWriteLine( )underLiberalRepublican, this program won’t compile
C and C++ programmers take note: you can’t fall through to the next
case unless the case statement is empty Thus, you can write this:
case 1: // fall through ok (no statement for case 1) case 2:
You can’t, however, write this:
case 1:
TakeSomeAction( );
// fall through not OK, case 1 not empty case 2:
Here, case 1 has a statement in it, and you can’t fall through If you
want case 1 to fall through to case 2 , you must explicitly use goto :
goto case Progressive;
It is not required that thegototake you to the next case statement For instance, inthe next example, theLibertarianchoice also has agoto, but this time it jumps allthe way back up to theDemocratcase Because our value was set toLibertarian, this
is just what occurs We print out theLibertarianstatement, go to theDemocratcase,print that statement, and then hit thebreak, taking us out of theswitchand down tothe final statement The output for all of this is:
Libertarians are voting Democrat now.
You voted Democrat.
Thank you for voting.
Note thedefault case, excerpted from Example 3-9:
default:
Console.WriteLine(
"You did not pick a valid choice.\n");
If none of the cases match, thedefaultcase will be invoked, warning the user of themistake
Trang 11Switch on string statements
In the previous example, the switch value was an integral constant C# offers theability to switch on astring, allowing you to write:
The goto statement
Thegotostatement is the seed from which all other iteration statements have beengerminated Unfortunately, it is a semolina seed, producer of spaghetti code and end-less confusion Most experienced programmers properly shun the goto statement,but in the interest of completeness, here’s how you use it:
1 Create a label
2 goto that label
The label is an identifier followed by a colon Thegotocommand is typically tied to acondition, as Example 3-10 illustrates
Example 3-10 Using goto
#region Using directives
Trang 12If you were to try to draw the flow of control in a program that makes extensive use
ofgotostatements, the resulting morass of intersecting and overlapping lines mightlook like a plate of spaghetti; hence the term “spaghetti code.” It was this phenome-non that led to the creation of alternatives such as thewhileloop Many programmersfeel that using gotoin anything other than a trivial example creates confusion anddifficult-to-maintain code
The while loop
The semantics of thewhileloop are “while this condition is true, do this work.” Thesyntax is:
while (expression) statement
As usual, an expression is any statement that returns a value While statementsrequire an expression that evaluates to a Boolean (true/false) value, and that state-ment can, of course, be a block of statements Example 3-11 updates Example 3-10,using awhile loop
goto repeat; // the dastardly deed
return;
}
}
}
Example 3-11 Using a while loop
#region Using directives
Trang 13The code in Example 3-11 produces results identical to the code in Example 3-10, butthe logicis a bit clearer Thewhilestatement is nicely self-contained, and it reads like
an English sentence: “whilei is less than 10, print this message and incrementi.”Notice that thewhileloop tests the value ofibefore entering the loop This ensuresthat the loop will not run if the condition tested is false; thus, ifiis initialized to11,the loop will never run
The do while loop
Awhilestatement will never execute if the condition tested returns false If you want
to ensure that your statement is run at least once, use ado while loop:
do statement while (expression);
An expression is any statement that returns a value Example 3-12 shows the do while loop
Here,iis initialized to11and thewhiletest fails, but only after the body of the loophas run once
The for loop
A careful examination of thewhileloop in Example 3-11 reveals a pattern often seen
in iterative statements: initialize a variable (i = 0), test the variable (i < 10), execute a
Example 3-12 The do while loop
#region Using directives
Trang 14series of statements, and increment the variable (i++) The for loop allows you tocombine all these steps in a single loop statement:
for ([initializers]; [expression]; [iterators]) statement
Example 3-13 illustrates thefor loop
Thisforloop makes use of the modulus operator described later in this chapter Thevalue ofi is printed untili is a multiple of10:
Trang 15VB 6 programmers take note: in C#, looping variables are declared
within the header of the for or foreach statement (rather than before
the statement begins) This means that they are in scope only within
the block, and you can’t refer to them outside the loop I cover the
foreach statement in detail in Chapter 9.
The individual values are printed using Console.Write( ), which is much likeWriteLine( ), but which doesn’t enter a newline, allowing the subsequent writes tooccur on the same line
A few quick points to notice: in aforloop, the condition is tested before the ments are executed Thus, in the example,iis initialized to 0, and then it is tested tosee whether it is less than 100 Becausei < 100 returnstrue, the statements withinthefor loop are executed After the execution,i is incremented (i++)
state-Note that the variableiis scoped to within theforloop (i.e., the variableiis visibleonly within thefor loop) Example 3-14 will not compile
The line shown in bold fails, as the variableiis not available outside the scope of thefor loop itself
Example 3-14 Scope of variables declared in a for loop
#region Using directives
Trang 16The foreach statement
The foreach statement is new to the C family of languages; it is used for loopingthrough the elements of an array or a collection I defer discussion of this incrediblyuseful statement until Chapter 9
The continue and break statements
There are times when you would like to return to the top of a loop without ing the remaining statements in the loop Thecontinuestatement causes the loop toskip the remaining steps in the loop
execut-The other side of that coin is the ability to break out of a loop and immediately endall further work within the loop For this purpose, thebreak statement exists
Example 3-15 illustrates the mechanics ofcontinueandbreak This code, suggested
to us by one of our technical reviewers, is intended to create a traffic signal ing system The signals are simulated by entering numerals and uppercase charactersfrom the keyboard, using Console.ReadLine( ), which reads a line of text from thekeyboard
process-The algorithm is simple: receipt of a 0 (zero) means normal conditions, and no ther action is required except to log the event (In this case, the program simply
fur-Whitespace and Braces
There is much controversy about the use of whitespace in programming For example,thisfor loop:
for (int i=0;i<100;i++)
can be written with more space between the operators:
for ( int i = 0; i < 100; i++ )
Trang 17prefer-writes a message to the console; a real application might enter a timestamped record
in a database.) On receipt of an abort signal (here simulated with an uppercase A),
the problem is logged and the process is ended Finally, for any other event, an alarm
is raised, perhaps notifying the police (Note that this sample doesn’t actually notifythe police, though it does print out a harrowing message to the console.) If the sig-nal isX, the alarm is raised, but thewhile loop is also terminated
Example 3-15 Using continue and break
#region Using directives
string signal = "0"; // initialize to neutral
while ( signal != "X" ) // X indicates stop
// faulty - abort signal processing
// Log the problem and abort.
Console.WriteLine( "Fault! Abort\n" );
break;
}
if ( signal == "0" )
{
// normal traffic condition
// log and continue on
Console.WriteLine( "All is well.\n" );
continue;
}
// Problem Take action and then log the problem
// and then continue on
Trang 18The point of this exercise is that when theA signal is received, the action in theif
statement is taken, and then the program breaks out of the loop without raising the
alarm When the signal is0, it is also undesirable to raise the alarm, so the program
continues from the top of the loop.
Operators
An operator is a symbol that causes C# to take an action The C# primitive types (e.g.,
int) support a number of operators such as assignment, increment, and so forth
The Assignment Operator (=)
The =symbol causes the operand on the left side of the operator to have its valuechanged to whatever is on the right side of the operator Statements that evaluate to
a value are called expressions You may be surprised how many statements do
evalu-ate to a value For example, an assignment such as:
myVariable = 57;
is an expression; it evaluates to the value assigned, which, in this case, is57
Note that the preceding statement assigns the value 57to the variable myVariable.The assignment operator (=) doesn’t test equality; rather, it causes whatever is on theright side (57) to be assigned to whatever is on the left side (myVariable)
VB programmers take note: C# distinguishes between equality (two
equals signs) and assignment (one equals sign).
Console.WriteLine( "{0} raise alarm!\n",
Trang 19BecausemyVariable = 57(read aloud as “assign the numericvalue 57 to the variablewhose name is myVariable”) is an expression that evaluates to57, it can be used aspart of another assignment operator, such as:
mySecondVariable = myVariable = 57;
In this statement, the literal value57is assigned to the variablemyVariable The value
of that assignment (57) is then assigned to the second variable, mySecondVariable.Thus, the value57 is assigned to both variables
The value 57is referred to as a literal value (as opposed to a symbolic
value) A symbolicvalue is one that is housed in a variable, a
con-stant, or an expression A literal value is the value itself, written in the
Simple arithmetical operators (+, -, *, /)
C# offers operators for simple arithmetic: the addition (+), subtraction (-), cation (*), and division (/) operators work as you might expect, with the possibleexception of integer division
multipli-When you divide two integers, C# divides like a child in fourth grade: it throwsaway any fractional remainder Thus, dividing 17 by 4 returns the value4(17/4 = 4,with a remainder of1) C# provides a special operator (modulus,%, which I describe
in the next section) to retrieve the remainder
Note, however, that C# does return fractional answers when you divide floats, bles, and decimals
dou-The modulus operator (%) to return remainders
To find the remainder in integer division, use the modulus operator (%) For example,the statement17%4 returns1 (the remainder after integer division)
The modulus operator turns out to be more useful than you might at first imagine.When you perform modulus non a number that is a multiple of n, the result is 0.
Thus,80%10 = 0because 80 is an exact multiple of 10 This fact allows you to set up
Trang 20loops in which you take an action every nth time through the loop by testing a
counter to see whether%nis equal to 0 This strategy comes in handy in the use of theforloop, as I described earlier in this chapter Example 3-16 illustrates the effects ofdivision on integers, floats, doubles, and decimals
Example 3-16 Division and modulus
#region Using directives
Trang 21Now, consider this line from Example 3-16:
is very similar It prints float: followed by two tabs (to ensure alignment), thecontents of the second parameter ({1}), and then another newline Notice the subse-quent line, as well:
Console.WriteLine("\nModulus:\t{0}", i1%i2);
This time, the string begins with a newline character, which causes a line to beskipped just before the stringModulus:is printed You can see this effect in the output
Increment and Decrement Operators
A common requirement is to add a value to a variable, subtract a value from a able, or otherwise change the mathematical value, and then to assign that new valueback to the same variable You might even want to assign the result to another vari-able altogether The following two sections discuss these cases respectively
vari-Calculate and reassign operators
Suppose you want to increment themySalaryvariable by 5,000 You can do this bywriting:
mySalary = mySalary + 5000;
The addition happens before the assignment, and it is perfectly legal to assign theresult back to the original variable Thus, after this operation completes, mySalarywill have been incremented by 5,000 You can perform this kind of assignment withany mathematical operator:
mySalary += 5000;
mySalary *= 5000;
mySalary -= 5000;
Trang 22The effect of this is to incrementmySalaryby 5,000, multiplymySalaryby 5,000, andsubtract 5,000 from themySalary variable, respectively.
Because incrementing and decrementing by 1 is a very common need, C# (like C andC++ before it) also provides two special operators To increment by 1, you use the++operator, and to decrement by 1, you use the operator
Thus, if you want to increment the variablemyAge by 1, you can write:
myAge++;
The prefix and postfix operators
To complicate matters further, you might want to increment a variable and assignthe results to a second variable:
firstValue = secondValue++;
The question arises: do you want to assign before you increment the value, or after?
In other words, ifsecondValuestarts out with the value10, do you want to end withfirstValueandsecondValueequal to11, or do you wantfirstValueto be equal to10(the original value), andsecondValue to be equal to11?
C# (again, like C and C++) offers two flavors of the increment and decrement
opera-tors: prefix and postfix Thus, you can write:
firstValue = secondValue++; // postfix
which will assign first, and then increment (firstValue=10,secondValue=11) You canalso write:
firstValue = ++secondValue; // prefix
which will increment first, and then assign (firstValue=11,secondValue=11)
It is important to understand the different effects of prefix and postfix, as illustrated
in Example 3-17
Example 3-17 Prefix versus postfix increment
#region Using directives
Trang 23Relational Operators
Relational operators are used to compare two values, and then return a Boolean (true
or false) The greater-than operator (>), for example, returns true if the value on theleft of the operator is greater than the value on the right Thus,5 > 2returns the valuetrue, whereas2 > 5 returns the valuefalse
Table 3-3 shows the relational operators for C# This table assumes two variables:bigValue and smallValue, in which bigValue has been assigned the value 100 andsmallValue the value50
Each relational operator acts as you might expect However, take note of the equalsoperator (==), which is created by typing two equals signs (=) in a row (i.e., withoutany space between them); the C# compiler treats the pair as a single operator
Table 3-3 C# relational operators (assumes bigValue = 100 and smallValue = 50)
Name Operator Given this statement The expression evaluates to
bigValue == 80
true false
bigValue != 80
false true Greater than > bigValue > smallValue true
Greater than or equals >= bigValue >= smallValue
smallValue >= bigValue
true false
Less than or equals <= smallValue <= bigValue
bigValue <= smallValue
true false
Example 3-17 Prefix versus postfix increment (continued)
Trang 24The C# equals operator (==) tests for equality between the objects on either side ofthe operator This operator evaluates to a Boolean value (trueorfalse) Thus, theexpression:
myX == 5
evaluates totrue if and only ifmyX is a variable whose value is5
Use of Logical Operators with Conditionals
If statements (discussed earlier in this chapter) test whether a condition is true.Often, you will want to test whether two conditions are true, or whether only one istrue or none is true C# provides a set of logical operators for this, as shown inTable 3-4 This table assumes two variables,xandy, in whichxhas the value5andythe value7
The andoperator (&&) tests whether two statements are both true The first line inTable 3-4 includes an example that illustrates the use of theand operator:
(x == 3) && (y == 7)
The entire expression evaluates false because one side (x == 3) is false
With theoroperator (||), either or both sides must be true; the expression is falseonly if both sides are false So, in the case of the example in Table 3-4:
(x == 3) || (y == 7)
the entire expression evaluates true because one side (y==7) is true
With anotoperator (!), the statement is true if the expression is false, and vice versa
So, in the accompanying example:
! (x == 3)
the entire expression is true because the tested expression (x==3) is false (The logicis
“it is true that it is not true that x is equal to 3.”)
Operator Precedence
The compiler must know the order in which to evaluate a series of operators Forexample, if we write:
myVariable = 5 + 7 * 3;
Table 3-4 C# logical operators (assumes x = 5, y = 7)
Name Operator Given this statement The expression evaluates to
and && (x == 3) && (y == 7) false
Trang 25there are three operators for the compiler to evaluate (=,+, and*) It could, for ple, operate left to right, which would assign the value5tomyVariable, then add 7 tothe 5 (12) and multiply by 3 (36)—but, of course, then it would throw that 36 away.This is clearly not what is intended.
exam-The rules of precedence tell the compiler which operators to evaluate first As is thecase in algebra, multiplication has higher precedence than addition, so 5+7*3 is equal
to 26 rather than 36 Both addition and multiplication have higher precedence thanassignment, so the compiler will do the math, and then assign the result (26) tomyVariable only after the math is completed
Short-Circuit Evaluation
Short-circuit evaluation allows you to test one-half of an expression and never evaluatethe second half if there is no logical way it can matter In the case of an AND expres-sion, the right half won’t be evaluated if the left half is false
Consider the following code snippet:
if ( (x != null) && (x.IsBigAndScary ) )
The entireifstatement is in parentheses, as are the two conditions to be tested thing within the outer parentheses must evaluate true for the entire expression toevaluate true, and thus both of the inner expressions must be true for the entire expres-sion to be true But here is the kicker: the compiler guarantees that it will evaluate thesetwo inner expressions left to right Thus,xwill be tested for null first, and if it is null,
Every-the second expression will never be tested (which is a good thing because accessing aproperty on a null object will throw an exception)
This is just like writing:
The logicof the first statement was “xmust be non-null AND it must be Big and Scary,
so if it is null, stop evaluating and don’t execute the action.”
The logic of the second short-circuit statement is “xmay be null or it may be Big andScary Ifxis null we’re done, go ahead and execute If it is not null, go see whether it
is Big and Scary, and if it is, execute.”
These tests are not quite identical (in the first, you never execute with a nullx), but theyboth protect you from evaluating whetherx is Big and Scary ifx is null