Relational, Logical, and Conditional Operators
AAHC

Click on A to make all fonts on the page smaller.

Click on A to make all fonts on the page larger.

Click on HC to toggle high contrast mode. When you move your mouse over some bold words in high contrast mode, related words are automatically highlighted. Text is shown in black and white.

In this lesson we'll explore operators that help us to make decisions through comparisons.

Relational Operators

You've seen relational operatorsbefore. These include:

• <: Less than
• >: Greater than
• <=: Less than or equal
• >=: Greater than or equal
• ==: Equal
• !=: Not equal

We're going to move on to logical operators. We assume you understand the relational operators above; if you have any questions about them, please review the earlier lessons before proceeding.

Boolean and Bitwise Logical Operators

Logical operators are of two kinds: boolean, and bitwise:

• &&: AND
• ||: OR
• !: NOT

#### Bitwise Logical Operators

• &: AND
• |: OR
• ^: XOR (exclusive OR)
• ~: Complement

In addition to these two categories, when working with logical operators, we also use true and false.

Body Fat Calculator: Using Relational and Boolean Logical Operators

Let's create a project and explore boolean logical and relational operators. We'll save bitwise logical operators for a later lesson where we can devote time to discussing numbering systems, conversions, and bit manipulations.

 Note From now on, we'll assume you know how to create new project, rename the form, and change the form's title bar. We'll just give the name for the new project and you can do the rest.

Create a new Project named BodyFatCalculator. Rename the form to BodyFatCalculator.cs, and change the title bar to Body Fat Calculator.

Drag a TabControl control from the Toolbox onto your form, and resize the TabControl to take up most of the space, leaving some room at the bottom for a Button. Change the Name property to bodyFatTabControl. Remember to save your changes periodically .

To work with each individual tab, or page, we will need to use the TabPage Collection Editor.

Select the TabPages Collection property in the Properties Window, and click on the ellipsis (...). In the TabPages Collection Editor dialog box, in the Members ListBox, click on tabPage1. Change the Name property to calculatorPage. Change the Text property to Calculator. Then, again in the TabPages Collection Editor dialog box, in the Members ListBox, click on tabPage2. Change the Name property to chartPage. Change the Text property to Chart. Click OK and .

Drag a Button control from the Toolbox onto your form below the TabControl. Change the Name property to closeButton. Change the Text property to Close. Save your changes .

Next, we'll add five Label controls and five TextBox controls. Rather than individual images for each control that changes the Name and Text properties, continue to use the pattern you've already established for changing these properties for each control as you add it.

Drag five Label controls and five TextBox controls from the Toolbox onto the Calculator Tab Page. Align the Label controls in one column, and the TextBox controls in a second column. Change the Name and Text properties as follows:

Label Controls
Original NameNew NameText Property
label1weightLabelWeight
label2waistLabelWaist
label3wristLabelWrist
label4hipsLabelHips
label5forearmLabelForearm
TextBox Controls
Original NameNew Name
textBox1weightTextBox
textBox2waistTextBox
textBox3wristTextBox
textBox4hipsTextBox
textBox5forearmTextBox

When you finish, it should look like this:

 Note If you find yourself spending a lot of time trying to align the Label and TextBox controls to match the image, consider using the Format menu option to help. Start with the TextBoxes, because they take up more vertical space than the Labels; once you have the TextBoxes correctly placed, you can align each Label to the corresponding TextBox. You can use Vertical Spacing | Make Equal to equalize the vertical spacing between each TextBox. You can use Align | Lefts to make all of the controls line up. Then, select each TextBox, Ctrl-click its corresponding Label control, and use Align | Middles.

Next, we'll add two RadioButton controls to select Male or Female, a Button control to perform the calculation, and another Label control to display the results.

Drag two RadioButton controls from the Toolbox onto the Calculator Tab Page. Align each RadioButton under each column. Add a new Button control to the Calculator Tab Page below the RadioButton controls. Add a Label control beneath the Button control. For the Label, change the Visible property to False. Again, we'll provide a table of the Name and Text property values, rather than show each individual property.

 Note You'll need to increase the size of the Form, and the size of the TabControl, to have enough space for these changes.

