﻿ Lesson 26: Topics on Logical Operators

Topics on Logical Operators

A Value Greater Than Another: >

To find out if one value is greater than the other, you can use the > operator. Its formula is:

`value1 > value2`

Both operands, in this case value1 and value2, can be variables or the left operand can be a variable while the right operand is a constant. If the value on the left of the > operator is greater than the value on the right side or a constant, the comparison produces a true or positive value. Otherwise, the comparison renders false or null:

Practical Learning: Finding Out Whether a Value is Greater Than Another

1. Start Microsoft Visual Studio and, on the Visual Studion 2019 dialog box, click Create a New Project (if Microsoft Visual Studio was already launched, on the main menu of, click File -> New -> roject...)
2. In the list of projects templates, click Winows Forms App (.NET Framework)
3. Click Next
4. Change the project Name to PayrollEvaluation03
5. Click Create
6. In the Solution Explorer, right-click PayrollEvaluation03 -> Add -> Class...
7. Change the name to Calculations
9. Right-click inside the Code Editor -> Remove and Sort Usings
10. Change the class as follows:
```namespace PayrollEvaluation03
{
static public class Calculations
{
public static double Add(double a, double b)
{
return a + b;
}

static public double Subtract(double a, double b)
{
return a - b;
}

public static double Multiply(double a, double b)
{
return a * b;
}

public static double Add5(double a, double b, double c, double d, double e)
{
return a + b + c + d + e;
}

public static double Multiply3(double a, double b, double c)
{
return a * b * c;
}
}
}```
12. Change the name to TimeSheet
14. Right-click inside the Code Editor -> Remove and Sort Usings
15. Change the code as follows:
```namespace PayrollEvaluation03
{
public class TimeSheet
{
public double Monday { get; set; }
public double Tuesday { get; set; }
public double Wednesday { get; set; }
public double Thursday { get; set; }
public double Friday { get; set; }

public TimeSheet()
{
Monday = 0.00;
Tuesday = 0.00;
Wednesday = 0.00;
Thursday = 0.00;
Friday = 0.00;
}

public TimeSheet(double mon, double tue,
double wed, double thu, double fri)
{
Monday = mon;
Tuesday = tue;
Wednesday = wed;
Thursday = thu;
Friday = fri;
}
}
}```
16. In the Solution Explorer, right-click Form1.cs and click Rename
17. Type PayrollPreparation (to get PayrollPreparation.cs) and press Enter
18. Read the message box and press Enter twice (to access the form)
19. Design the form as follows:

 Control (Name) Text GroupBox Work Preparation Label Hourly Salary: TextBox txtHourlySalary Label Monday Label Tuesday Label Wednesday Label Thursday Label Friday TextBox txtMonday TextBox txtTuesday TextBox txtWednesday TextBox txtThursday TextBox txtFriday Button btnCalculate Calculate GroupBox Pay Summary Label Time Worked: TextBox txtTimeWorked Label Overtime: TextBox txtOvertime Label Net Pay: TextBox txtNetPay
20. Double-click the Calculate button
21. Right-click inside the Code Editor and click Remove and Sort Usings
22. Implement the event as follows:
```using System;
using System.Windows.Forms;

namespace PayrollEvaluation03
{
public partial class PayrollPreparation : Form
{
public PayrollPreparation()
{
InitializeComponent();
}

void Evaluate(TimeSheet ts, double hourlySalary)
{
double overtime = 0.00;
double overtimePay = 0.00;
// mon + tue + wed + thu + fri
double timeWorked = Calculations.Add5(ts.Monday, ts.Tuesday, ts.Wednesday, ts.Thursday, ts.Friday);
double netPay = hourlySalary * timeWorked;

if (timeWorked > 40.00)
{
// timeWorked - 40.00
overtime = Calculations.Subtract(timeWorked, 40.00);
// hourlySalary * 1.50 * overtime
overtimePay = Calculations.Multiply3(hourlySalary, 1.50, overtime);
// (hourlySalary * 40.00) + overtimePay
}

txtTimeWorked.Text = timeWorked.ToString("F");
txtOvertime.Text = overtime.ToString("F");
txtNetPay.Text = netPay.ToString("F");
}

private void btnCalculate_Click(object sender, EventArgs e)
{
double hSalary = 0.00;
TimeSheet record = new TimeSheet();

double mon = Convert.ToDouble(txtMonday.Text);
double tue = Convert.ToDouble(txtTuesday.Text);
double wed = Convert.ToDouble(txtWednesday.Text);
double thu = Convert.ToDouble(txtThursday.Text);
double fri = Convert.ToDouble(txtFriday.Text);

hSalary = Convert.ToDouble(txtHourlySalary.Text);

record = new TimeSheet(mon, tue, wed, thu, fri);

Evaluate(record, hSalary);
}
}
}```
23. To execute the project, press Ctrl + F5

