Topics on Logical Operators
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
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; } } }
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; } } }
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 
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 netPay = Calculations.Add(Calculations.Multiply(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); } } }
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:
Practical Learning: Finding Out Whether a Value is Greater or Equal
namespace GasUtilityCompany1 { static public class Calculations { public static double Multiply(double a, double b) => a * b; } }
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 
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");
}
}
}
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:
!variableorexpression
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


Previous  Copyright © 20012021, FunctionX  Next 