Original NameNew NameText Property
button1calculateButtonCalculate Body Fat
label1bodyFatLabelBody Fat Percentage:

Your form should look like this:

Sometimes, you realize you forgot a control, and have to move controls around to squeeze in what you forgot. We need to add another label control between the last TextBox and the RadioButtons that will help us to distinguish which controls are only necessary for females.

Make enough room for a label between the forearmTextBox and the RadioButtons. Drag a Label control into that space on the Calculator Tab Page. Change the label's Name to requiredForFemaleLabel and the Text to * Only required for Female:

Next, let's add Label controls following each TextBox control to indicate the units, and to add the female-only indicator, an asterisk.

Drag five Label controls from the Toolbox onto the Calculator Tab Page, placing each to the right of each TextBox control. Change the Name and Text properties as shown below:

Original NameNew NameText Property
label1weightUnitLabelpounds
label2waistUnitLabelinches (at navel)
label3wristUnitLabelinches (at fullest point)*
label4hipsUnitLabelinches (at fullest point)*
label5forearmUnitLabelinches (at fullest point)*

Your form should now look like this:

Finally, let's add the Body Fat Chart labels to the Chart Page of the TabControl control.

Drag five Label controls from the Toolbox onto the Chart Tab Page. Change the Name and Text properties as shown below.

Original NameNew NameText Property
label1essentialLabelEssential Fat: Women 10-12%, Men 2-4%
label2athletesLabelAthletes: Women 14-20%, Men 6-13%
label3fitnessLabelFitness: Women 21-24%, Men 14-17%
label4acceptableLabelAcceptable: Women 25-31%, Men 18-25%
label5obeseLabelObese: Women 32%+, Men 25%+

Your form should look like this:

So, maybe it's time to get back to the purpose of the lesson: boolean and relational operators! Let's start adding code!

Double-click the closeButton to add an event handler for this button. Add code as shown below (some code is omitted from this example for brevity):

BodyFatCalculator.cs
```using ...

namespace BodyFatCalculator
{
public partial class BodyFatCalculator : Form
{
public BodyFatCalculator()
{
InitializeComponent();
// Default to female.
}

private double calculateBodyFat(bool isFemale, double weightFactor, double wristFactor,
double waistFactor, double hipsFactor, double forearmFactor)
{
double leanBodyMass = 0;
double bodyFatWeight = 0;
double bodyWeight = weightFactor;

if (isFemale)
{
// Calculate for female.
weightFactor = weightFactor * 0.732 + 8.987;
wristFactor /= 3.140;
waistFactor *= 0.157;
hipsFactor *= 0.249;
forearmFactor *= 0.434;
leanBodyMass = weightFactor + wristFactor - waistFactor - hipsFactor + forearmFactor;
bodyFatWeight = bodyWeight - leanBodyMass;
}
else
{
// Calculate for male.
weightFactor = weightFactor * 1.082 + 94.42;
waistFactor *= 4.15;
leanBodyMass = weightFactor - waistFactor;
bodyFatWeight = bodyWeight - leanBodyMass;
}

return bodyFatWeight;
}

private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
```

Let's discuss how this code works.

OBSERVE: BodyFatCalculator.cs
```.
.
.

namespace BodyFatCalculator
{
public partial class BodyFatCalculator : Form
{
public BodyFatCalculator()
{
InitializeComponent();
// Default to female.
}

private double calculateBodyFat(bool isFemale, double weightFactor, double wristFactor,
double waistFactor, double hipsFactor, double forearmFactor)
{
double leanBodyMass = 0;
double bodyFatWeight = 0;
double bodyWeight = weightFactor;

if (isFemale)
{
// Calculate for female.
weightFactor = weightFactor * 0.732 + 8.987;
wristFactor /= 3.140;
waistFactor *= 0.157;
hipsFactor *= 0.249;
forearmFactor *= 0.434;
leanBodyMass = weightFactor + wristFactor - waistFactor - hipsFactor + forearmFactor;
bodyFatWeight = bodyWeight - leanBodyMass;
}
else
{
// Calculate for male.
weightFactor = weightFactor * 1.082 + 94.42;
waistFactor *= 4.15;
leanBodyMass = weightFactor - waistFactor;
bodyFatWeight = bodyWeight - leanBodyMass;
}

return bodyFatWeight;
}
.
.
.
private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
```