24. In the Hourly Salary text box, type a decimal number such as 25.85
25. In each days text box, type a decimal number between 0 and 8 but divisible by 0.50. Here are examples:

26. Click the Calculate button:

28. Execute the application again and, in the Hourly Salary text box, type a decimal number such as 25.50
29. In each day's text box, type a decimal number between 0 and 16 but divisible by 0.50. Here are examples:

30. Click the Calculate button:

A Value Greater Than or Equal to Another: >=

The greater than or the equality operators can be combined to produce an operator as follows: >=. This is the "greater than or equal to" operator. The formula it follows is:

`Value1 >= Value2`

The comparison is performed on both operands: Value1 and Value2:

• If the value of Value1 and that of Value2 are the same, the comparison produces a true or positive value
• If the value of the left operand is greater than that of the right operand,, the comparison produces true or positive also
• If the value of the left operand is strictly less than the value of the right operand, the comparison produces a false or null result

Practical Learning: Finding Out Whether a Value is Greater or Equal

1. On the main menu of Microsoft Visual Studio, click File -> New -> Project...
2. In the list of projects templates, click Windows Forms App (.NET Framework)
3. Click Next
4. Change the project Name to GasUtilityCompany1
5. Click Create
6. In the Solution Explorer, right-click GasUtilityCompany1 -> Add -> Class..
7. Change the name to Calculations
9. Right-click inside the Code Editor and click Remove and Sort Usings
10. Change class as follows:
```namespace GasUtilityCompany1
{
static public class Calculations
{
public static double Multiply(double a, double b) => a * b;
}
}```
11. In the Solution Explorer, right-click Form1.cs -> Rename
12. Type BillPreparation and press Enter three times
13. Design the form as follows:

 Control (Name) Text TextAlign Label Gas Consumption: TextBox txtConsumption Right Button btnEvaluate Evaluate Label Price Per CCF: TextBox txtPricePerCCF Right Label Monthly Charges: TextBox txtMonthlyCharges Right
14. Double-click the Evaluate button
15. Right-click inside the Code Editor and click Remove and Sort Usings
16. Change the code as follows:
```using System;
using System.Windows.Forms;

namespace GasUtilityCompany1
{
public partial class BillPreparation : Form
{
public BillPreparation()
{
InitializeComponent();
}

private void btnEvaluate_Click(object sender, EventArgs e)
{
double consumption =  Convert.ToDouble(txtConsumption.Text);

double pricePerCCF = 0.00;

if (consumption >= 0.50)
{
pricePerCCF = 35.00;
}

double monthlyCharges = Calculations.Multiply(consumption, pricePerCCF);

txtPricePerCCF.Text = pricePerCCF.ToString("F");
txtMonthlyCharges.Text = monthlyCharges.ToString("F");
}
}
}```

18. In the top text box, type a decimal number between 1 and 10, such as 0.74
19. Click the Calculate button:

Less Than: <

To find out whether one value is lower than another, use the < operator. Its formula is:

`Value1 < Value2`

Less Than of Equal To: <=

The previous two operations can be combined to compare two values. This allows you to know if two values are the same or if the first value is lower than the second value. The operator used is <= and its syntax is:

`Value1 <= Value2`

The <= operation performs a comparison as any of the last two. If both Value1 and Value2 hold the same value, the result is true or positive. If the left operand, in this case Value1, holds a value lower than the second operand, in this case Value2, the result is still true.

Negating a Logical Operation or Value

As you might have found out, every logical operator has an opposite. They can be resumed as follows:

 Operator Meaning Example Opposite == Equality to a == b != != Not equal to 12 != 7 == < Less than 25 < 84 >= <= Less than or equal to Cab <= Tab > > Greater than 248 > 55 <= >= Greater than or equal to Val1 >= Val2 <

Negating a Condition

On the other hand, if you have a logical expression or value, to let you get its logical opposite, the C# language provides the logical NOT operator represented by !. The formula to use it is:

`!variable-or-expression`

Actually there are various ways the logical NOT operator is used. The classic way is to check the state of a variable. To nullify a variable, you can write the exclamation point to its left. Here is an example:

```bool employed = true;

bool validation = !employed;```

When a variable has been "negated", its logical value changes. If the logical value was true, it would be changed to false. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it.

Practical Learning: Ending the Lesson