The C# compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what is referred to as the dangling-else problem. The indentation of the nested statement

```
if (x > 5)
if (y > 5)
Console.WriteLine("x and y are > 5");
else
Console.WriteLine("x is <= 5");
```
appears to indicate that if x is greater than 5, the nested if statement determines whether y is also greater than 5. If so, the statement outputs the string "x and y are > 5". Otherwise, it appears that if x is not greater than 5, the else part of the if…else outputs the string "x is <= 5". Beware! This nested if…else statement does not execute as it appears. The compiler actually interprets the statement as
```
if (x > 5)
if (y > 5)
Console.WriteLine("x and y are > 5");
else
Console.WriteLine("x is <= 5");
```
in which the body of the first if is a nested if…else. The outer if statement tests whether x is greater than 5. If so, execution continues by testing whether y is also greater than 5. If the second condition is true, the proper string—"x and y are > 5"—is displayed. However, if the second con- dition is false, the string "x is <= 5" is displayed, even though we know that x is greater than 5. Equally bad, if the outer if statement’s condition is false, the inner if…else is skipped and nothing is displayed. For this exercise, add braces to the preceding code snippet to force the nested if…else statement to execute as it was originally intended.


```
if (x > 5)
{
if (y > 5)
{
Console.WriteLine("x and y are > 5");
}
}
else
{
Console.WriteLine("x is <= 5");
}
```

Computer Science & Information Technology

You might also like to view...

Select all that apply. Which of the following is a check for data accuracy?

a. the user enters the number of hours watching movies in a given day b. the user enters a number in an allowable range c. the user selects a username of any length, using any keyboard characters d. the user enters a phone number to be formatted as (555)555-5555

Computer Science & Information Technology

Draw a use case diagram for a ticket distributor for a train system. The system includes two actors: a traveler, who purchases different types of tickets, and a central computer system, which maintains a reference database for the tariff. Use cases should include: BuyOneWayTicket, BuyWeeklyCard, BuyMonthlyCard, UpdateTariff. Also include the following exceptional cases: Time-Out (i.e., traveler took too long to insert the right amount), TransactionAborted (i.e., traveler selected the cancel button without completing the transaction), DistributorOutOfChange, and DistributorOutOfPaper.

Computer Science & Information Technology

You can add new page divs to the site as shown in the accompanying figure using the ____ button in the jQuery Mobile category on the Insert panel.

A. Div B. Tool C. Layer D. Page

Computer Science & Information Technology

A(n) ____ is a step-by-step process.

A. function B. method C. algorithm D. object

Computer Science & Information Technology