Again, the comments in the code should help you understand the process used to make the code work. We added a line of code to make sure the femaleRadioButton is selected using the Checked property. We added a private method named calculateBodyFat to isolate the code that calculates the actual body fat. We also added a this.Close() method call to enable closing the application using the closeButton.

Within the calculateBodyFat, we used if (isFemale). You may remember from previous lessons that an if statement must evaluate to a boolean value. isFemale is a boolean variable; simply using its name is by definition acceptable within an if statement. You can read this expression as if isFemale is true. You could also write this expression as if (isFemale == true), or if (isFemale != false).

Next, let's add the event handler and code to perform the body fat calculation.

Go back to the Design tab and double-click the Calculate Body Fat button to add the event handler. Then, add the code shown below:

BodyFatCalculator.cs
```using ...

namespace BodyFatCalculator
{
public partial class BodyFatCalculator : Form
{
public BodyFatCalculator()
{
InitializeComponent();
// Default to female.
}

private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}

private double calculateBodyFat(bool isFemale, double weightFactor, double wristFactor,
double waistFactor, double hipsFactor, double forearmFactor)
{
...
}

private void calculateButton_Click(object sender, EventArgs e)
{
double weightFactor = 0;
double waistFactor = 0;
double wristFactor = 0;
double hipsFactor = 0;
double forearmFactor = 0;
double bodyFatPercentage = 0;
bool isFemale = false;

// See if female.
isFemale = true;

// Attempt to convert TextBox values to doubles, marking unit labels red for failures.
if (!double.TryParse(weightTextBox.Text, out weightFactor))
weightUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(waistTextBox.Text, out waistFactor))
waistUnitLabel.ForeColor = Color.Red;
if (isFemale)
{
if (!double.TryParse(wristTextBox.Text, out wristFactor))
wristUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(hipsTextBox.Text, out hipsFactor))
hipsUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(forearmTextBox.Text, out forearmFactor))
forearmUnitLabel.ForeColor = Color.Red;
}

// Make sure all values are above zero.
if (weightFactor <= 0)
weightUnitLabel.ForeColor = Color.Red;
if (waistFactor <= 0)
waistUnitLabel.ForeColor = Color.Red;
if (isFemale)
{
if (wristFactor <= 0)
wristUnitLabel.ForeColor = Color.Red;
if (hipsFactor <= 0)
hipsUnitLabel.ForeColor = Color.Red;
if (forearmFactor <= 0)
forearmUnitLabel.ForeColor = Color.Red;
}

// Check for error.
bool isError = false;
if (isFemale)
isError = (weightUnitLabel.ForeColor == Color.Red || waistUnitLabel.ForeColor == Color.Red || wristUnitLabel.ForeColor == Color.Red
|| hipsUnitLabel.ForeColor == Color.Red || forearmUnitLabel.ForeColor == Color.Red);
else
isError = (weightUnitLabel.ForeColor == Color.Red || waistUnitLabel.ForeColor == Color.Red);

if (!isError)
{
// Display the body fat Label.
bodyFatLabel.Visible = true;
bodyFatLabel.ForeColor = Color.Red;

// No errors, so calculate and display body fat percentage, formatting to two decimal places.
bodyFatPercentage = (calculateBodyFat(isFemale, weightFactor, wristFactor, waistFactor, hipsFactor, forearmFactor) * 100) / weightFactor;
bodyFatLabel.Text = "Body Fat Percentage: " + bodyFatPercentage.ToString("#0.00") + "%";
}
}

}
}
```

Save your changes , and click to run the program. Try various kinds of entries, including illegal ones, such as not entering a forearm measurement for a female.

Now let's discuss how it works.

OBSERVE: BodyFatCalculator.cs
```.
.
.
namespace BodyFatCalculator
{
public partial class BodyFatCalculator : Form
{
public BodyFatCalculator()
{
InitializeComponent();
// Default to female.
}

private double calculateBodyFat(bool isFemale, double weightFactor, double wristFactor,
double waistFactor, double hipsFactor, double forearmFactor)
{
...
}

private void closeButton_Click(object sender, EventArgs e)
{
this.Close();
}

private void calculateButton_Click(object sender, EventArgs e)
{
double weightFactor = 0;
double waistFactor = 0;
double wristFactor = 0;
double hipsFactor = 0;
double forearmFactor = 0;
double bodyFatPercentage = 0;
bool isFemale = false;

// See if female.
isFemale = true;

// Attempt to convert TextBox values to doubles, marking unit labels red for failures.
if (!double.TryParse(weightTextBox.Text, out weightFactor))
weightUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(waistTextBox.Text, out waistFactor))
waistUnitLabel.ForeColor = Color.Red;
if (isFemale)
{
if (!double.TryParse(wristTextBox.Text, out wristFactor))
wristUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(hipsTextBox.Text, out hipsFactor))
hipsUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(forearmTextBox.Text, out forearmFactor))
forearmUnitLabel.ForeColor = Color.Red;
}

// Make sure all values are above zero.
if (weightFactor <= 0)
weightUnitLabel.ForeColor = Color.Red;
if (waistFactor <= 0)
waistUnitLabel.ForeColor = Color.Red;
if (isFemale)
{
if (wristFactor <= 0)
wristUnitLabel.ForeColor = Color.Red;
if (hipsFactor <= 0)
hipsUnitLabel.ForeColor = Color.Red;
if (forearmFactor <= 0)
forearmUnitLabel.ForeColor = Color.Red;
}

// Check for error.
bool isError = false;
if (isFemale)
isError = (weightUnitLabel.ForeColor == Color.Red || waistUnitLabel.ForeColor == Color.Red || wristUnitLabel.ForeColor == Color.Red
|| hipsUnitLabel.ForeColor == Color.Red || forearmUnitLabel.ForeColor == Color.Red);
else
isError = (weightUnitLabel.ForeColor == Color.Red || waistUnitLabel.ForeColor == Color.Red);

if (!isError)
{
// Display the body fat Label.
bodyFatLabel.Visible = true;
bodyFatLabel.ForeColor = Color.Red;

// No errors, so calculate and display body fat percentage, formatting to two decimal places.
bodyFatPercentage = (calculateBodyFat(isFemale, weightFactor, wristFactor, waistFactor, hipsFactor, forearmFactor) * 100) / weightFactor;
bodyFatLabel.Text = "Body Fat Percentage: " + bodyFatPercentage.ToString("#0.00") + "%";
}
}
}
}
```

We modified the calculateButton_Click event handler to try to convert each of the TextBox control values to double values. We change the Unit Label controls to red if any of the TextBox control values fail to convert to a valid double or were not above 0. Finally, we check that we don't have any errors, and if not, we go ahead and calculate the percentage of body fat.

Within the calculateButton_Click event handler, we use a few more relational and boolean operators. We use the TryParse method of double to attempt to convert each of the TextBox control values. We use the ! operator to to indicate we want to check if the TryParse method returned false. Literally, the ! reverses the value, so you could read it as if the result of TryParse is NOT true.

After attempting to convert each TextBox control value, we then test to see that each value is greater than zero using the <= relational operator. We also test to see if any of the Unit Label controls have their ForeColor set to Color.Red using the boolean operator || and relational operator ==. The isError boolean variable will be true if any of the Unit Label controls have their ForeColor set to Color.Red.

Read through the code again, looking for any other use of relational or boolean operators.

Before finishing this section, we do want to add a bit more code, just for fun, to support the display of a ToolTip to show the error message. A ToolTip is a popup that appears if the mouse pointer hovers over a control.

Modify the code as shown below.

BodyFatCalculator.cs
```using ...

namespace BodyFatCalculator
{
public partial class BodyFatCalculator : Form
{
.
.
.
private void calculateButton_Click(object sender, EventArgs e)
{
double weightFactor = 0;
double waistFactor = 0;
double wristFactor = 0;
double hipsFactor = 0;
double forearmFactor = 0;
double bodyFatPercentage = 0;
bool isFemale = false;

// See if female.
isFemale = true;

// Set all unit labels to black.
weightUnitLabel.ForeColor = Color.Black;
waistUnitLabel.ForeColor = Color.Black;
wristUnitLabel.ForeColor = Color.Black;
hipsUnitLabel.ForeColor = Color.Black;
forearmUnitLabel.ForeColor = Color.Black;

// Attempt to convert TextBox values to doubles, marking unit labels red for failures.
if (!double.TryParse(weightTextBox.Text, out weightFactor))
weightUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(waistTextBox.Text, out waistFactor))
waistUnitLabel.ForeColor = Color.Red;
if (isFemale)
{
if (!double.TryParse(wristTextBox.Text, out wristFactor))
wristUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(hipsTextBox.Text, out hipsFactor))
hipsUnitLabel.ForeColor = Color.Red;
if (!double.TryParse(forearmTextBox.Text, out forearmFactor))
forearmUnitLabel.ForeColor = Color.Red;
}

// Make sure all values are above zero.
if (weightFactor <= 0)
weightUnitLabel.ForeColor = Color.Red;
if (waistFactor <= 0)
waistUnitLabel.ForeColor = Color.Red;
if (isFemale)
{
if (wristFactor <= 0)
wristUnitLabel.ForeColor = Color.Red;
if (hipsFactor <= 0)
hipsUnitLabel.ForeColor = Color.Red;
if (forearmFactor <= 0)
forearmUnitLabel.ForeColor = Color.Red;
}

// See if there were any errors by checking Unit label Forecolor.
// Weight
if (weightUnitLabel.ForeColor == Color.Red)
setToolTip(weightLabel, weightTextBox, weightUnitLabel, "Weight (pounds) must be numeric and above 0.");
else
setToolTip(weightLabel, weightTextBox, weightUnitLabel, "");
// Waist
if (waistUnitLabel.ForeColor == Color.Red)
setToolTip(waistLabel, waistTextBox, waistUnitLabel, "Waist (inches) must be numeric and above 0.");
else
setToolTip(waistLabel, waistTextBox, waistUnitLabel, "");
if (isFemale)
{
// Wrist
if (wristUnitLabel.ForeColor == Color.Red)
setToolTip(wristLabel, wristTextBox, wristUnitLabel, "Wrist (inches) must be numeric and above 0.");
else
setToolTip(wristLabel, wristTextBox, wristUnitLabel, "");
// Hips
if (hipsUnitLabel.ForeColor == Color.Red)
setToolTip(hipsLabel, hipsTextBox, hipsUnitLabel, "Hips (inches) must be numeric and above 0.");
else
setToolTip(hipsLabel, hipsTextBox, hipsUnitLabel, "");
// Forearm
if (forearmUnitLabel.ForeColor == Color.Red)
setToolTip(forearmLabel, forearmTextBox, forearmUnitLabel, "Forearm (inches) must be numeric and above 0.");
else
setToolTip(forearmLabel, forearmTextBox, forearmUnitLabel, "");
}

.
.
.

if (!isError)
{
// Display the body fat Label.
bodyFatLabel.Visible = true;
bodyFatLabel.ForeColor = Color.Red;

// No errors, so calculate and display body fat percentage, formatting to two decimal places.
bodyFatPercentage = (calculateBodyFat(isFemale, weightFactor, wristFactor, waistFactor, hipsFactor, forearmFactor) * 100) / weightFactor;
bodyFatLabel.Text = "Body Fat Percentage: " + bodyFatPercentage.ToString("#0.00") + "%";
}
else
{
// There were errors.
bodyFatLabel.Visible = true;
bodyFatLabel.ForeColor = Color.Red;
bodyFatLabel.Text = "Hover mouse over error to see error message";
}
}

private double calculateBodyFat(bool isFemale, double weightFactor, double wristFactor,
double waistFactor, double hipsFactor, double forearmFactor)
{
...
}

private void setToolTip(Control labelControl, Control textBoxControl, Control labelUnitControl, string toolText)
{
// Create a tooltip object.
ToolTip tp = new ToolTip();

// Set tooltip for each control.
tp.SetToolTip(labelControl, toolText);
tp.SetToolTip(textBoxControl, toolText);
tp.SetToolTip(labelUnitControl, toolText);
}
}
}
```

and it. It should look something like this:

Let's discuss how this code works.

OBSERVE: BodyFatCalculator.cs
```.
.
.
private void calculateButton_Click(object sender, EventArgs e)
{
...

// Set all unit labels to black.
weightUnitLabel.ForeColor = Color.Black;
waistUnitLabel.ForeColor = Color.Black;
wristUnitLabel.ForeColor = Color.Black;
hipsUnitLabel.ForeColor = Color.Black;
forearmUnitLabel.ForeColor = Color.Black;

...

// See if there were any errors by checking Unit label Forecolor.
// Weight
if (weightUnitLabel.ForeColor == Color.Red)
setToolTip(weightLabel, weightTextBox, weightUnitLabel, "Weight (pounds) must be numeric and above 0.");
else
setToolTip(weightLabel, weightTextBox, weightUnitLabel, "");
...

if (!isError)
{
...
}
else
{
// There were errors.
bodyFatLabel.Visible = true;
bodyFatLabel.ForeColor = Color.Red;
bodyFatLabel.Text = "Hover mouse over error to see error message";
}
}

private double calculateBodyFat(bool isFemale, double weightFactor, double wristFactor,
double waistFactor, double hipsFactor, double forearmFactor)
{
...
}

private void setToolTip(Control labelControl, Control textBoxControl, Control labelUnitControl, string toolText)
{
// Create a tooltip object.
ToolTip tp = new ToolTip();

// Set tooltip for each control.
tp.SetToolTip(labelControl, toolText);
tp.SetToolTip(textBoxControl, toolText);
tp.SetToolTip(labelUnitControl, toolText);
}
}
}
```

These final additions to the code first make sure that the ForeColor of all Unit Labels is reset Color.Black. Then, we call the setToolTip method to set all three of the related controls to have a ToolTip if there's an error. And of course we created the setToolTip method.

Conditional Operator

The Conditional Operator in C#, also known as the ternary operator because it has three components, is fundamentally a shortcut, or compact, syntax, for the if..else construct. What does it look like?

Conditional Operator Syntax
```Condition ? First expression : Second expression
```

If the Condition component is true, then the First expression is evaluated, and becomes the result. If the Condition is false, then the Second expression is evaluated, and becomes the result.

An if..else structure for the same logic would look like this:

Conditional Operator Syntax
```if (Condition)
First expression
else
Second expression
```

The Conditional Operator is often used to eliminate the more lengthly if..else syntax, especially when the result is to used for output. In fact, we'll replace some of our code with this operator. Before moving on, scan through the code, and see if you can determine what if..else constructs might be replaced.

Modify the code as shown below.

BodyFatCalculator.cs
```.
.
.
// See if there were any errors by checking Unit label Forecolor.
// Weight
setToolTip(weightLabel, weightTextBox, weightUnitLabel, weightUnitLabel.ForeColor == Color.Red ? "Weight (pounds) must be numeric and above 0." : "");
if (weightUnitLabel.ForeColor == Color.Red)
setToolTip(weightLabel, weightTextBox, weightUnitLabel, "Weight (pounds) must be numeric and above 0.");
else
setToolTip(weightLabel, weightTextBox, weightUnitLabel, "");
// Waist
setToolTip(waistLabel, waistTextBox, waistUnitLabel, waistUnitLabel.ForeColor == Color.Red ? "Waist (inches) must be numeric and above 0." : "");
if (waistUnitLabel.ForeColor == Color.Red)
setToolTip(waistLabel, waistTextBox, waistUnitLabel, "Waist (inches) must be numeric and above 0.");
else
setToolTip(waistLabel, waistTextBox, waistUnitLabel, "");
if (isFemale)
{
// Wrist
setToolTip(wristLabel, wristTextBox, wristUnitLabel, wristUnitLabel.ForeColor == Color.Red ? "Wrist (inches) must be numeric and above 0." : "");
if (wristUnitLabel.ForeColor == Color.Red)
setToolTip(wristLabel, wristTextBox, wristUnitLabel, "Wrist (inches) must be numeric and above 0.");
else
setToolTip(wristLabel, wristTextBox, wristUnitLabel, "");
// Hips
setToolTip(hipsLabel, hipsTextBox, hipsUnitLabel, hipsUnitLabel.ForeColor == Color.Red ? "Hips (inches) must be numeric and above 0." : "");
if (hipsUnitLabel.ForeColor == Color.Red)
setToolTip(hipsLabel, hipsTextBox, hipsUnitLabel, "Hips (inches) must be numeric and above 0.");
else
setToolTip(hipsLabel, hipsTextBox, hipsUnitLabel, "");
// Forearm
setToolTip(forearmLabel, forearmTextBox, forearmUnitLabel, forearmUnitLabel.ForeColor == Color.Red ? "Forearm (inches) must be numeric and above 0." : "");
if (forearmUnitLabel.ForeColor == Color.Red)
setToolTip(forearmLabel, forearmTextBox, forearmUnitLabel, "Forearm (inches) must be numeric and above 0.");
else
setToolTip(forearmLabel, forearmTextBox, forearmUnitLabel, "");
}
.
.
.
```

Save your changes , and click to run the program. You should see identical results.

Let's discuss how this code works.

OBSERVE: BodyFatCalculator.cs
```.
.
.
// See if there were any errors by checking Unit label Forecolor.
// Weight
setToolTip(weightLabel, weightTextBox, weightUnitLabel, weightUnitLabel.ForeColor == Color.Red ? "Weight (pounds) must be numeric and above 0." : "");
setToolTip(waistLabel, waistTextBox, waistUnitLabel, waistUnitLabel.ForeColor == Color.Red ? "Waist (inches) must be numeric and above 0." : "");
if (isFemale)
{
// Wrist
setToolTip(wristLabel, wristTextBox, wristUnitLabel, wristUnitLabel.ForeColor == Color.Red ? "Wrist (inches) must be numeric and above 0." : "");
// Hips
setToolTip(hipsLabel, hipsTextBox, hipsUnitLabel, hipsUnitLabel.ForeColor == Color.Red ? "Hips (inches) must be numeric and above 0." : "");
// Forearm
setToolTip(forearmLabel, forearmTextBox, forearmUnitLabel, forearmUnitLabel.ForeColor == Color.Red ? "Forearm (inches) must be numeric and above 0." : "");
}
.
.
.
```

This version certainly makes the section of code that set up the ToolTips much more compact! Rather than using the if..else syntax, we're able to just call the setToolTip method, but now the toolText parameter uses the Conditional Operator. We use weightUnitLabel.ForeColor == Color.Red as the Condition component. If the Condition is true, we set the toolText parameter to "Weight (pounds) must be numeric and above 0.", and if the Condition is false, we set the toolText parameter to "". Very nice!

Before you move on to the next lesson, do your homework! Right-click in the window where this lesson text appears and select Back. Then select Quiz for this lesson in the syllabus and answer the quiz questions. When you finish the quiz questions, click Hand it in at the bottom of that window. Then do the same with the Project(s) for the lesson. Your instructor will grade your quiz(zes) and project(s) and provide guidance if needed.